CKM
CKM

Reputation: 1971

what does lstm returns when return_state and return_sequences are false

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

Answers (1)

mujjiga
mujjiga

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.

Pictorial representation

![enter image description here

  • if return_sequences=True then vectors y1,y2,...yn are returned
  • if return_sequences=False then only vector yn is returned

Upvotes: 3

Related Questions