liveBook · Manning

Return_sequences

As explained in Deep Learning with R, Third Edition

return_sequences is a Keras recurrent-layer configuration option that makes the layer output its full sequence rather than only its final-timestep output.

return_sequences controls whether a Keras recurrent layer emits one output for the whole input sequence or an output for every timestep. With FALSE, the layer returns a rank-2 tensor shaped (batch_size, output_features); with TRUE, it returns a rank-3 tensor shaped (batch_size, timesteps, output_features). section 13.3.2

Returning every timestep

num_features <- 14 steps <- 120 inputs <- keras_input(shape = c(steps, num_features)) outputs <- inputs |> layer_simple_rnn(16, return_sequences = TRUE) op_shape(outputs) shape(NA, 120, 16)

Choose the setting according to how the next layer consumes the recurrent output:

  • Set return_sequences = TRUE when later processing must access every timestep, producing a rank-3 sequence. section 13.3.2
  • When stacking recurrent layers, set TRUE on every intermediate layer and use the final layer to produce the stack’s final output. section 13.3.5
Listing 13.24 Training and evaluating a stacked GRU model
inputs <- keras_input(shape = c(sequence_length, ncol_input_data))
outputs <- inputs |>
  layer_gru(32, recurrent_dropout = 0.5, return_sequences = TRUE) |>
  layer_gru(32, recurrent_dropout = 0.5) |>
  layer_dropout(0.5) |>
  layer_dense(1)
model <- keras_model(inputs, outputs)

callbacks <- list(
  callback_model_checkpoint(
    "jena_stacked_gru_dropout.keras", save_best_only = TRUE
  )
)

model |> compile(optimizer = "adam", loss = "mse", metrics = "mae")history <- model |> fit(
  train_dataset,
  epochs = 50,
  validation_data = val_dataset,
  callbacks = callbacks
)plot(history)model <- load_model("jena_stacked_gru_dropout.keras")
sprintf("Test MAE: %.2f", evaluate(model, test_dataset)[["mae"]])

[1] "Test MAE: 2.40"

A complete stacked GRU example shows return_sequences = TRUE on the first recurrent layer, followed by a final GRU layer, dropout, and a dense prediction layer. section 13.3.5

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines rank-2 and rank-3 output shapes and explains stacking, without descending into recurrent-cell internals.
Examples
Uses R/Keras SimpleRNN shape checks and a stacked GRU temperature-forecasting model.
What you can do after
Apply it via Keras to configure recurrent output shapes and connect multiple recurrent layers.
How it connects
Connects Keras recurrent layers with SimpleRNN, LSTM, GRU, and stacked recurrent networks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.