# Lecture 10

## Introduction

Recurrent Networks offer a lot of flexibility:

1. one to one: Vanilla Neural Networks
2. one to many: e.g. Image Captioning (image -> sequence of words)
3. many to one: e.g. Sentiment Classification (sequence of words -> sentiment)
4. many to many:
• e.g. Machine Translation (seq of words -> seq of words)
• e.g. Video classification on frame level

RNN can also do sequential precessing of fix inputs (Multiple Object Recognition with Visual Attention, Ba et al.) or fixed outputs (DRAW: A Recurrent Neural Network For Image Generation, Gregor et al.).

## Recurrent Neural Network

### Concept

Usually we want to predict a vector at some time steps. To achieve this goal, we can process a sequence of vectors $x$ by applying a recurrence formula at every time step:

Notice: the same function and the same set of parameters are used at every time step. That’s to say, we use shared weights.

(Vanilla) Recurrent Neural Network

The state consists of a single “hidden” vector $h$:

• $h_t = tanh (W_{hh} h_{t-1} + W_{xh} x_t)$
• $y_t = W_{hy} h_t$

### Example: Character-level language model

We have a vocabulary of four characters $\begin{bmatrix} h & e & l & o \end{bmatrix}$, and the example training sequence is “hello”.

And we can look its the implement.

Data I/O

Initializations

Main Loop

Loss function

• forward pass (compute loss)
• backward pass (compute param gradient)

Sampling

Results

Using Shakespeare’s sonnet as input:

### Example: Image Captioning

We use CNN to recognize objects and use RNN to generate captions.

Cut the last two layers from CNN and connect it to RNN:

And smaple the output from previous layer to next layer as input:

Sampling is stoped when meeting an END

Finally, we’ll get a complete sentence (using Microsoft COCO dataset). The first row are good, but the second row may be not satisfactory.

Reference:

• Explain Images with Multimodal Recurrent Neural Networks, Mao et al.
• Deep Visual-Semantic Alignments for Generating Image Descriptions, Karpathy and Fei-Fei
• Show and Tell: A Neural Image Caption Generator, Vinyals et al.
• Long-term Recurrent Convolutional Networks for Visual Recognition andDescription, Donahue et al.
• Learning a Recurrent Visual Representation for Image CaptionGeneration, Chen and Zitnick

### More examples

We can also use RNN to generate open source textbooks written in LaTex, or generate C code from Linux source code, or searching for interpretable cells.

## Long Short Term Memory (LSTM)

• Truncated BPTT
• Clip gradients at threshold (something like anti-windup in control science LOL)
• RMSProp to adjust learning rate
• Harder to detect
• Weight Initialization
• ReLU activation functions
• RMSProp
• LSTM, GRUs (<– That’s why we use LSTM)

### Introduction

LSTM is proposed in [Hochreiter et al., 1997]. GRU is a knid of simplified LSTM.

ResNet is to PlainNet what LSTM is to RNN, kind of.

### Concept

LSTM have two states, one is cell state ($c$), another is hidden state ($h$):

• $i$: input gate, “add to memory”, decides whether do we want to add value to this cell.
• $f$: forget gate, “flush the memory”, decides whether to shut off the cell and reset the counter.
• $o$: output gate, “get from memory”, decides how much do we want to get from this cell.
• $g$: input, decides how much do we want to add to this cell.

## Summary

• RNNs allow a lot of flexibility inarchitecture design
• Vanilla RNNs are simple but don’twork very well
• Common to use LSTM or GRU: theiradditive interactions improve gradient flow
• Backward flow of gradients in RNNcan explode or vanish. Exploding is controlled with gradient clipping.Vanishing is controlled with additive interactions (LSTM)
• Better/simpler architectures are ahot topic of current research
• Better understanding (boththeoretical and empirical) is needed.

(To be improved by adding extra materials…)