liveBook · Manning

GRU

As explained in Deep Learning with R, Third Edition

A gated recurrent unit (GRU) is a recurrent neural-network architecture that models sequence dependencies with update and reset mechanisms.

A gated recurrent unit (GRU) is a recurrent layer for ordered data. Like other recurrent neural networks, it processes a sequence one timestep at a time while carrying a state forward. It is a simpler, cheaper alternative to an LSTM, making it a practical choice for timeseries and text. section 20.1.6 section 15.1.1

Forecast temperature with stacked GRUs

With prepared sequence datasets and their input dimensions, build the model in R/Keras: 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) model |> compile(optimizer = "adam", loss = "mse", metrics = "mae") The first GRU returns an output at every timestep so the second GRU receives a sequence. The second returns its last output for the single-value forecast. Recurrent dropout and a separate dropout layer regularize the model. In the reported run, its test mean absolute error was 2.40 degrees, about 8.3% better than the baseline; the extra layer helped, but only modestly. 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"

The full example includes training, saving the best checkpoint, reloading it, and evaluating test error. section 13.3.5

The same layer can predict the next character of text. A small Shakespeare language model places a GRU after a character embedding and sets return_sequences = TRUE so a softmax classifier can make one prediction per input character. Its single GRU keeps the model simpler to train than a stack. section 15.1.1

Start with a commonsense baseline and a simpler model before paying for more recurrent layers. Stacking increases representational power and cost; it is not automatically worthwhile on a smaller problem. section 13.5

How Deep Learning with R, Third Edition covers it

How deep it goes
Shows working single-layer and stacked architectures, including sequence outputs and dropout, but does not derive GRU internals.
Examples
Uses a stacked temperature-forecasting model and a single-layer Shakespeare character language model.
What it assumes
Assumes prepared sequence datasets and input dimensions while explaining how intermediate recurrent layers pass full sequences onward.
What you can do after
Apply it via Keras layer_gru to build single-layer or stacked sequence models and evaluate them against a baseline.
How it connects
Connects GRUs to recurrent neural networks, LSTMs, embeddings, dropout, softmax classification, and timeseries forecasting.

Also explained in

Compare how 7 books explain GRU →

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.