3

我正在尝试开发一个有状态的 LSTM 网络。我将使用算法超参数搜索,但在此之前,它应该使用默认参数,但是,它没有。我不断收到“tensorflow.python.framework.errors_impl.InvalidArgumentError:无法使用形状为 [1000] 的张量更新形状为 [] 的变量,形状必须相等。” 我已经没有关于如何解决这个问题的想法了。

模型创建

一切似乎都很好。

self.model = Sequential()
self.model.add(Dense(init_dense_nodes,
                     batch_input_shape=(self.batch_size, None,14)))
# Adding first dense layers
for i in range(init_dense_layers):
   self.model.add(Dense(lstm_nodes,
                        activation=init_dense_activation))
   self.model.add(Dropout(dropout))

# Adding LSTM layers
for i in range(lstm_layers):
   self.model.add(LSTM(lstm_nodes,
                       activation=lstm_activation,
                       return_sequences=True,
                       stateful=True
                       ))
    self.model.add(Dropout(dropout))
    self.model.add(LSTM(init_dense_nodes, activation=lstm_activation, return_sequences=False))
# Adding last dense layers
for i in range(last_dense_layers):
    self.model.add(Dense(last_dense_nodes,
activation=last_dense_activation))
    self.model.add(Dropout(dropout))
    self.model.add(Flatten())
    self.model.add(Dense(4, activation='linear'))
    optimizer = Adam(lr=learning_rate, decay=learning_decay)
    self.model.compile(optimizer=optimizer, loss='mean_absolute_error', metrics=['mae'])
        print(self.model.summary())

训练和测试,输入和输出形状

训练输入形状:(86000, 1, 14) 训练输出形状:(86000, 1, 4) 测试输入形状:(70000, 1, 14) 测试输出形状:(70000, 1, 4)

网络总结:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (1000, None, 128)         1920      
_________________________________________________________________
dense_1 (Dense)              (1000, None, 128)         16512     
_________________________________________________________________
dropout (Dropout)            (1000, None, 128)         0         
_________________________________________________________________
dense_2 (Dense)              (1000, None, 128)         16512     
_________________________________________________________________
dropout_1 (Dropout)          (1000, None, 128)         0         
_________________________________________________________________
dense_3 (Dense)              (1000, None, 128)         16512     
_________________________________________________________________
dropout_2 (Dropout)          (1000, None, 128)         0         
_________________________________________________________________
lstm (LSTM)                  (1000, None, 128)         131584    
_________________________________________________________________
dropout_3 (Dropout)          (1000, None, 128)         0         
_________________________________________________________________
lstm_1 (LSTM)                (1000, None, 128)         131584    
_________________________________________________________________
dropout_4 (Dropout)          (1000, None, 128)         0         
_________________________________________________________________
lstm_2 (LSTM)                (1000, 128)               131584    
_________________________________________________________________
dense_4 (Dense)              (1000, 128)               16512     
_________________________________________________________________
dropout_5 (Dropout)          (1000, 128)               0         
_________________________________________________________________
dense_5 (Dense)              (1000, 128)               16512     
_________________________________________________________________
dropout_6 (Dropout)          (1000, 128)               0         
_________________________________________________________________
dense_6 (Dense)              (1000, 128)               16512     
_________________________________________________________________
dropout_7 (Dropout)          (1000, 128)               0         
_________________________________________________________________
flatten (Flatten)            (1000, 128)               0         
_________________________________________________________________
dense_7 (Dense)              (1000, 4)                 516       
=================================================================
Total params: 496,260
Trainable params: 496,260
Non-trainable params: 0
_________________________________________________________________
None

问题出现在执行此操作:

self.model.fit(x=self.train_dataset_x,
               y=self.train_dataset_y,
               epochs=1,
               shuffle=False,
               batch_size=self.batch_size,
               validation_data=(self.test_dataset_x, self.test_dataset_y))

# The error:
 File "Find_HP.py", line 33, in <module>
    the_model.fitness(the_model.default_parameters)
  File "\Training\Tr_Model\MakeModel.py", line 282, in fitness
    self.test_dataset_y))
  File "\lib\site-packages\tensorflow\python\keras\engine\training.py", line 880, in fit
    validation_steps=validation_steps)
  File "\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 329, in model_iteration
    batch_outs = f(ins_batch)
  File "\lib\site-packages\tensorflow\python\keras\backend.py", line 3076, in __call__
    run_metadata=self.run_metadata)
  File "\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)
  File "\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot update variable with shape [] using a Tensor with shape [1000], shapes must be equal.
     [[{{node metrics/mean_absolute_error/AssignAddVariableOp}}]]
     [[{{node loss/mul}}]]

有人有什么建议吗?

4

0 回答 0