AI Hints
Home›Deep Learning Tutorial›Sequences and Memory
Deep Learning · Lesson 26 of 92

Sequences and Memory

Recurrent neural networks.

⏱ 2 min read▶ Video at 53:41🎓 Free
Watch this lesson in the Deep Learning courseWatch on YouTube ↗Full Deep Learning course

Read this sentence. The movie was not bad. Now try reading those same words, in a random order. Bad, was, movie, the, not. The meaning has gone. Order carries meaning. And a network that looks at everything at once has no idea which word came first. Aria needs a memory.

Sequences and Memory
From the video: Sequences and Memory
  • Order carries meaning
  • Shuffled words mean nothing
  • Aria needs a memory

A network with a memory

A recurrent neural network reads one word at a time. It keeps a small vector, called the hidden state, which is its memory. For each new word, it combines that word with its memory, and writes a new memory. Then it passes that along to the next step. By the end of the sentence, the memory holds a summary of everything so far. Notice that the very same network is reused at every step, so it can handle sentences of any length.

Shapes in an RNN

In PyTorch, it's one line. Our input has two sentences, five words each, and eight numbers for each word. The R N N returns two things. Output has the memory at every step. Hidden has only the final memory. Run it. Five steps, with sixteen numbers of memory each. For classifying a whole sentence, you'd use the final memory. To generate text, you'd use every step.

rnn.pypython
import torch
from torch import nn

rnn = nn.RNN(input_size=8, hidden_size=16, batch_first=True)

x = torch.rand(2, 5, 8)   # 2 sentences, 5 words, 8 numbers per word
output, hidden = rnn(x)

print("Input:         ", list(x.shape))
print("Each step:     ", list(output.shape))
print("Final memory:  ", list(hidden.shape))
Output
Input:          [2, 5, 8]
Each step:      [2, 5, 16]
Final memory:   [1, 2, 16]

Summary

A recurrent network reads step by step, and carries a memory forward. It's an elegant idea. But how do we turn words into numbers that the network can read? Next, one of the most surprising ideas in deep learning.

Key takeaways
  • One step at a time
  • Hidden state is the memory
  • The same network at every step
Next up: Tokens and Embeddings — giving words meaning