You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
\item Original papers: Pascanu et al. (2013) On the Difficulty of Training RNNs, Bengio et al. (1994) Learning long-term dependencies is difficult.
1169
1169
\end{itemize}
1170
1170
\end{frame}
1171
+
1172
+
1173
+
PyTorch RNN Time Series Example
1174
+
1175
+
We first implement a simple RNN in PyTorch to forecast a univariate time series (a sine wave). The steps are: (1) generate synthetic data and form input/output sequences; (2) define an nn.RNN model; (3) train the model with MSE loss and an optimizer; (4) evaluate on a held-out test set. For example, using a sine wave as in prior tutorials , we create sliding windows of length seq_length. The code below shows each step. We use nn.RNN (the basic recurrent layer) followed by a linear output. The training loop (with MSELoss and Adam) updates the model to minimize prediction error .
1176
+
1177
+
import numpy as np
1178
+
import torch
1179
+
from torch import nn, optim
1180
+
1181
+
# 1. Data preparation: generate a sine wave and create input-output sequences
1182
+
time_steps = np.linspace(0, 100, 500)
1183
+
data = np.sin(time_steps) # shape (500,)
1184
+
seq_length = 20
1185
+
X, y = [], []
1186
+
for i in range(len(data) - seq_length):
1187
+
X.append(data[i:i+seq_length]) # sequence of length seq_length
1188
+
y.append(data[i+seq_length]) # next value to predict
• Model Explanation: Here input_size=1 because each time step has one feature. The RNN hidden state has size 16, and batch_first=True means input tensors have shape (batch, seq_len, features). We take the last RNN output and feed it through a linear layer to predict the next value .
1224
+
1225
+
# 3. Training loop: MSE loss and Adam optimizer
1226
+
criterion = nn.MSELoss() # mean squared error loss
• Training Details: We train for 50 epochs, printing the training loss every 10 epochs. As training proceeds, the loss (MSE) typically decreases, indicating the RNN is learning the sine-wave pattern .
1247
+
1248
+
# 4. Evaluation on test set
1249
+
model.eval()
1250
+
with torch.no_grad():
1251
+
pred = model(X_test)
1252
+
test_loss = criterion(pred, y_test)
1253
+
print(f'Test Loss: {test_loss.item():.4f}')
1254
+
1255
+
# (Optional) View a few actual vs. predicted values
1256
+
print("Actual:", y_test[:5].flatten().numpy())
1257
+
print("Pred : ", pred[:5].flatten().numpy())
1258
+
1259
+
• Evaluation: We switch to eval mode and compute loss on the test set. The lower test loss indicates how well the model generalizes. The code prints a few sample predictions against actual values for qualitative assessment. This simple PyTorch RNN code closely follows known tutorials .
1260
+
1261
+
TensorFlow (Keras) RNN Time Series Example
1262
+
1263
+
Next, we use TensorFlow/Keras to do the same task. We build a tf.keras.Sequential model with a SimpleRNN layer (the most basic recurrent layer)  followed by a Dense output. The workflow is similar: create the same synthetic sine data and split it into train/test sets; then define, train, and evaluate the model.
1264
+
1265
+
import numpy as np
1266
+
import tensorflow as tf
1267
+
1268
+
# 1. Data preparation: same sine wave data and sequences as above
1269
+
time_steps = np.linspace(0, 100, 500)
1270
+
data = np.sin(time_steps) # (500,)
1271
+
seq_length = 20
1272
+
X, y = [], []
1273
+
for i in range(len(data) - seq_length):
1274
+
X.append(data[i:i+seq_length])
1275
+
y.append(data[i+seq_length])
1276
+
X = np.array(X) # (480, seq_length)
1277
+
y = np.array(y) # (480,)
1278
+
# reshape for RNN: (samples, timesteps, features)
1279
+
X = X.reshape(-1, seq_length, 1) # (480, 20, 1)
1280
+
y = y.reshape(-1, 1) # (480, 1)
1281
+
1282
+
# Split into train/test (80/20)
1283
+
split = int(0.8 * len(X))
1284
+
X_train, X_test = X[:split], X[split:]
1285
+
y_train, y_test = y[:split], y[split:]
1286
+
1287
+
• Data: We use the same sine-wave sequence and sliding-window split as in the PyTorch example . The arrays are reshaped to (batch, timesteps, features) for Keras.
• Model Explanation: Here SimpleRNN(16) creates 16 recurrent units. The model summary shows the shapes and number of parameters. (Keras handles the sequence dimension internally.)
1307
+
1308
+
# 3. Training
1309
+
history = model.fit(
1310
+
X_train, y_train,
1311
+
epochs=50,
1312
+
batch_size=32,
1313
+
validation_split=0.2, # use 20% of train data for validation
1314
+
verbose=1
1315
+
)
1316
+
# Training progress prints loss/val_loss each epoch
1317
+
1318
+
• Training: We train for 50 epochs. The fit call also reports validation loss (using a 20% split of the training data) to monitor generalization. (This follows the standard Keras approach .)
• Evaluation: After training, we call model.evaluate on the test set. A low test loss indicates good forecasting accuracy. We also predict and compare a few samples of actual vs. predicted values. This completes the simple RNN forecasting example in TensorFlow.
1330
+
1331
+
Both examples use only basic RNN cells (no LSTM/GRU) and include data preparation, model definition, training loop, and evaluation. The PyTorch code uses nn.RNN as in common tutorials  , and the Keras code uses SimpleRNN layer . Each code block above is self-contained and can be run independently with standard libraries (NumPy, PyTorch or TensorFlow).
1332
+
1333
+
Sources: We adapted the PyTorch example from a tutorial using a sine-wave dataset   , and the Keras steps follow standard time-series RNN usage  .
0 commit comments