在Keras中拟合LSTM模型时input_shape错误

时间:2020-07-21 21:00:09

标签: keras lstm

我收到以下错误,无法找出原因:

InvalidArgumentError:  [_Derived_]  Invalid input_h shape: [1,32,5] [1,26,5]
     [[{{node CudnnRNN}}]]
     [[sequential_11/lstm_11/StatefulPartitionedCall]] [Op:__inference_train_function_27562]

这是代码:

def build_and_fit_model(n_epochs = 10, neurons=5,
                batch_size=32,
                stateful=True):
  
  layer = LSTM(neurons, 
               batch_input_shape=(batch_size, X_train.shape[1], X_train.shape[2]), 
               stateful=stateful)

  model = Sequential()
  model.add(layer)
  model.add(Dense(1))
  
  model.compile(loss='mean_squared_error',
                optimizer='adam')
  print(model.summary())
  
  for i in range(n_epochs):
      model.fit(X_train, y_train, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
      model.reset_states()

  return model

model = build_and_fit_model(stateful=True)

X_train的形状为(6522,30,1),y_train的形状为(6522,1)。

模型摘要为:

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_11 (LSTM)               (32, 5)                   140       
_________________________________________________________________
dense_10 (Dense)             (32, 1)                   6         
=================================================================
Total params: 146
Trainable params: 146
Non-trainable params: 0
_________________________________________________________________
None

0 个答案:

没有答案