连接到初始值设定项的LSTM值错误

2021-05-13 14:59:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Keras建立了一个LSTM模型。在

 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

输入的数据是形状的 十_火车.形状=(77100,34);Y_火车.形状=(77100,7)

Y数据是一个热编码的。对于最后一个列表条目,两个输入张量都是零填充的。Y峎train中的填充值为0。所以没有一个状态为填充端的值为1。dropout=0,hidden_units=2,这似乎与以下错误无关。在

不幸的是,我得到了以下的错误,我认为这与Y的形状有关,但我不能把我的手指放在它上面。在初始化/添加第一个LSTM层时发生错误。在

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.

如果我遵循错误,我注意到它归结为:

dtype: If set, initial_value will be converted to the given type. If None, either the datatype will be kept (if initial_value is a Tensor), or convert_to_tensor will decide.

“convert to tensor”创建一个对象,然后该对象为None并导致错误。显然,LSTM试图将输入转换为张量。。。但是如果我看一下我的输入,它已经是一个张量了。在

你们中有谁知道出了什么问题或者如何使用lambda作为初始化器吗?谢谢

编辑:堆栈跟踪

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)