Reputation: 1971
Here is my code.
from keras.layers import LSTM, Input
from keras.models import Model
x = Input(shape=(3,2))
lstm1 = LSTM(units=1, return_sequences=True, return_state=True)
lstm2 = LSTM(units=1, return_sequences=False, return_state=False)
out,h,c = lstm1(x)
out = lstm2(out, initial_state=[h,c])
model = Model(inputs=x, outputs=out)
Since both return_state
and return_sequences
are False
. However, the following code still returns something. I want to understand what is that?
import numpy as np
y=np.array([[3,1],[2,1],
[1,2]]).reshape((1,3,2))
z=model.predict(y)
print(z)
output:
[[-0.01467976]]
Upvotes: 1
Views: 2366
Reputation: 16906
From keras docs
return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence.
setting return_sequences=False
will return the state of the last LSTM sequence unwrapping, which in your case is of size 1
.
Change your units=10
then you will see that it will return an array of size 10.
return_sequences=True
then vectors y1,y2,...yn
are returnedreturn_sequences=False
then only vector yn
is returnedUpvotes: 3