Keras LSTM input dimension setting

后端 未结 1 2020
名媛妹妹
名媛妹妹 2020-12-01 11:44

I was trying to train a LSTM model using keras but I think I got something wrong here.

I got an error of

ValueError: Error when checking inpu

相关标签:
1条回答
  • 2020-12-01 11:47

    For the sake of completeness, here's what's happened.

    First up, LSTM, like all layers in Keras, accepts two arguments: input_shape and batch_input_shape. The difference is in convention that input_shape does not contain the batch size, while batch_input_shape is the full input shape including the batch size.

    Hence, the specification input_shape=(None, 20, 64) tells keras to expect a 4-dimensional input, which is not what you want. The correct would have been just (20,).

    But that's not all. LSTM layer is a recurrent layer, hence it expects a 3-dimensional input (batch_size, timesteps, input_dim). That's why the correct specification is input_shape=(20, 1) or batch_input_shape=(10000, 20, 1). Plus, your training array should also be reshaped to denote that it has 20 time steps and 1 input feature per each step.

    Hence, the solution:

    X_train = np.expand_dims(X_train, 2)  # makes it (10000,20,1)
    ...
    model = Sequential()
    model.add(LSTM(..., input_shape=(20, 1)))
    
    0 讨论(0)
提交回复
热议问题