Path: blob/master/3 - Natural Language Processing with Sequence Models/Week 2/C3W2_L1_Hidden_State_Activation.ipynb
65 views
Hidden State Activation : Ungraded Lecture Notebook
In this notebook you'll take another look at the hidden state activation function. It can be written in two different ways.
I'll show you, step by step, how to implement each of them and then how to verify whether the results produced by each of them are same or not.
Background
This is the hidden state activation function for a vanilla RNN.
Which is another way of writing this:
Where
in the first formula is denotes the horizontal concatenation of and from the second formula.
in the first formula is then multiplied by , another concatenation of parameters from the second formula but this time in a different direction, i.e vertical!
Let us see what this means computationally.
Imports
Joining (Concatenation)
Weights
A join along the vertical boundary is called a horizontal concatenation or horizontal stack.
Visually, it looks like this:-
I'll show you two different ways to achieve this using numpy.
Note: The values used to populate the arrays, below, have been chosen to aid in visual illustration only. They are NOT what you'd expect to use building a model, which would typically be random variables instead.
Try using random initializations for the weight arrays.
Hidden State & Inputs
Joining along a horizontal boundary is called a vertical concatenation or vertical stack. Visually it looks like this:
I'll show you two different ways to achieve this using numpy.
Try using random initializations for the hiddent state and input matrices.
Verify Formulas
Now you know how to do the concatenations, horizontal and vertical, lets verify if the two formulas produce the same result.
Formula 1:
Formula 2:
To prove:- Formula 1 Formula 2
We will ignore the bias term and the activation function because the transformation will be identical for each formula. So what we really want to compare is the result of the following parameters inside each formula:
We'll see how to do this using matrix multiplication combined with the data and techniques (stacking/concatenating) from above.
Try adding a sigmoid activation function and bias term to the checks for completeness.
Summary
That's it! We've verified that the two formulas produce the same results, and seen how to combine matrices vertically and horizontally to make that happen. We now have all the intuition needed to understand the math notation of RNNs.