Okay, let's design the computational graph and derive the forward propagation equations for a recurrent neural network (RNN) that reads an entire sequence and produces a single output. This architecture is often referred to as a "many-to-one" RNN.
Problem Definition
- Input: A sequence of vectors ( { \boldsymbol{x}_1, \boldsymbol{x}_2, \dots, \boldsymbol{x}_T } ). Let the dimension of each input vector be ( d ).
- Hidden State: A sequence of hidden state vectors ( { \boldsymbol{h}_0, \boldsymbol{h}_1, \dots, \boldsymbol{h}T } ). Let the dimension of the hidden state be ( h{\text{dim}} ). ( \boldsymbol{h}_0 ) is the initial state, typically set to a zero vector.
- Output: A single output vector ( \boldsymbol{y} ). Let its dimension be ( o_{\text{dim}} ). This output is generated after processing the entire sequence, usually based on the final hidden state ( \boldsymbol{h}_T ).
- Parameters:
- Input-to-hidden weights: ( W_{\text{xh}} )
- Hidden-to-hidden (recurrent) weights: ( W_{\text{hh}} )
- Hidden layer bias: ( \boldsymbol{b}_h )
- Hidden-to-output weights: ( W_{\text{hy}} )
- Output layer bias: ( \boldsymbol{b}_y )
- Activation Functions:
- Hidden layer activation: ( \sigma ) (e.g., ( \tanh ) or ReLU)
- Output layer activation: ( g ) (e.g., Softmax for classification, linear for regression)
Computational Graph Design
The graph represents the computation flow. It's useful to visualize the RNN "unrolled" over the time steps of the sequence.
Key Elements of the Graph:
- Input Vectors (( \boldsymbol{x}_t )): Represent the input at each time step ( t ).
- Hidden States (( \boldsymbol{h}_t )): Represent the internal memory of the network. ( \boldsymbol{h}_t ) depends on ( \boldsymbol{x}t ) and ( \boldsymbol{h}{t-1} ).
- RNN Cell (or "Block"): A conceptual unit that performs the computation for a single time step. It takes ( \boldsymbol{x}t ) and ( \boldsymbol{h}{t-1} ) as input and outputs ( \boldsymbol{h}_t ).
- Recurrent Connection: The output hidden state ( \boldsymbol{h}_{t-1} ) from one time step feeds into the RNN cell of the next time step ( t ).
- Parameter Sharing: The weights ( W_{\text{xh}}, W_{\text{hh}}, \boldsymbol{b}_h ) are used in every RNN cell. This is a crucial characteristic of basic RNNs.
- Output Layer: Takes the final hidden state ( \boldsymbol{h}_T ) and computes the single output ( \boldsymbol{y} ).
Visual Representation:
graph TD
% Initialization
h0((h₀)):::initial --> h1
h0:::initial --> RnnCell1
% Time Step 1
x1((x₁)) --> RnnCell1
RnnCell1[RNN Cell]:::rnn --> h1((h₁))
Wxh1[W_xh, b_h]:::param --> RnnCell1
Whh1[W_hh]:::param --> RnnCell1
% Time Step 2
x2((x₂)) --> RnnCell2
h1 --> RnnCell2
RnnCell2[RNN Cell]:::rnn --> h2((h₂))
Wxh2[W_xh, b_h]:::param --> RnnCell2
Whh2[W_hh]:::param --> RnnCell2
% Ellipsis for intermediate steps
h2 --> Ellipsis
x3((x₃)) --> Ellipsis
Ellipsis[...] --> hT_in
xT((x_T)) --> hT_in
% Time Step T
hT_in --> RnnCellT
RnnCellT[RNN Cell]:::rnn --> hT((h_T))
WxhT[W_xh, b_h]:::param --> RnnCellT
WhhT[W_hh]:::param --> RnnCellT
% Output Layer
hT --> OutputLayer
OutputLayer[Output Layer]:::output --> y((y))
Why[W_hy]:::param --> OutputLayer
by[b_y]:::param --> OutputLayer
% Styling
classDef initial fill:#f9f,stroke:#333,stroke-width:2px;
classDef rnn fill:#ccf,stroke:#333,stroke-width:2px;
classDef param fill:#cfc,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
classDef output fill:#ffc,stroke:#333,stroke-width:2px;
% Annotating parameter sharing (conceptual)
linkStyle 0 stroke-dasharray: 5 5; % h0 -> RnnCell1 (conceptual)
linkStyle 2 stroke-dasharray: 5 5; % x1 -> RnnCell1 (conceptual)
linkStyle 4 stroke-dasharray: 5 5; % RnnCell1 -> h1 (conceptual)
linkStyle 5 stroke-dasharray: 5 5; % Wxh1 -> RnnCell1 (conceptual)
linkStyle 6 stroke-dasharray: 5 5; % Whh1 -> RnnCell1 (conceptual)
% Note: In a true computational graph, parameters are inputs to operations,
% not directly linked to cells like this in a diagram. This shows conceptual flow.
% The key is that W_xh, W_hh, b_h are *the same* for RnnCell1, RnnCell2, ..., RnnCellT.
Explanation of the Graph:
- The graph unrolls the recurrent process over ( T ) time steps.
- Each ( (\boldsymbol{x}t, \boldsymbol{h}{t-1}) \rightarrow \boldsymbol{h}_t ) block represents an RNN cell.
- The hidden state ( \boldsymbol{h}_{t-1} ) is passed from one cell to the next, forming the recurrent connection.
- The inputs ( \boldsymbol{x}_1, \boldsymbol{x}_2, \dots, \boldsymbol{x}_T ) are fed sequentially.
- The parameters ( W_{\text{xh}}, W_{\text{hh}}, \boldsymbol{b}_h ) are shown conceptually linked to the cells, emphasizing that the same parameters are used in each cell.
- Finally, the last hidden state ( \boldsymbol{h}T ) is passed to the Output Layer, which uses its own parameters ( W{\text{hy}}, \boldsymbol{b}_y ) to produce the single output ( \boldsymbol{y} ).
Forward Propagation Equations
We will use the convention where inputs and states are row vectors.
1. Initialization:
The initial hidden state ( \boldsymbol{h}0 ) is typically a zero vector.
[
\boldsymbol{h}0 = \mathbf{0}{1 \times h{\text{dim}}}
]
2. Hidden State Update (for each time step ( t = 1, 2, \dots, T )):
At each time step ( t ), the new hidden state ( \boldsymbol{h}_t ) is computed by combining the current input ( \boldsymbol{x}t ) and the previous hidden state ( \boldsymbol{h}{t-1} ), passing the result through a linear transformation and then an activation function.
First, calculate the pre-activation value (often denoted ( \boldsymbol{a}t )):
[
\boldsymbol{a}t = \boldsymbol{x}t W{\text{xh}} + \boldsymbol{h}{t-1} W{\text{hh}} + \boldsymbol{b}_h
]
where:
- ( \boldsymbol{x}_t \in \mathbb{R}^{1 \times d} ) is the input vector at time ( t ).
- ( W_{\text{xh}} \in \mathbb{R}^{d \times h_{\text{dim}}} ) is the input-to-hidden weight matrix.
- ( \boldsymbol{h}{t-1} \in \mathbb{R}^{1 \times h{\text{dim}}} ) is the previous hidden state.
- ( W_{\text{hh}} \in \mathbb{R}^{h_{\text{dim}} \times h_{\text{dim}}} ) is the hidden-to-hidden weight matrix.
- ( \boldsymbol{b}h \in \mathbb{R}^{1 \times h{\text{dim}}} ) is the hidden layer bias vector.
Next, apply the activation function ( \sigma ) element-wise to ( \boldsymbol{a}_t ) to get the current hidden state ( \boldsymbol{h}_t ):
[
\boldsymbol{h}_t = \sigma(\boldsymbol{a}_t)
]
This calculation is performed for ( t = 1, 2, \dots, T ).
3. Final Output Calculation:
After the loop finishes, the final hidden state ( \boldsymbol{h}_T ) captures the information processed from the entire sequence. This state is then used to compute the single output ( \boldsymbol{y} ).
First, calculate the pre-activation for the output layer:
[
\boldsymbol{a}_y = \boldsymbol{h}T W{\text{hy}} + \boldsymbol{b}_y
]
where:
- ( \boldsymbol{h}T \in \mathbb{R}^{1 \times h{\text{dim}}} ) is the final hidden state.
- ( W_{\text{hy}} \in \mathbb{R}^{h_{\text{dim}} \times o_{\text{dim}}} ) is the hidden-to-output weight matrix.
- ( \boldsymbol{b}y \in \mathbb{R}^{1 \times o{\text{dim}}} ) is the output layer bias vector.
Finally, apply the output activation function ( g ) element-wise to ( \boldsymbol{a}_y ) to obtain the final output ( \boldsymbol{y} ):
[
\boldsymbol{y} = g(\boldsymbol{a}_y)
]
Summary of Operations
- Initialize ( \boldsymbol{h}_0 = \mathbf{0} ).
- For ( t = 1 ) to ( T ):
a. Compute ( \boldsymbol{a}t = \boldsymbol{x}t W{\text{xh}} + \boldsymbol{h}{t-1} W_{\text{hh}} + \boldsymbol{b}_h ).
b. Compute ( \boldsymbol{h}_t = \sigma(\boldsymbol{a}_t) ).
- Compute ( \boldsymbol{y} = g(\boldsymbol{h}T W{\text{hy}} + \boldsymbol{b}_y) ).
This setup allows the RNN to process sequential data and compress the relevant information into its final hidden state to make a single prediction or classification.