Recurrent Neural Networks
Recurrent Neural Networks
Hidden state model
Long-term Dependency: Natural language contains patterns, it need to learn the relationships or dependencies between tokens in a sequence. A convolutional layer can handle the local patch, if multiple convolutional layers are used, the number of pixels increase. Thus, to handle the long-term dependency with convolutional layers, the deep learning model should increase the filter size and number of layers to make the final outputs link to number of different pixels in the original image
Hidden state: aggregate all the previous information. The hidden state depends on the previous hidden state and the current input, so the final hidden state considers all tokens within the input

Computations

Computations of parameters:
- $h_t = \phi(w_{xh} \cdot x_{t-1} + w_{hh} \cdot h_{t-1} + b_h)$ which based on the previous hidden state and the current input
- $o_t = \hat{x}_t = \delta(w_{ho} \cdot h_t + b_o)$ which only depends on the current hidden state
Consider the input $x$ can be a vector:
- $\mathbf{H}_t = \phi(\mathbf{X}_{t-1}\mathbf{W}_{xh} + \mathbf{H}_{t-1}\mathbf{W}_{hh} + \mathbf{b}_h)$
- $\mathbf{O}_t = \hat{x}_t = \delta(\mathbf{H}_t\mathbf{W}_{ho} + \mathbf{b}_o)$
- Dimension of $\mathbf{X}_{t-1} = \text{batch size} \times \text{input dimension}$
- Dimension of $\mathbf{H}_{t} = \text{batch size} \times \text{number of hidden states}$
- number of hidden states (defined by model) is different from number of time steps (depends on the data)
- Note that the parameters $\mathbf{H}$ and $\mathbf{W}$ share across time steps
- Compare to CNN, RNN has the property of sequential dependency
Generalization
Suppose the model has been trained and now performing the language generation task
- We use 1 batch size, character-level tokens ()
Output: Softmax for classification as prediction

Language Generation:
- Input: “It is raining”
- Output 1: “It is raining outside”
- Output 2: “It is raining banana tree”
- Output 3: “It is raining piouw;kcj”
Evaluation
At each position, treat it as classification problem
- Similar to cross-entropy, we define the cross-entropy loss $\frac{1}{n} \sum_{t=1}^n -\log P(x_t | x_{t-1}, \dots, x_1)$ where $x_t$ is the the actual token observed at time step $t$
- Perplexity (1: oerfect match, positive infinity: worst case): $\exp \big( \frac{1}{n} \sum_{t=1}^n -\log P(x_t | x_{t-1}, \dots, x_1) \big) \in (1, +\inf)$
Problem: There are no novel sentences, different sentences can represent the same meaning
RNN Training
Forward Propagation

Formula:
Recursive Structure:
Note that $\{ \dots (x_{t-1}, h_{t-1}, o_{t-1}), (x_{t}, h_{t}, o_{t}), \dots \}$ depends on each other recursively
- $h_3$ can affect $L$ through $o_3$
- $h_2$ can affect $L$ through $o_2$, and further affect $h_3$, $o_3$ and $L$
- $h_1$ can affect $L$ through $o_1$, and further affect $h_2$, $o_2$ and $L$, and further affect $h_3$, $o_3$ and $L$
- $h_1$ can affect $L$ through 3 different ways
- In addition, the patches of $h_0$ and $h_1$, $h_1$ and $h_2$, $h_2$ and $h_3$ are all related to $\mathbf{W}_{hh}$
Loss Function:
- Gradient Vanishing and Explosion Problem: Since the network is repeatedly using the weight $\mathbf{W}_{hh}$ in different time steps, the eventual number of multiplications of the weight $\mathbf{W}_{hh}$ is equal to the number of time steps $\mathcal{O}(\mathbf{W}_{hh}^t)$, if $t$ is very large, the gradient vanishing or explosion problem become serious
Backward Propagation
Note that $L(x_1, \dots, x_T, y_1, \dots, y_T, w_h, w_o) = \frac{1}{T}\sum_{t=1}^T \ell (y_t, o_t)$ depends on $o_t$,
Note that $o_t = g(h_t, w_o)$ depends on $h_t$,
Note that $h_t = f(x_t, h_{t-1}, w_h)$ depends on both $ h_{t-1}$ and $w_h$,
Remind that $h_{t-1}$ is also depends on $w_h$, by chain rule,
And recursively,
Example

- $h_t = \mathbf{W}_{hx}\mathbf{x}_t + \mathbf{W}_{hh}\mathbf{h}_{t-1}$
- $o_t = \mathbf{W}_{qh}\mathbf{h}_t$
- $L = \frac{1}{T}\sum_{t=1}^T \ell (\mathbf{y}_t, \mathbf{o}_t)$
Change of $\mathbf{W}_{qh}$:
Change of $\mathbf{h}_t$:

- Direct path: $$ prod \big( \frac{\partial L}{\partial \mathbf{o}_t}, \frac{\partial \mathbf{o}_t}{\partial \mathbf{h}_t} \big) = \mathbf{W}_{qh}^\text{Tr} \frac{\partial L}{\partial \mathbf{o}_t} $$
- Indirect path: $$ prod \big( \frac{\partial L}{\partial \mathbf{h}_{t+1}}, \frac{\partial \mathbf{h}_{t+1}}{\partial \mathbf{h}_t} \big) = \mathbf{W}_{hh}^\text{Tr} \frac{\partial L}{\partial \mathbf{h}_{t+1}} $$
Change of $\mathbf{W}_{hx}$:
Change of $\mathbf{W}_{hh}$:
- Problems:
- Gradient Vanishing and Explosion: The term $(\mathbf{W}_{hh}^\text{Tr})^{T-i}$ is the source of gradient vanishing and explosion
- Inefficient
Truncation
Note that the term $(\mathbf{W}_{hh}^\text{Tr})^{T-i}$ is the source of gradient vanishing and explosion, one of the solutions is to reduce the value of $T-i$ by truncating the time steps
- Full computation: Inefficient and lead to gradient explosion or vanishing
- Truncating time steps: Truncate the sum after $\tau$ times
- Randomized truncation: Truncate the sum after random t times

Long-term dependency
The long dependency chain in RNN causes
- Gradient explosion and vanishing
- Slow computation
- Memory vanishing (the information of a previous time step vanishes after several time steps)
- Low GPU usage (the reason is RNN depends on the previous time step, which makes it difficult to perform the parallel computing within the RNN, unlike the CNN mainly local dependency that can compute parallelly, most of the calculations in RNN will be on the CPU instead of GPU)


