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
