Welcome to the next step in your journey of mastering Recurrent Neural Networks (RNNs) for time series analysis. In the previous lesson, you learned how to prepare time series data for RNNs by normalizing it and converting it into sequences. This foundation is crucial as we now move on to building and evaluating a basic RNN model. In this lesson, you will learn how to define, train, and evaluate a simple RNN model using PyTorch. By the end of this lesson, you will be able to implement a basic RNN model to predict time series values and assess its performance.
Let's start by defining a basic RNN model. We will use PyTorch, which is a powerful tool for building neural networks. The model will consist of an RNN layer, followed by a Linear layer. The RNN layer is responsible for processing the sequences of data, while the Linear layer outputs the prediction.
Here's how you can define the model:
In this code, we define a class SimpleRNNModel that subclasses nn.Module. The model consists of an RNN layer with 10 hidden units and a Linear layer to produce the final output. The forward method defines the forward pass of the model, where we take the last output of the RNN sequence to pass through the Linear layer.
For time series data, it's important to split the data chronologically to avoid data leakage. This means using the earlier part of the series for training and the later part for testing.
Here's how you can split the data:
In this code, we use the first 80% of the data for training and the remaining 20% for testing, preserving the temporal order of the time series. This approach ensures that the model is always tested on data points that occur after those it was trained on, which is essential for time series forecasting.
With the model defined and data split, the next step is to train it using the training dataset. Training involves feeding the model with input sequences and adjusting its parameters to minimize the loss function.
Here's how you can train the model:
Explanation:
-
Forward Pass:
In the forward pass, the input batch (X_batch) is passed through the model (model(X_batch)). The model processes the input data and produces predictions (outputs). These predictions are then compared to the actual target values (y_batch) using the loss function (criterion). The result is a single loss value that quantifies how well the model's predictions match the actual values.
After training the model, it's important to evaluate its performance on the test set. One way to do this is by plotting the training loss curve and calculating the RMSE on the test data.
Here's how you can evaluate the model:
In this code, we first plot the training loss curve using Matplotlib. We then compute the RMSE on the test data by predicting the values using the trained model and rescaling them back to the original scale. The RMSE is calculated using the mean_squared_error function from scikit-learn.
Once the model is trained and evaluated, we can use it to make predictions on the test data. It's important to rescale the predictions back to the original scale to interpret them correctly. Visualizing the actual vs. predicted values can help assess the model's accuracy.
Here's how you can make predictions and visualize the results:
In this code, we plot the actual and predicted values on the test data using Matplotlib. The y_test_rescaled and y_pred_test_rescaled variables contain the rescaled actual and predicted values, respectively. The plot provides a visual comparison of the model's predictions against the actual test data, allowing you to assess its performance.

The plot above shows two lines: one for the actual values from the test set (in blue) and one for the predicted values generated by the RNN model (in orange, dashed). By comparing these lines, you can visually assess how closely the model's predictions follow the true values over time. Ideally, the predicted line should closely track the actual line, indicating that the model is accurately capturing the underlying patterns in the time series. Significant deviations between the two lines may suggest areas where the model could be improved, such as by tuning hyperparameters, increasing the model complexity, or providing more training data. This kind of visualization is a valuable diagnostic tool for understanding model performance beyond just numerical metrics like RMSE.
In this lesson, you learned how to build and evaluate a basic RNN model for time series forecasting using a train-test split. We defined the model using PyTorch, trained it on the training dataset, and evaluated its performance on the test dataset using the training loss curve and RMSE. You also learned how to make predictions and visualize the results to assess the model's accuracy.
As you move on to the practice exercises, focus on applying these techniques to your own datasets. Experiment with different model parameters and datasets to deepen your understanding. Keep up the great work, and happy learning!
