Recurrent Neural Networks in R

Last Updated : 14 Apr, 2026

Recurrent Neural Networks (RNNs) are designed to process sequential data such as time series, text or audio. RNNs are widely used in tasks like language translation, speech recognition and time series prediction. This article will demonstrate how to implement and train an RNN in R.

recurrent_neural_network
Architecture of RNN

Example: In next word prediction, if the input sequence is “I am going to”, an RNN can use the previous words to predict the next word like “school” or “market”. This ability to use past context makes RNNs important for sequence-based tasks.

Architecture of RNN

The architecture of an RNN consists of several key components that work together to process sequential data. The following are the major components:

1. Input Layer: receives the sequential data, which could be time series data, text or other forms of sequential data. Each element in the sequence is fed to the network one step at a time.

2. Hidden Layer(s): It is the core of the RNN. At each time step, the hidden layer updates its state based on the current input and the previous hidden state. This hidden state acts as the "memory" of the network, allowing it to capture dependencies across time steps.

3. Output Layer: This layer generates the final output of the network. For classification tasks, this might be a probability distribution over the possible classes. In regression tasks, the output could be a single continuous value.

Key Features of RNNs

Recurrent Neural Networks (RNNs) have unique characteristics that make them highly effective for processing sequential data.

1. Activation Functions: RNNs typically use activation functions such as tanh and ReLU are used in the hidden layers to introduce non-linearity, enabling the RNN to learn complex patterns.

2. Weight Sharing: RNNs use weight sharing, that the same set of weights is used at each time step, ensuring that the model has the same structure regardless of the time step.

3. Backpropagation Through Time (BPTT): RNNs use a process called Backpropagation Through Time to update the weights during training. BPTT unrolls the RNN across time steps and calculates the gradient of the loss function with respect to each weight by propagating the errors backward through the network.

Vanishing Gradient Problem

A limitation of standard RNNs is the vanishing gradient problem, where gradients become very small during backpropagation, making it difficult for the network to learn long-term dependencies. This issue is often addressed using more advanced RNN architectures like LSTM (Long Short-Term Memory) or GRU (Gated Recurrent Unit).

Implementation of RNN in R

We will be building recurrent neural network (RNN) using R programming language.

1. Installing and Loading the Packages

We will install and load the necessary R packages required to build, train and evaluate our Recurrent Neural Network (RNN) model. We will install tidyverse package for data manipulation and visualization and the keras package for building and training neural networks.

  • install.packages(): Installs the package in our R environment.
  • library(): Loads the installed function
R
install.packages(c("keras", "tidyverse"))
library(keras)
library(tidyverse)

2. Preprocessing the Data

We will preprocess the IMDB movie review dataset for text classification. We will load the IMDB dataset and split it into training and testing sets. Also we will pad the sequences to ensure they have the same length.

  • num_words = max_words: Limits the dataset to the top max_words most frequent words.
  • pad_sequences(): Pads sequences to ensure uniform input size.
R
max_words <- 10000
max_len <- 100

imdb <- dataset_imdb(num_words = max_words)

x_train <- imdb$train$x
y_train <- imdb$train$y
x_test <- imdb$test$x
y_test <- imdb$test$y

x_train <- pad_sequences(x_train, maxlen = max_len)
x_test <- pad_sequences(x_test, maxlen = max_len)

3. Defining the RNN Model

Next, we will define the architecture of the RNN model. We will use an embedding layer to represent words as dense vectors, add a vanilla RNN layer to process the word sequences and then add a dense output layer for binary classification.

  • keras_model_sequential(): Initializes a sequential model.
  • layer_embedding(input_dim, output_dim): Converts input data (word indices) into dense vectors of length output_dim.
  • layer_simple_rnn(units = 32): This is the vanilla RNN layer, where units = 32 specifies the number of units (neurons) in the hidden state.
  • layer_dense(units, activation): Adds a dense output layer with a sigmoid activation function for binary classification.
R
model <- keras_model_sequential() %>%
  layer_embedding(input_dim = max_words, output_dim = 32) %>%
  layer_simple_rnn(units = 32) %>%
  layer_dense(units = 1, activation = "sigmoid")

4. Compiling the Model

We will compile the model by specifying the loss function and optimization algorithm. We will use the Adam optimizer, binary cross-entropy as the loss function for binary classification and track accuracy as a metric.

  • optimizer: Specifies the Adam optimization algorithm.
  • loss = "binary_crossentropy": Defines the binary cross-entropy loss function, suitable for binary classification.
  • metrics = c("accuracy"): Tracks accuracy during training.
R
model %>% compile(
  optimizer = "adam",
  loss = "binary_crossentropy",
  metrics = c("accuracy")
)

5. Training the Model

We will now train the model using the training data. We will set the number of epochs to 10, use a batch size of 32. We will be using 20% of the training data for validation.

  • epochs = 10: Trains the model for 10 epochs.
  • batch_size = 32: Uses 32 samples per batch.
  • validation_split = 0.2: Reserves 20% of the training data for validation.
R
history <- model %>% fit(
  x_train, y_train,
  epochs = 10,
  batch_size = 32,
  validation_split = 0.2
)

6. Evaluating the Model

We will now evaluate the performance of the model on the test set. We will calculate the test accuracy.

  • evaluate(x_test, y_test): Evaluates the model's performance on the test data.
  • verbose = 0: Suppresses output during evaluation.
  • scores[[2]]: Extracts the accuracy score from the evaluation result.
R
scores <- model %>% evaluate(x_test, y_test, verbose = 0)
print(paste("Test accuracy:", scores[[2]]))

Output:

Test accuracy: 0.814639985561371

By adjusting the hyperparameters, adding layers or experimenting with other architectures, the model's performance can be further improved. An accuracy of 81.46% on the test set indicates a reasonably good performance, but further fine-tuning may increase the classification accuracy.

Comment

Explore