简体   繁体   中英

Keras in Python: LSTM Dimensions

I am building an LSTM network. My data looks as following:

X_train.shape = (134, 300000, 4)

X_train contains 134 sequences, with 300000 timesteps and 4 features.

Y_train.shape = (134, 2)

Y_train contains 134 labels, [1, 0] for True and [0, 1] for False.

Below is my model in Keras.

model = Sequential()
model.add(LSTM(4, input_shape=(300000, 4), return_sequences=True))
model.compile(loss='categorical_crossentropy', optimizer='adam')

Whenever I run the model, I get the following error:

Error when checking target: expected lstm_52 to have 3 dimensions, but got array with shape (113, 2)

It seems to be related to my Y_train data -- as its shape is (113, 2).

Thank you!

The output shape of your LSTM layer is (batch_size, 300000, 4) (because of return_sequences=True ). Therefore your model expects the target y_train to have 3 dimensions but you are passing an array with only 2 dimensions (batch_size, 2) .

You probably want to use return_sequences=False instead. In this case the output shape of the LSTM layer will be (batch_size, 4) . Moreover, you should add a final softmax layer to your model in order to have the desired output shape of (batch_size, 2) :

model = Sequential()
model.add(LSTM(4, input_shape=(300000, 4), return_sequences=False))
model.add(Dense(2, activation='softmax')) # 2 neurons because you have 2 classes
model.compile(loss='categorical_crossentropy', optimizer='adam')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM