1

In Keras, when using LSTM or GRU, if I set return_sequences=False, I will get the last output; if I set return_sequences=True, I will get the full sequence; but how to get them both at the same time?

today
  • 32,602
  • 8
  • 95
  • 115
yeoyi519
  • 13
  • 3

1 Answers1

1

Actually, the last timestep returned when return_sequences=True is equivalent to the output of LSTM layer when return_sequences=False:

lstm_out_rs = LSTM(..., return_sequences=True)(x)
lstm_out_rs[:,-1]  # this is the last timestep of returned sequence 

lstm_out = LSTM(..., return_sequences=False)(x)

lstm_out_rs[:,-1] and lstm_out are equivalent to each other. Therefore, to have them both you can use a Lambda layer:

lstm_out_rs = LSTM(..., return_sequences=True)(x)
out = Lambda(lambda t: [t, t[:,-1]])(lstm_out_rs)

# out[0] is all the outputs, out[1] is the last output
today
  • 32,602
  • 8
  • 95
  • 115