Comparison of GRU and LSTM in keras with an example

This recipe explains the key points of GRU and LSTM also the difference between GRU and LSTM using Keras in python is given

Recipe Objective

Difference between a GRU and LSTM. Explaining with an example.

The key difference between GRU and LSTM is that GRU's bag has two gates that are reset and update while LSTM has three gates that are input, output, forget. GRU is less complex than LSTM because it has less number of gates.

If the dataset is small then GRU is preferred otherwise LSTM for the larger dataset.

GRU exposes the complete memory and hidden layers but LSTM doesn't.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1- Importing Libraries

import keras from keras.models import Sequential from keras.layers import GRU, LSTM import numpy as np

Step 2- Defining two different models

We will define two different models and Add a GRU layer in one model and an LSTM layer in the other model.

# define model where GRU is also output layer model_1 = Sequential() model_1.add(GRU(1, input_shape=(20,1))) model_1.compile(optimizer='adam', loss='mse') # define model where LSTM is also output layer model_2 = Sequential() model_2.add(LSTM(1, input_shape=(50,1))) model_2.compile(optimizer='adam', loss='mse')

 

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 3- Define a sample array.

We will define a sample array to run in both models.

# input time steps y = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36, 37, 38, 39, 40]]).reshape((5,10,1)) # make and show prediction print(model_1.predict(y))

[[6.1044526e-01]
 [4.0416101e-01]
 [1.4171210e-02]
 [1.2617696e-04]
 [8.3446486e-07]]

# input time steps y = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36, 37, 38, 39, 40]]).reshape((5,10,1)) # make and show prediction print(model_2.predict(y))

[[-1.9881524e-02]
 [-5.2695298e-01]
 [-3.5639611e-04]
 [-3.7144428e-06]
 [-2.5736982e-08]]

Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.