The Wayback Machine - https://web.archive.org/web/20240927182105/https://www.geeksforgeeks.org/gated-recurrent-unit-networks/
Open In App

Gated Recurrent Unit Networks

Last Updated : 02 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Gated Recurrent Unit (GRU) is a type of recurrent neural network (RNN) that was introduced by Cho et al. in 2014 as a simpler alternative to Long Short-Term Memory (LSTM) networks. Like LSTM, GRU can process sequential data such as text, speech, and time-series data.

The basic idea behind GRU is to use gating mechanisms to selectively update the hidden state of the network at each time step. The gating mechanisms are used to control the flow of information in and out of the network. The GRU has two gating mechanisms, called the reset gate and the update gate.

The reset gate determines how much of the previous hidden state should be forgotten, while the update gate determines how much of the new input should be used to update the hidden state. The output of the GRU is calculated based on the updated hidden state.

The equations used to calculate the reset gate, update gate, and hidden state of a GRU are as follows:

Reset gate: r_t = sigmoid(W_r * [h_{t-1}, x_t])
Update gate: z_t = sigmoid(W_z * [h_{t-1}, x_t])
Candidate hidden state: h_t’ = tanh(W_h * [r_t * h_{t-1}, x_t])
Hidden state: h_t = (1 – z_t) * h_{t-1} + z_t * h_t’
where W_r, W_z, and W_h are learnable weight matrices, x_t is the input at time step t, h_{t-1} is the previous hidden state, and h_t is the current hidden state.

In summary, GRU networks are a type of RNN that use gating mechanisms to selectively update the hidden state at each time step, allowing them to effectively model sequential data. They have been shown to be effective in various natural language processing tasks, such as language modeling, machine translation, and speech recognition

Prerequisites: Recurrent Neural Networks, Long Short Term Memory Networks 

To solve the Vanishing-Exploding gradients problem often encountered during the operation of a basic Recurrent Neural Network, many variations were developed. One of the most famous variations is the Long Short Term Memory Network(LSTM). One of the lesser-known but equally effective variations is the Gated Recurrent Unit Network(GRU). 

Unlike LSTM, it consists of only three gates and does not maintain an Internal Cell State. The information which is stored in the Internal Cell State in an LSTM recurrent unit is incorporated into the hidden state of the Gated Recurrent Unit. This collective information is passed onto the next Gated Recurrent Unit. The different gates of a GRU are as described below:- 

  1. Update Gate(z): It determines how much of the past knowledge needs to be passed along into the future. It is analogous to the Output Gate in an LSTM recurrent unit.
  2. Reset Gate(r): It determines how much of the past knowledge to forget. It is analogous to the combination of the Input Gate and the Forget Gate in an LSTM recurrent unit.
  3. Current Memory Gate(  \overline{h}_{t}    ): It is often overlooked during a typical discussion on Gated Recurrent Unit Network. It is incorporated into the Reset Gate just like the Input Modulation Gate is a sub-part of the Input Gate and is used to introduce some non-linearity into the input and to also make the input Zero-mean. Another reason to make it a sub-part of the Reset gate is to reduce the effect that previous information has on the current information that is being passed into the future.

The basic work-flow of a Gated Recurrent Unit Network is similar to that of a basic Recurrent Neural Network when illustrated, the main difference between the two is in the internal working within each recurrent unit as Gated Recurrent Unit networks consist of gates which modulate the current input and the previous hidden state. 

Working of a Gated Recurrent Unit: 

  • Take input the current input and the previous hidden state as vectors. 
  • Calculate the values of the three different gates by following the steps given below:- 
    1. For each gate, calculate the parameterized current input and previously hidden state vectors by performing element-wise multiplication (Hadamard Product) between the concerned vector and the respective weights for each gate.
    2. Apply the respective activation function for each gate element-wise on the parameterized vectors. Below given is the list of the gates with the activation function to be applied for the gate.
Update Gate : Sigmoid Function
Reset Gate  : Sigmoid Function
  • The process of calculating the Current Memory Gate is a little different. First, the Hadamard product of the Reset Gate and the previously hidden state vector is calculated. Then this vector is parameterized and then added to the parameterized current input vector. 

    \overline{h}_{t} = tanh(W\odot x_{t}+W\odot (r_{t}\odot h_{t-1}))
  • To calculate the current hidden state, first, a vector of ones and the same dimensions as that of the input is defined. This vector will be called ones and mathematically be denoted by 1. First, calculate the Hadamard Product of the update gate and the previously hidden state vector. Then generate a new vector by subtracting the update gate from ones and then calculate the Hadamard Product of the newly generated vector with the current memory gate. Finally, add the two vectors to get the currently hidden state vector.
    h_{t} = z_{t}\odot h_{t-1} + (1-z_{t})\odot \overline{h}_{t}

    The above-stated working is stated as below:- 

Note that the blue circles denote element-wise multiplication. The positive sign in the circle denotes vector addition while the negative sign denotes vector subtraction(vector addition with negative value). The weight matrix W contains different weights for the current input vector and the previous hidden state for each gate. 

Just like Recurrent Neural Networks, a GRU network also generates an output at each time step and this output is used to train the network using gradient descent.  

Note that just like the workflow, the training process for a GRU network is also diagrammatically similar to that of a basic Recurrent Neural Network and differs only in the internal working of each recurrent unit. 

The Back-Propagation Through Time Algorithm for a Gated Recurrent Unit Network is similar to that of a Long Short Term Memory Network and differs only in the differential chain formation. 

Let \overline{y}_{t}       be the predicted output at each time step and y_{t}       be the actual output at each time step. Then the error at each time step is given by:- 

E_{t} = -y_{t}log(\overline{y}_{t})

The total error is thus given by the summation of errors at all time steps. 

E = \sum _{t} E_{t}
\Rightarrow E = \sum _{t} -y_{t}log(\overline{y}_{t})

Similarly, the value \frac{\partial E}{\partial W}       can be calculated as the summation of the gradients at each time step. 

\frac{\partial E}{\partial W} = \sum _{t} \frac{\partial E_{t}}{\partial W}

Using the chain rule and using the fact that \overline{y}_{t}       is a function of h_{t}       and which indeed is a function of \overline{h}_{t}       , the following expression arises:- 

\frac{\partial E_{t}}{\partial W} = \frac{\partial E_{t}}{\partial \overline{y}_{t}}\frac{\partial \overline{y}_{t}}{\partial h_{t}}\frac{\partial h_{t}}{\partial h_{t-1}}\frac{\partial h_{t-1}}{\partial h_{t-2}}......\frac{\partial h_{0}}{\partial W}

Thus the total error gradient is given by the following:- 

\frac{\partial E}{\partial W} = \sum _{t}\frac{\partial E_{t}}{\partial \overline{y}_{t}}\frac{\partial \overline{y}_{t}}{\partial h_{t}}\frac{\partial h_{t}}{\partial h_{t-1}}\frac{\partial h_{t-1}}{\partial h_{t-2}}......\frac{\partial h_{0}}{\partial W}

Note that the gradient equation involves a chain of \partial {h}_{t}       which looks similar to that of a basic Recurrent Neural Network but this equation works differently because of the internal workings of the derivatives of h_{t}       . 

How do Gated Recurrent Units solve the problem of vanishing gradients? 

The value of the gradients is controlled by the chain of derivatives starting from \frac{\partial h_{t}}{\partial h_{t-1}}       . Recall the expression for h_{t}       :- 

h_{t} = z_{t}\odot h_{t-1} + (1-z_{t})\odot \overline{h}_{t}

Using the above expression, the value for \frac{\partial {h}_{t}}{\partial {h}_{t-1}}       is:- 

\frac{\partial h_{t}}{\partial h_{t-1}} = z + (1-z)\frac{\partial \overline{h}_{t}}{\partial h_{t-1}}

Recall the expression for \overline{h}_{t}       :- 

\overline{h}_{t} = tanh(W\odot x_{t}+W\odot (r_{t}\odot h_{t-1}))

Using the above expression to calculate the value of \frac{\partial \overline{h_{t}}}{\partial h_{t-1}}       :- 

\frac{\partial \overline{h_{t}}}{\partial h_{t-1}} = \frac{\partial (tanh(W\odot x_{t}+W\odot (r_{t}\odot h_{t-1})))}{\partial h_{t-1}} \Rightarrow \frac{\partial \overline{h_{t}}}{\partial h_{t-1}} = (1-\overline{h}_{t}^{2})(W\odot r)

Since both the update and reset gate use the sigmoid function as their activation function, both can take values either 0 or 1. 

Case 1(z = 1): 

In this case, irrespective of the value of r       , the term \frac{\partial \overline{h_{t}}}{\partial h_{t-1}}       is equal to z which in turn is equal to 1. 

Case 2A(z=0 and r=0): 

In this case, the term \frac{\partial \overline{h_{t}}}{\partial h_{t-1}}       is equal to 0. 

Case 2B(z=0 and r=1): 

In this case, the term \frac{\partial \overline{h_{t}}}{\partial h_{t-1}}       is equal to (1-\overline{h}_{t}^{2})(W)       . This value is controlled by the weight matrix which is trainable and thus the network learns to adjust the weights in such a way that the term \frac{\partial \overline{h_{t}}}{\partial h_{t-1}}       comes closer to 1. 

Thus the Back-Propagation Through Time algorithm adjusts the respective weights in such a manner that the value of the chain of derivatives is as close to 1 as possible.
 



Previous Article
Next Article

Similar Reads

ML | Text Generation using Gated Recurrent Unit Networks
This article will demonstrate how to build a Text Generator by building a Gated Recurrent Unit Network. The conceptual procedure of training the network is to first feed the network a mapping of each character present in the text on which the network is training to a unique number. Each character is then hot-encoded into a vector which is the requi
6 min read
Recurrent Neural Networks Explanation
Today, different Machine Learning techniques are used to handle different types of data. One of the most difficult types of data to handle and the forecast is sequential data. Sequential data is different from other types of data in the sense that while all the features of a typical dataset can be assumed to be order-independent, this cannot be ass
8 min read
Bidirectional Recurrent Neural Network
Recurrent Neural Networks (RNNs) are a particular class of neural networks that was created with the express purpose of processing sequential input, including speech, text, and time series data. RNNs process data as a sequence of vectors rather than feedforward neural networks, which process data as a fixed-length vector. Each vector is processed d
9 min read
Text Generation using Recurrent Long Short Term Memory Network
This article will demonstrate how to build a Text Generator by building a Recurrent Long Short Term Memory Network. The conceptual procedure of training the network is to first feed the network a mapping of each character present in the text on which the network is training to a unique number. Each character is then hot-encoded into a vector which
5 min read
Introduction to Recurrent Neural Network
In this article, we will introduce a new variation of neural network which is the Recurrent Neural Network also known as (RNN) that works better than a simple neural network when data is sequential like Time-Series data and text data. What is Recurrent Neural Network (RNN)?Recurrent Neural Network(RNN) is a type of Neural Network where the output f
15+ min read
Signed Networks in Social Networks
Prerequisite: Introduction to Social Networks In Social Networks, Network is of 2 types- Unsigned Network and Signed Network. In the unsigned network, there are no signs between any nodes, and in the signed network, there is always a sign between 2 nodes either + or -. The '+' sign indicates friendship between 2 nodes and the '-' sign indicates enm
4 min read
Basics of Generative Adversarial Networks (GANs)
GANs is an approach for generative modeling using deep learning methods such as CNN (Convolutional Neural Network). Generative modeling is an unsupervised learning approach that involves automatically discovering and learning patterns in input data such that the model can be used to generate new examples from the original dataset. GANs is a way of
3 min read
Depth wise Separable Convolutional Neural Networks
Convolution is a very important mathematical operation in artificial neural networks(ANN's). Convolutional neural networks (CNN's) can be used to learn features as well as classify data with the help of image frames. There are many types of CNN's. One class of CNN's are depth wise separable convolutional neural networks. These type of CNN's are wid
4 min read
Use Cases of Generative Adversarial Networks
Image synthesis: Generating new, realistic images from a given data distribution, such as faces, landscapes, or animals. Text-to-Image synthesis: Generating images from text descriptions, such as scene descriptions, object descriptions, or attributes.Image-to-Image translation: Translating images from one domain to another, such as converting grays
5 min read
Long Short Term Memory Networks Explanation
Prerequisites: Recurrent Neural Networks To solve the problem of Vanishing and Exploding Gradients in a Deep Recurrent Neural Network, many variations were developed. One of the most famous of them is the Long Short Term Memory Network(LSTM). In concept, an LSTM recurrent unit tries to "remember" all the past knowledge that the network is seen so f
7 min read
Introduction to Residual Networks
Recent years have seen tremendous progress in the field of Image Processing and Recognition. Deep Neural Networks are becoming deeper and more complex. It has been proved that adding more layers to a Neural Network can make it more robust for image-related tasks. But it can also cause them to lose accuracy. That's where Residual Networks come into
3 min read
Understanding of LSTM Networks
This article talks about the problems of conventional RNNs, namely, the vanishing and exploding gradients, and provides a convenient solution to these problems in the form of Long Short Term Memory (LSTM). Long Short-Term Memory is an advanced version of recurrent neural network (RNN) architecture that was designed to model chronological sequences
9 min read
Residual Networks (ResNet) - Deep Learning
After the first CNN-based architecture (AlexNet) that win the ImageNet 2012 competition, Every subsequent winning architecture uses more layers in a deep neural network to reduce the error rate. This works for less number of layers, but when we increase the number of layers, there is a common problem in deep learning associated with that called the
10 min read
Dropout in Neural Networks
The concept of Neural Networks is inspired by the neurons in the human brain and scientists wanted a machine to replicate the same process. This craved a path to one of the most important topics in Artificial Intelligence. A Neural Network (NN) is based on a collection of connected units or nodes called artificial neurons, which loosely model the n
3 min read
DeepPose: Human Pose Estimation via Deep Neural Networks
DeepPose was proposed by researchers at Google for Pose Estimation in the 2014 Computer Vision and Pattern Recognition conference. They work on formulating the pose Estimation problem as a DNN-based regression problem towards body joints. They present a cascade of DNN-regressors which resulted in high precision pose estimates.. Architecture: Pose V
5 min read
Introduction to Social Networks using NetworkX in Python
Prerequisite - Python Basics Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social media sites such as Facebook, Instagram, and LinkedI
4 min read
Different dataset forms in Social Networks
Prerequisite: Python Basics For constructing any network we need a good dataset and every network has a different format for data of datasets. Basically a dataset is nothing but a huge collection of data that can be used further for any analysis. Data in dataset can be in many formats. Example of Network datasets: Ingredients network.Synonymy netwo
3 min read
Single Layered Neural Networks in R Programming
Neural networks also known as neural nets is a type of algorithm in machine learning and artificial intelligence that works the same as the human brain operates. The artificial neurons in the neural network depict the same behavior of neurons in the human brain. Neural networks are used in risk analysis of business, forecasting the sales, and many
5 min read
Activation functions in Neural Networks | Set2
The article Activation-functions-neural-networks will help to understand the use of activation function along with the explanation of some of its variants like linear, sigmoid, tanh, Relu and softmax. There are some other variants of the activation function like Elu, Selu, Leaky Relu, Softsign and Softplus which are discussed briefly in this articl
3 min read
Implementation of Erdos-Renyi Model on Social Networks
Prerequisite: Introduction to Social Networks, Erdos-Renyi Model Erdos Renyi model is used to create random networks or graphs on social networking. In the Erdos Reny model, each edge has a fixed probability of being present and being absent independent of the edges in a network. Implementing a Social Network using the Erdos-Renyi model:Step 1) Imp
3 min read
Selection and Social Influence-Homophily in Social Networks
Homophily is the tendency in social groups of similar people connected together. We often hear similar voices interact with like-minded people. Homophily has a significant impact on social media. Example - Birds with feather flock together. Assume there are 1000 people in a party out of which 500 are of age ranges from 18-25 and the other 500 peopl
2 min read
Basic Understanding of Bayesian Belief Networks
Bayesian Belief Network is a graphical representation of different probabilistic relationships among random variables in a particular set. It is a classifier with no dependency on attributes i.e it is condition independent. Due to its feature of joint probability, the probability in Bayesian Belief Network is derived, based on a condition — P(attri
4 min read
Fatman Evolutionary Model in Social Networks
Prerequisite: Introduction to Social Networks In the Fatman evolutionary model, there are 3 main concepts to make an evolutionary model i.e Homophily, Social influence, and closure. Homophily- People who are similar to each other, they tend to make friends with each other.Social Influence- People change their behavior and properties because of soci
4 min read
Spatial Segregation in Social Networks
Prerequisite - Introduction to Social Networks Segregation refers to the act of separation of people from others or the main group. Spatial Segregation refers to the distribution of social groups or any other elements in space. In Spatial Segregation, people tend to migrate to other places where they have more neighbors who are like them. According
6 min read
Cascading Behavior in Social Networks
Prerequisite: Introduction to Social Networks, Python Basics When people are connected in networks to each other then they can influence each other's behavior and decisions. This is called Cascading Behavior in Networks. Let's consider an example, assume all the people in a society have adopted a trend X. Now there comes new trend Y and a small gro
7 min read
Training Neural Networks with Validation using PyTorch
Neural Networks are a biologically-inspired programming paradigm that deep learning is built around. Python provides various libraries using which you can create and train neural networks over given data. PyTorch is one such library that provides us with various utilities to build and train neural networks easily. When it comes to Neural Networks i
8 min read
Episodic Memory and Deep Q-Networks
Episodic Memory: Episodic Memory is a category of long-term memory that involves recent recollection of specific events, situations, and experiences. For Example Your first day at college. There are two important aspects of episodic memory are Pattern Separation and Pattern Completion. Semantic Memory is used in many ways in machine learning such a
7 min read
Numpy Gradient - Descent Optimizer of Neural Networks
NumPy Gradient Descent Optimizer is a commonly used optimization algorithm in neural network training that is based on the gradient descent algorithm. It is used to minimize the cost function of a neural network model, by adjusting the model's weights and biases through a series of iterations. The basic steps of NumPy Gradient Descent Optimizer are
6 min read
GrowNet: Gradient Boosting Neural Networks
GrowNet was proposed in 2020 by students from Purdue, UCLA, and Virginia Tech in collaboration with engineers from Amazon and LinkedIn California. They proposed a new gradient boosting algorithm where they used a shallow neural network as the weak learners, a general loss function for training the gradient boosting models under classification, regr
6 min read
Understanding Multi-Layer Feed Forward Networks
Let's understand how errors are calculated and weights are updated in backpropagation networks(BPNs). Consider the following network in the below figure. The network in the above figure is a simple multi-layer feed-forward network or backpropagation network. It contains three layers, the input layer with two neurons x1 and x2, the hidden layer with
7 min read
Article Tags :
Practice Tags :