keras: LSTM (return_sequences=True) followed by TimeDistributed(Dense) doesn't work!
Hello, everyone, Recently I wanted to build a network as following Input --> sequence padding and masking 0 --> LSTM (return_sequence=True) --> Timedistributed(Dense(outputnumber, softmax)), but an error of mismatched dimension was reported.
# Model definition
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(None,100)))
model.add(LSTM(args.dimension, dropout_W=0.2, dropout_U=0.2, return_sequences=True))
model.add(TimeDistributed(Dense(97, activation='softmax')))
The error is as : ValueError: Error when checking model target: expected timedistributed_1 to have 3 dimensions, but got array with shape (3000, 97)
The dataset has 3000 sequences as input.
Thanks.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 16 (3 by maintainers)
Thanks again, I have figured out why. Since I thought the softmax layer would automatically deal with the timedistributed stuff, I didn’t explicitly set expand the labels to 3-dimension. Now it works.
Can you please share your workaround solution? I faced the same problem!
Would be great if somebody could explain! Thank you!
Here is the code
And the error happens when I try to fit the model:
The error is
Thanks for reply, and the output of model.summary() (The input feature dimension is now 39): `
I tried to change the input shape to (None,None,39) but it will report errors of dimension mismatch. ` By the way, if I set the return_sequences=False, and no timedistributed stuff used, everything works fine. I just thought maybe it was the wrong usage of TimeDistributed
The input to the LSTM should also be a 3D array. Make sure that is the case.
Your masking input_shape seems weird and should probably have 3 dims.
Make sure to mention when the error occurs. When you build the model, when you compile, when you run?
Try running
model.summary()
. If the error happens when you start passing data, check the shape of your data.