I am using Keras to built a LSTM model.
def LSTM_model_1(X_train,Y_train,Dropout,hidden_units):
model = Sequential()
model.add(Masking(mask_value=666, input_shape=(X_train.shape[1],X_train.shape[2])))
model.add(LSTM(hidden_units, activation='tanh', return_sequences=True, dropout=Dropout))
model.add(LSTM(hidden_units, return_sequences=True))
model.add(LSTM(hidden_units, return_sequences=True))
model.add(Dense(Y_train.shape[-1], activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='adam',metrics['categorical_accuracy'])
return model
The input data is of shape X_train.shape=(77,100,34); Y_Train.shape=(77,100,7)
The Y data is one-hot-encoded. Both input tensors are zero-padded for the last list entry. The padded values in Y_train is 0. So no state gets a value of 1 for the padded end. dropout=0 and hidden_units=2 which seems not related to the following error.
Unfortunately, I get following error which I think is connected with the shape of Y. But I cannot put my finger on it. The error happens when the first LSTM layer is initialized/added.
ValueError: Initializer for variable lstm_58/kernel/ is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.
If I follow the error I noticed that it comes down to this:
dtype: If set, initial_value will be converted to the given type. If
None
, either the datatype will be kept (ifinitial_value
is a Tensor), orconvert_to_tensor
will decide.
"convert to tensor' creates an object which is then None and leads to the error. Apparently, the LSTM tries to convert the input into a tensor... But if I look at my input, it is already a tensor.
Does any of you have an idea what went wrong or how to use lambda as an initializer? Thanks
EDit: the stack trace
File "C:\Users\310122653\Documents\GitHub\DNN\build_model.py", line 44, in LSTM_model_1 model.add(LSTM(hidden_units, activation='tanh', return_sequences=True, dropout=Dropout))
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\models.py", line 492, in add output_tensor = layer(self.outputs[0])
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\recurrent.py", line 499, in call return super(RNN, self).call(inputs, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 592, in call self.build(input_shapes[0])
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\recurrent.py", line 461, in build self.cell.build(step_input_shape)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\recurrent.py", line 1838, in build constraint=self.kernel_constraint)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 416, in add_weight constraint=constraint)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 395, in variable v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 235, in init constraint=constraint)
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 356, in _init_from_args "initializer." % name)
model.add(Masking(mask_value=666, input_shape=X_train.shape[1],X_train.shape[2])))
(it seems the input shape should be(X_train.shape[1], X_train.shape[2])
instead. – Daniel Möller