Deep Learning and Applications (202047804)
12202080501060
Practical 8:
Aim: Implement GRU Model and Test it for a Given Dataset
About GRU Model:
Gated Recurrent Unit (GRU) is a variant of Recurrent Neural Networks (RNNs), similar to LSTM
but with a simplified architecture. GRUs combine the forget and input gates into a single update
gate, making them computationally more efficient while still addressing the vanishing gradient
problem.
Key Points of GRU: - GRUs are simpler than LSTMs but often achieve comparable performance.
- They use fewer parameters, making them faster to train.
- Particularly effective when training data is limited or computation resources are constrained.-
Commonly used in time series forecasting, speech recognition, and NLP tasks.
Python Code Implementation (with LSTM vs GRU Comparison):
import numpy as np import matplotlib.pyplot as plt from
pandas import read_csv import math import tensorflow
as tf from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM,
GRU from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# Load dataset dataframe = read_csv('/content/drive/MyDrive/DataSet/airline-passengers.csv', usecols=[1],
engine='py dataset = dataframe.values dataset = dataset.astype('float32')
# Normalize the dataset scaler =
MinMaxScaler(feature_range=(0, 1)) dataset =
scaler.fit_transform(dataset)
# Split into train and test sets train_size = int(len(dataset) * 0.67) test_size =
len(dataset) - train_size train, test = dataset[0:train_size,:],
dataset[train_size:len(dataset),:]
# Convert an array of values into a dataset matrix def
create_dataset(dataset, look_back=1):
dataX, dataY = [], [] for i in
range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a) dataY.append(dataset[i +
look_back, 0]) return np.array(dataX),
np.array(dataY)
look_back = 10 trainX, trainY = create_dataset(train,
look_back) testX, testY = create_dataset(test,
look_back)
# Reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX =
np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# Build LSTM model
lstm_model = Sequential()
GCET 41
Deep Learning and Applications (202047804)
12202080501060
lstm_model.add(LSTM(10, input_shape=(1, look_back)))
lstm_model.add(Dense(1))
lstm_model.compile(loss='mean_squared_error', optimizer='adam')
lstm_model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=0)
# Predictions using LSTM trainPredict_LSTM =
lstm_model.predict(trainX) testPredict_LSTM =
lstm_model.predict(testX)
# Build GRU model
gru_model = Sequential() gru_model.add(GRU(10, input_shape=(1,
look_back))) gru_model.add(Dense(1))
gru_model.compile(loss='mean_squared_error', optimizer='adam')
gru_model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=0)
# Predictions using GRU trainPredict_GRU =
gru_model.predict(trainX) testPredict_GRU =
gru_model.predict(testX)
# Invert predictions back to original scale trainPredict_LSTM =
scaler.inverse_transform(trainPredict_LSTM) testPredict_LSTM =
scaler.inverse_transform(testPredict_LSTM) trainPredict_GRU =
scaler.inverse_transform(trainPredict_GRU) testPredict_GRU =
scaler.inverse_transform(testPredict_GRU) trainY =
scaler.inverse_transform([trainY]) testY =
scaler.inverse_transform([testY])
# Calculate RMSE lstm_trainScore = np.sqrt(mean_squared_error(trainY[0],
trainPredict_LSTM[:,0])) lstm_testScore = np.sqrt(mean_squared_error(testY[0],
testPredict_LSTM[:,0])) gru_trainScore = np.sqrt(mean_squared_error(trainY[0],
trainPredict_GRU[:,0])) gru_testScore = np.sqrt(mean_squared_error(testY[0],
testPredict_GRU[:,0]))
print('LSTM Train Score: %.2f RMSE' % (lstm_trainScore))
print('LSTM Test Score: %.2f RMSE' % (lstm_testScore))
print('GRU Train Score: %.2f RMSE' % (gru_trainScore))
print('GRU Test Score: %.2f RMSE' % (gru_testScore))
# Plot results for comparison plt.figure(figsize=(12,6)) plt.plot(scaler.inverse_transform(dataset),
label='Actual Data') plt.plot(np.concatenate((np.full((look_back,1), np.nan), trainPredict_LSTM,
testPredict_LSTM)), label plt.plot(np.concatenate((np.full((look_back,1), np.nan), trainPredict_GRU,
testPredict_GRU)), label= plt.legend()
plt.show()
Output :
GCET 42
Deep Learning and Applications (202047804)
12202080501060
Conclusion:
In this practical, we implemented both LSTM and GRU models for time series forecasting using the
airline passengers dataset. Both models captured the underlying trend effectively. However, the
GRU model, due to its simpler architecture with fewer parameters, trained faster and provided
comparable results to the LSTM model. The plotted comparison graph showed that both models
closely follow the actual data trend, proving that GRU can be a computationally efficient alternative
to LSTM for sequential data prediction.
GCET 43