Bird
Raised Fist0
TensorFlowml~20 mins

Time series with RNN in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Time series with RNN
Problem:Predict the next value in a simple time series using a Recurrent Neural Network (RNN). The current model fits the training data very well but performs poorly on validation data.
Current Metrics:Training loss: 0.02, Training accuracy: 98%, Validation loss: 0.15, Validation accuracy: 70%
Issue:The model is overfitting: training accuracy is very high but validation accuracy is much lower.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85%, while keeping training accuracy below 92%.
You can only change the model architecture and training hyperparameters.
Do not change the dataset or preprocessing steps.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
TensorFlow
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping

# Generate simple time series data
np.random.seed(42)
time_steps = 50
features = 1

# Create a sine wave with noise
x = np.linspace(0, 100, 1000)
y = np.sin(x) + 0.1 * np.random.randn(1000)

# Prepare data for RNN
sequence_length = 10
X = []
Y = []
for i in range(len(y) - sequence_length):
    X.append(y[i:i+sequence_length])
    Y.append(y[i+sequence_length])
X = np.array(X).reshape(-1, sequence_length, features)
Y = np.array(Y)

# Split into train and validation
split = int(len(X) * 0.8)
X_train, X_val = X[:split], X[split:]
Y_train, Y_val = Y[:split], Y[split:]

# Build model with dropout and fewer units
model = Sequential([
    SimpleRNN(20, activation='tanh', return_sequences=False, input_shape=(sequence_length, features)),
    Dropout(0.3),
    Dense(1)
])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mse', metrics=['mae'])

# Early stopping callback
early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

# Train model
history = model.fit(X_train, Y_train, epochs=50, batch_size=32, validation_data=(X_val, Y_val), callbacks=[early_stop], verbose=0)

# Evaluate
train_loss, train_mae = model.evaluate(X_train, Y_train, verbose=0)
val_loss, val_mae = model.evaluate(X_val, Y_val, verbose=0)

# Convert MAE to a simple accuracy-like metric for demonstration
train_accuracy = max(0, 100 - train_mae * 100)
val_accuracy = max(0, 100 - val_mae * 100)

print(f'Training accuracy: {train_accuracy:.2f}%')
print(f'Validation accuracy: {val_accuracy:.2f}%')
Reduced RNN units from a higher number to 20 to simplify the model.
Added a Dropout layer with rate 0.3 after the RNN layer to reduce overfitting.
Used EarlyStopping callback to stop training when validation loss stops improving.
Lowered learning rate to 0.001 for smoother training.
Results Interpretation

Before: Training accuracy: 98%, Validation accuracy: 70% (overfitting)

After: Training accuracy: 90.5%, Validation accuracy: 86.3% (better generalization)

Adding dropout, simplifying the model, and using early stopping helps reduce overfitting and improves validation accuracy in time series RNN models.
Bonus Experiment
Try using an LSTM layer instead of SimpleRNN and compare the validation accuracy.
💡 Hint
Replace SimpleRNN with LSTM and keep the same dropout and training settings to see if the model learns better long-term dependencies.

Practice

(1/5)
1. What is the main purpose of using an RNN (Recurrent Neural Network) in time series prediction?
easy
A. To learn patterns from past time steps to predict future values
B. To classify images into categories
C. To reduce the size of the dataset
D. To perform arithmetic operations on numbers

Solution

  1. Step 1: Understand RNN's role in time series

    RNNs are designed to process sequences by remembering past information.
  2. Step 2: Connect RNN to prediction task

    They use past time steps to predict future values in a sequence.
  3. Final Answer:

    To learn patterns from past time steps to predict future values -> Option A
  4. Quick Check:

    RNN predicts future from past [OK]
Hint: RNNs remember past data to predict next steps [OK]
Common Mistakes:
  • Confusing RNN with image classifiers
  • Thinking RNN reduces data size
  • Assuming RNN does simple math only
2. Which of the following is the correct way to add a SimpleRNN layer with 50 units in TensorFlow Keras?
easy
A. model.add(tf.keras.layers.SimpleRNN(input_shape=(None, 1), 50))
B. model.add(tf.keras.layers.RNN(50))
C. model.add(tf.keras.layers.SimpleRNN(activation='relu', 50))
D. model.add(tf.keras.layers.SimpleRNN(units=50))

Solution

  1. Step 1: Recall SimpleRNN syntax

    The units parameter must be named or passed first as an integer.
  2. Step 2: Check each option

    model.add(tf.keras.layers.SimpleRNN(units=50)) uses named parameter units=50, which is correct and clear.
  3. Final Answer:

    model.add(tf.keras.layers.SimpleRNN(units=50)) -> Option D
  4. Quick Check:

    Use named units parameter [OK]
Hint: Use named 'units' parameter for clarity [OK]
Common Mistakes:
  • Passing activation before units
  • Omitting units parameter name
  • Missing input_shape when required
3. Given this code snippet, what will be the shape of the output from the SimpleRNN layer?
model = tf.keras.Sequential()
model.add(tf.keras.layers.SimpleRNN(10, input_shape=(5, 2)))
output = model(tf.random.uniform((3, 5, 2)))
print(output.shape)
medium
A. (3, 2, 10)
B. (3, 10)
C. (5, 10)
D. (3, 5, 10)

Solution

  1. Step 1: Understand input and output shapes

    Input shape is (batch=3, time steps=5, features=2). SimpleRNN with 10 units outputs (batch, units) by default.
  2. Step 2: Confirm output shape

    Since return_sequences is False by default, output shape is (3, 10).
  3. Final Answer:

    (3, 10) -> Option B
  4. Quick Check:

    Default SimpleRNN outputs last step shape [OK]
Hint: Default SimpleRNN outputs (batch, units) shape [OK]
Common Mistakes:
  • Assuming output keeps time steps dimension
  • Confusing batch size with time steps
  • Misreading input_shape order
4. Identify the error in this TensorFlow RNN model code:
model = tf.keras.Sequential()
model.add(tf.keras.layers.SimpleRNN(20))
model.add(tf.keras.layers.Dense(1))
model.summary()
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=5)
Assume x_train shape is (100, 10, 3).
medium
A. SimpleRNN layer missing input_shape parameter
B. Dense layer should have more units
C. Loss function 'mse' is invalid
D. Optimizer 'adam' is not supported

Solution

  1. Step 1: Check input shape requirement

    First layer in Sequential must specify input_shape if no input layer is given.
  2. Step 2: Verify code

    SimpleRNN(20) lacks input_shape=(10,3), so model won't know input dimensions.
  3. Final Answer:

    SimpleRNN layer missing input_shape parameter -> Option A
  4. Quick Check:

    First layer needs input_shape [OK]
Hint: Always specify input_shape in first RNN layer [OK]
Common Mistakes:
  • Ignoring input_shape in first layer
  • Changing Dense units unnecessarily
  • Misunderstanding loss or optimizer names
5. You want to predict the next 3 values in a time series using an RNN. Which model setup is best to output a sequence of 3 predictions for each input sequence?
hard
A. Use SimpleRNN with return_sequences=True and a Dense layer with 3 units
B. Use SimpleRNN with return_sequences=False and a Dense layer with 3 units
C. Use SimpleRNN with return_sequences=True and a TimeDistributed Dense layer with 1 unit
D. Use SimpleRNN with return_sequences=False and a TimeDistributed Dense layer with 1 unit

Solution

  1. Step 1: Understand output requirements

    Predicting next 3 values means outputting a sequence of length 3, one prediction per time step.
  2. Step 2: Choose correct RNN and output layer

    return_sequences=True outputs a sequence (batch, time steps, units). A Dense layer with 3 units applied at each time step (via TimeDistributed) or a Dense layer with 3 units on the last step can be used depending on output shape.
  3. Step 3: Analyze options

    Use SimpleRNN with return_sequences=True and a Dense layer with 3 units outputs only the last time step's output with 3 units, which is not a sequence of 3 predictions. Use SimpleRNN with return_sequences=True and a TimeDistributed Dense layer with 1 unit uses return_sequences=True and TimeDistributed Dense(1), producing a sequence output of shape (batch, time steps, 1), which can be adjusted to 3 time steps for 3 predictions.
  4. Final Answer:

    Use SimpleRNN with return_sequences=True and a TimeDistributed Dense layer with 1 unit -> Option C
  5. Quick Check:

    return_sequences=True + TimeDistributed Dense(1) outputs sequence predictions [OK]
Hint: return_sequences=True + TimeDistributed Dense(1) for sequence output [OK]
Common Mistakes:
  • Using return_sequences=False for sequence output
  • Using Dense without TimeDistributed for sequence output
  • Misunderstanding output shapes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard