Search code examples
python-3.xlstmkeras-layerkeyerror

Keras LSTM input/output shape


I need outputs at every recurrent layer and my setup is as follows:

100 training examples, 3 time steps per example, and 20-d feature vector for each individual element.

x_train: (100,3,20)

y_train: (100,20)

LSTM architecture:

model.add(LSTM(20, input_shape=(3,20), return_sequences=True))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accuracy'])
model.summary()

Training:

history = model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test))

Error:

ValueError: Dimensions must be equal, but are 20 and 3 for '{{node Equal}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](IteratorGetNext:1, Cast_1)' with input shapes: [?,20], [?,3].

Please help me with the correct input/output LSTM dimensions. Thanks


Solution

  • LSTM(20, input_shape=(3,20), return_sequences=True) takes as input shape (100,3,20) and returns (100,3,20). Your target output is however encoded as (100,20).

    From the dimensions, I assume you want to map each sequence to a non-sequence, i.e. you can do:

    LSTM(20, input_shape=(3,20), return_sequences=False)
    

    This will return the final hidden state, i.e. a shape of (100,20) which matches your target output.