Lecture 12 Basic RNN
Lecture 12 Basic RNN
Lecturer : Hongpu Liu Lecture 12-1 PyTorch Tutorial @ SLAM Research Group
Revision: DNN
𝑥1 Linear Layer
Sigmoid Layer
𝑥2 𝑧1 𝑜1
𝑥3 𝑧2 𝑜2 𝑧1 𝑜1
𝑥4 𝑧3 𝑜3 𝑧2 𝑜2
𝑧1 𝑜1 𝑦ො
𝑥5 𝑧4 𝑜4 𝑧3 𝑜3
𝑥6 𝑧5 𝑜5 𝑧4 𝑜4
𝑥7 𝑧6 𝑜6
𝑥8
Lecturer : Hongpu Liu Lecture 12-2 PyTorch Tutorial @ SLAM Research Group
Revision: CNN
𝑪𝟏 𝑺𝟏 𝑪𝟐 𝑺𝟐 𝒏𝟏 𝒏𝟐
Input Feature maps Feature maps Feature maps Feature maps Output
1 × 28 × 28 4 × 24 × 24 4 × 12 × 12 8×8×8 8×4×4
0
1
8
9
5×5 2×2 5×5 2×2 Fully Fully
Convolution Subsampling Convolution Subsampling Connected Connected
Lecturer : Hongpu Liu Lecture 12-3 PyTorch Tutorial @ SLAM Research Group
What is RNNs?
ℎ𝑡 ℎ1 ℎ2 ℎ3 ℎ4
RNN Cell ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell
𝑥𝑡 𝑥1 𝑥2 𝑥3 𝑥4
Lecturer : Hongpu Liu Lecture 12-4 PyTorch Tutorial @ SLAM Research Group
What is RNN?
RNN Cell
𝑾𝒊𝒉 𝒙𝒕 + 𝒃𝒊𝒉
𝒙𝒕 ∈ ℝ𝒊𝒏𝒑𝒖𝒕_𝒔𝒊𝒛𝒆
Lecturer : Hongpu Liu Lecture 12-5 PyTorch Tutorial @ SLAM Research Group
What is RNNs?
ℎ𝑡 ℎ1 ℎ2 ℎ3 ℎ4
RNN Cell ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell
𝑥𝑡 𝑥1 𝑥2 𝑥3 𝑥4
Lecturer : Hongpu Liu Lecture 12-6 PyTorch Tutorial @ SLAM Research Group
RNN Cell in PyTorch
RNN Cell
𝑾𝒊𝒉 𝒙𝒕 + 𝒃𝒊𝒉
𝒙𝒕 ∈ ℝ𝒊𝒏𝒑𝒖𝒕_𝒔𝒊𝒛𝒆
Lecturer : Hongpu Liu Lecture 12-7 PyTorch Tutorial @ SLAM Research Group
RNN Cell in PyTorch
RNN Cell
𝑾𝒊𝒉 𝒙𝒕 + 𝒃𝒊𝒉
𝒙𝒕 ∈ ℝ𝒊𝒏𝒑𝒖𝒕_𝒔𝒊𝒛𝒆
Lecturer : Hongpu Liu Lecture 12-8 PyTorch Tutorial @ SLAM Research Group
RNN Cell in PyTorch
input output
Lecturer : Hongpu Liu Lecture 12-9 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
Lecturer : Hongpu Liu Lecture 12-10 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
Lecturer : Hongpu Liu Lecture 12-11 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
Lecturer : Hongpu Liu Lecture 12-12 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
Lecturer : Hongpu Liu Lecture 12-13 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
Parameters
input_size = 4
hidden_size = 2
Lecturer : Hongpu Liu Lecture 12-14 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
Lecturer : Hongpu Liu Lecture 12-15 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
Lecturer : Hongpu Liu Lecture 12-16 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
Lecturer : Hongpu Liu Lecture 12-17 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
Lecturer : Hongpu Liu Lecture 12-18 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
Lecturer : Hongpu Liu Lecture 12-19 PyTorch Tutorial @ SLAM Research Group
How to use RNNCell
import torch
batch_size = 1
seq_len = 3
input_size = 4 ========== 0 ==========
hidden_size = 2 Input size: torch.Size([1, 4])
outputs size: torch.Size([1, 2])
cell = torch.nn.RNNCell(input_size=input_size, hidden_size=hidden_size) tensor([[-0.1579, 0.5140]])
========== 1 ==========
# (seq, batch, features) Input size: torch.Size([1, 4])
dataset = torch.randn(seq_len, batch_size, input_size)
outputs size: torch.Size([1, 2])
hidden = torch.zeros(batch_size, hidden_size)
tensor([[-0.9577, 0.6502]])
for idx, input in enumerate(dataset): ========== 2 ==========
print('=' * 20, idx, '=' * 20) Input size: torch.Size([1, 4])
print('Input size: ', input.shape) outputs size: torch.Size([1, 2])
tensor([[-0.7661, -0.9128]])
hidden = cell(input, hidden)
Lecturer : Hongpu Liu Lecture 12-20 PyTorch Tutorial @ SLAM Research Group
How to use RNN
ℎ1 ℎ2 ℎ3 ℎ4 ℎ𝑁
ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell ℎ𝑁
𝑥1 𝑥2 𝑥3 𝑥4 𝑥𝑁
Lecturer : Hongpu Liu Lecture 12-21 PyTorch Tutorial @ SLAM Research Group
How to use RNN
ℎ1 ℎ2 ℎ3 ℎ4 ℎ𝑁
ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell ℎ𝑁
𝑥1 𝑥2 𝑥3 𝑥4 𝑥𝑁
Lecturer : Hongpu Liu Lecture 12-22 PyTorch Tutorial @ SLAM Research Group
How to use RNN
output
Lecturer : Hongpu Liu Lecture 12-23 PyTorch Tutorial @ SLAM Research Group
How to use RNN
Lecturer : Hongpu Liu Lecture 12-24 PyTorch Tutorial @ SLAM Research Group
How to use RNN
Lecturer : Hongpu Liu Lecture 12-25 PyTorch Tutorial @ SLAM Research Group
How to use RNN
Lecturer : Hongpu Liu Lecture 12-26 PyTorch Tutorial @ SLAM Research Group
How to use RNN
Lecturer : Hongpu Liu Lecture 12-27 PyTorch Tutorial @ SLAM Research Group
How to use RNN
Lecturer : Hongpu Liu Lecture 12-28 PyTorch Tutorial @ SLAM Research Group
How to use RNN - numLayers
output ℎ1 ℎ2 ℎ3 ℎ4 ℎ𝑁
ℎ03 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell 3
ℎ𝑁
ℎ02 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell 2
ℎ𝑁
ℎ10 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell ℎ1𝑁
hidden
𝑥1 𝑥2 𝑥3 𝑥4 𝑥𝑁
Lecturer : Hongpu Liu Lecture 12-29 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4 Parameters
hidden_size = 2
num_layers = 1
Lecturer : Hongpu Liu Lecture 12-30 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Lecturer : Hongpu Liu Lecture 12-31 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Lecturer : Hongpu Liu Lecture 12-32 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Lecturer : Hongpu Liu Lecture 12-33 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Lecturer : Hongpu Liu Lecture 12-34 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Lecturer : Hongpu Liu Lecture 12-35 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Output size: torch.Size([3, 1, 2])
cell = torch.nn.RNN(input_size=input_size, hidden_size=hidden_size, Output: tensor([
num_layers=num_layers) [[ 0.2360, -0.5550]],
[[ 0.2685, 0.0106]],
# (seqLen, batchSize, inputSize) [[ 0.1172, -0.2262]]])
inputs = torch.randn(seq_len, batch_size, input_size) Hidden size: torch.Size([1, 1, 2])
hidden = torch.zeros(num_layers, batch_size, hidden_size) Hidden: tensor([[[ 0.1172, -0.2262]]])
out, hidden = cell(inputs, hidden)
Lecturer : Hongpu Liu Lecture 12-36 PyTorch Tutorial @ SLAM Research Group
How to use RNN
• batch_first: if True, the input and output tensors are provided as:
• 𝑏𝑎𝑡𝑐ℎ𝑆𝑖𝑧𝑒, 𝑠𝑒𝑞𝐿𝑒𝑛, 𝑖𝑛𝑝𝑢𝑡_𝑠𝑖𝑧𝑒
Lecturer : Hongpu Liu Lecture 12-37 PyTorch Tutorial @ SLAM Research Group
How to use RNN
import torch
batch_size = 1
seq_len = 3
input_size = 4
hidden_size = 2
num_layers = 1
Output size: torch.Size([1, 3, 2])
cell = torch.nn.RNN(input_size=input_size, hidden_size=hidden_size, Output: tensor([[
num_layers=num_layers, batch_first=True) [ 0.6582, -0.7281],
[ 0.5021, -0.8324],
# (seqLen, batchSize, inputSize) [-0.5225, -0.5621]]])
inputs = torch.randn(batch_size, seq_len, input_size) Hidden size: torch.Size([1, 1, 2])
hidden = torch.zeros(num_layers, batch_size, hidden_size) Hidden: tensor([[[-0.5225, -0.5621]]])
out, hidden = cell(inputs, hidden)
Lecturer : Hongpu Liu Lecture 12-38 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
𝑜 ℎ 𝑙 𝑜 𝑙
ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell ℎ𝑁
ℎ 𝑒 𝑙 𝑙 𝑜
Lecturer : Hongpu Liu Lecture 12-39 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
h 1 0 1 0 0
character index
e e 0 0 1 0 0 0
l h 1 2 0 0 1 0
l l 2 2 0 0 1 0
o 3
o 3 0 0 0 1
Lecturer : Hongpu Liu Lecture 12-40 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
h 1 0 1 0 0
character index
e e 0 0 1 0 0 0
l h 1 2 0 0 1 0
l l 2 2 0 0 1 0
o 3
o 3 0 0 0 1
𝒊𝒏𝒑𝒖𝒕𝑺𝒊𝒛𝒆 =?
Lecturer : Hongpu Liu Lecture 12-41 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
h 1 0 1 0 0
character index
e e 0 0 1 0 0 0
l h 1 2 0 0 1 0
l l 2 2 0 0 1 0
o 3
o 3 0 0 0 1
𝒊𝒏𝒑𝒖𝒕𝑺𝒊𝒛𝒆 = 𝟒
Lecturer : Hongpu Liu Lecture 12-42 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
𝑜 ℎ 𝑙 𝑜 𝑙
ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell ℎ𝑁
ℎ 𝑒 𝑙 𝑙 𝑜
Lecturer : Hongpu Liu Lecture 12-43 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
𝑃(𝑦 = 𝑒)
One-Hot
𝑃(𝑦 = 𝑒)
𝑥𝑡 RNN Cell Softmax NLLLoss 𝑦𝑡
𝑃(𝑦 = 𝑙)
𝑃(𝑦 = 𝑜)
ℎ𝑡−1 𝑙𝑜𝑠𝑠
𝒐𝒖𝒕𝒑𝒖𝒕𝑺𝒊𝒛𝒆 = ?
Lecturer : Hongpu Liu Lecture 12-44 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
𝑃(𝑦 = 𝑒)
One-Hot
𝑃(𝑦 = 𝑒)
𝑥𝑡 RNN Cell Softmax NLLLoss 𝑦𝑡
𝑃(𝑦 = 𝑙)
𝑃(𝑦 = 𝑜)
ℎ𝑡−1 𝑙𝑜𝑠𝑠
𝒐𝒖𝒕𝒑𝒖𝒕𝑺𝒊𝒛𝒆 = 4
Lecturer : Hongpu Liu Lecture 12-45 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Using RNNCell
CrossEntropyLoss
𝑃(𝑦 = 𝑒)
One-Hot
𝑃(𝑦 = 𝑒)
𝑥𝑡 RNN Cell Softmax NLLLoss 𝑦𝑡
𝑃(𝑦 = 𝑙)
𝑃(𝑦 = 𝑜)
ℎ𝑡−1 𝑙𝑜𝑠𝑠
𝒐𝒖𝒕𝒑𝒖𝒕𝑺𝒊𝒛𝒆 = 4
Lecturer : Hongpu Liu Lecture 12-46 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Parameters
import torch
input_size = 4
hidden_size = 4
batch_size = 1
Lecturer : Hongpu Liu Lecture 12-47 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Prepare Data
Lecturer : Hongpu Liu Lecture 12-48 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Prepare Data
Lecturer : Hongpu Liu Lecture 12-49 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Prepare Data
Lecturer : Hongpu Liu Lecture 12-50 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Prepare Data
Lecturer : Hongpu Liu Lecture 12-51 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Prepare Data
Lecturer : Hongpu Liu Lecture 12-52 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Prepare Data
Lecturer : Hongpu Liu Lecture 12-53 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Design Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size):
super(Model, self).__init
self.batch_size = batch_size
Initial the parameters
self.input_size = input_size
self.hidden_size = hidden_size
self.rnncell = torch.nn.RNNCell(input_size=self.input_size,
hidden_size=self.hidden_size)
def init_hidden(self):
return torch.zeros(self.batch_size, self.hidden_size)
Lecturer : Hongpu Liu Lecture 12-54 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Design Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size):
super(Model, self).__init__()
# self.num_layers = num_layers
self.batch_size = batch_size
self.input_size = input_size Shape of inputs :
self.hidden_size = hidden_size
self.rnncell = torch.nn.RNNCell(input_size=self.input_size, 𝒃𝒂𝒕𝒄𝒉𝑺𝒊𝒛𝒆, 𝒊𝒏𝒑𝒖𝒕𝑺𝒊𝒛𝒆
hidden_size=self.hidden_size) Shape of hidden:
def init_hidden(self):
return torch.zeros(self.batch_size, self.hidden_size)
Lecturer : Hongpu Liu Lecture 12-55 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Design Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size):
super(Model, self).__init__()
# self.num_layers = num_layers
self.batch_size = batch_size
self.input_size = input_size
self.hidden_size = hidden_size
self.rnncell = torch.nn.RNNCell(input_size=self.input_size,
hidden_size=self.hidden_size)
def init_hidden(self):
return torch.zeros(self.batch_size, self.hidden_size) Provide initial hidden
Lecturer : Hongpu Liu Lecture 12-56 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Loss and Optimizer
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.1)
Lecturer : Hongpu Liu Lecture 12-57 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Training Cycle
Lecturer : Hongpu Liu Lecture 12-58 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Training Cycle
Lecturer : Hongpu Liu Lecture 12-59 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Training Cycle
Lecturer : Hongpu Liu Lecture 12-60 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Training Cycle
Lecturer : Hongpu Liu Lecture 12-61 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Training Cycle
Lecturer : Hongpu Liu Lecture 12-62 PyTorch Tutorial @ SLAM Research Group
Example 12-1: Code – Result
Lecturer : Hongpu Liu Lecture 12-63 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Using RNN Module
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.05)
_, idx = outputs.max(dim=1)
idx = idx.data.numpy()
print('Predicted: ', ''.join([idx2char[x] for x in idx]), end='')
print(', Epoch [%d/15] loss = %.3f' % (epoch + 1, loss.item()))
Lecturer : Hongpu Liu Lecture 12-64 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Change Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size, num_layers=1):
super(Model, self).__init__()
self.num_layers = num_layers input_size = 4
self.batch_size = batch_size hidden_size = 4
self.input_size = input_size num_layers = 1
self.hidden_size = hidden_size batch_size = 1
self.rnn = torch.nn.RNN(input_size=self.input_size, seq_len = 5
hidden_size=self.hidden_size,
num_layers=num_layers)
Lecturer : Hongpu Liu Lecture 12-65 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Change Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size, num_layers=1):
super(Model, self).__init__()
self.num_layers = num_layers
self.batch_size = batch_size
self.input_size = input_size
self.hidden_size = hidden_size
self.rnn = torch.nn.RNN(input_size=self.input_size, input_size = 4
hidden_size=self.hidden_size, hidden_size = 4
num_layers=num_layers) num_layers = 1
batch_size = 1
def forward(self, input): seq_len = 5
hidden = torch.zeros(self.num_layers,
self.batch_size,
self.hidden_size)
out, _ = self.rnn(input, hidden)
return out.view(-1, self.hidden_size)
Lecturer : Hongpu Liu Lecture 12-66 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Change Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size, num_layers=1):
super(Model, self).__init__()
self.num_layers = num_layers
self.batch_size = batch_size
self.input_size = input_size
input_size = 4
self.hidden_size = hidden_size hidden_size = 4
self.rnn = torch.nn.RNN(input_size=self.input_size, num_layers = 1
hidden_size=self.hidden_size, batch_size = 1
num_layers=num_layers) seq_len = 5
def forward(self, input):
hidden = torch.zeros(self.num_layers,
Shape of hidden :
self.batch_size,
self.hidden_size) 𝒏𝒖𝒎𝑳𝒂𝒚𝒆𝒓𝒔, 𝒃𝒂𝒕𝒄𝒉𝑺𝒊𝒛𝒆, 𝒉𝒊𝒅𝒅𝒆𝒏𝑺𝒊𝒛𝒆
out, _ = self.rnn(input, hidden)
return out.view(-1, self.hidden_size)
Lecturer : Hongpu Liu Lecture 12-67 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Change Model
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size, num_layers=1):
super(Model, self).__init__()
self.num_layers = num_layers
self.batch_size = batch_size
self.input_size = input_size
self.hidden_size = hidden_size
self.rnn = torch.nn.RNN(input_size=self.input_size,
hidden_size=self.hidden_size,
num_layers=num_layers)
Lecturer : Hongpu Liu Lecture 12-68 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Change Data
input_size = 4
hidden_size = 4
idx2char = ['e', 'h', 'l', 'o'] num_layers = 1
x_data = [1, 0, 2, 2, 3] batch_size = 1
y_data = [3, 1, 2, 3, 2] seq_len = 5
Lecturer : Hongpu Liu Lecture 12-69 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Change Data
input_size = 4
hidden_size = 4
idx2char = ['e', 'h', 'l', 'o'] num_layers = 1
x_data = [1, 0, 2, 2, 3] batch_size = 1
y_data = [3, 1, 2, 3, 2] seq_len = 5
Lecturer : Hongpu Liu Lecture 12-70 PyTorch Tutorial @ SLAM Research Group
Example 12-2 Result
Lecturer : Hongpu Liu Lecture 12-71 PyTorch Tutorial @ SLAM Research Group
Associate a vector with a word/character
Lecturer : Hongpu Liu Lecture 12-72 PyTorch Tutorial @ SLAM Research Group
Associate a vector with a word/character
Lecturer : Hongpu Liu Lecture 12-73 PyTorch Tutorial @ SLAM Research Group
Associate a vector with a word/character
Lecturer : Hongpu Liu Lecture 12-74 PyTorch Tutorial @ SLAM Research Group
One-hot vs Embedding
Lecturer : Hongpu Liu Lecture 12-75 PyTorch Tutorial @ SLAM Research Group
Embedding in PyTorch
𝑒𝑚𝑏𝑒𝑑𝑑𝑖𝑛𝑔𝑆𝑖𝑧𝑒
Lecturer : Hongpu Liu Lecture 12-76 PyTorch Tutorial @ SLAM Research Group
Embedding in PyTorch
Lecturer : Hongpu Liu Lecture 12-77 PyTorch Tutorial @ SLAM Research Group
Embedding in PyTorch
Embedding Layer
Lecturer : Hongpu Liu Lecture 12-78 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
𝑠𝑒𝑞𝐿𝑒𝑛, 4 𝑜1 𝑜2 𝑜3 𝑜4 𝑜5
ℎ0 RNN Cell RNN Cell RNN Cell RNN Cell RNN Cell ℎ5
LongTensor 𝑥1 𝑥2 𝑥3 𝑥4 𝑥5
Lecturer : Hongpu Liu Lecture 12-79 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
Lecturer : Hongpu Liu Lecture 12-80 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
Lecturer : Hongpu Liu Lecture 12-81 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
Lecturer : Hongpu Liu Lecture 12-82 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__() Lookup matrix of Embedding:
self.emb = torch.nn.Embedding(input_size, embedding_size)
self.rnn = torch.nn.RNN(input_size=embedding_size, 𝒊𝒏𝒑𝒖𝒕𝑺𝒊𝒛𝒆, 𝒆𝒎𝒃𝒆𝒅𝒅𝒊𝒏𝒈𝑺𝒊𝒛𝒆
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True)
self.fc = torch.nn.Linear(hidden_size, num_class)
Lecturer : Hongpu Liu Lecture 12-83 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb = torch.nn.Embedding(input_size, embedding_size)
self.rnn = torch.nn.RNN(input_size=embedding_size,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True)
Input should be LongTensor:
self.fc = torch.nn.Linear(hidden_size, num_class)
𝒃𝒂𝒕𝒄𝒉𝑺𝒊𝒛𝒆, 𝒔𝒆𝒒𝑳𝒆𝒏
def forward(self, x):
hidden = torch.zeros(num_layers, x.size(0), hidden_size) Output with shape:
x = self.emb(x) # (batch, seqLen, embeddingSize)
x, _ = self.rnn(x, hidden)
𝒃𝒂𝒕𝒄𝒉𝑺𝒊𝒛𝒆, 𝒔𝒆𝒒𝑳𝒆𝒏, 𝒆𝒎𝒃𝒆𝒅𝒅𝒊𝒏𝒈𝑺𝒊𝒛𝒆
x = self.fc(x) Notice: batch FIRST
return x.view(-1, num_class)
Lecturer : Hongpu Liu Lecture 12-84 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb = torch.nn.Embedding(input_size, embedding_size) Input of RNN:
self.rnn = torch.nn.RNN(input_size=embedding_size,
hidden_size=hidden_size, 𝒃𝒂𝒕𝒄𝒉𝑺𝒊𝒛𝒆, 𝒔𝒆𝒒𝑳𝒆𝒏, 𝒆𝒎𝒃𝒆𝒅𝒅𝒊𝒏𝒈𝑺𝒊𝒛𝒆
num_layers=num_layers,
batch_first=True) Output of RNN:
self.fc = torch.nn.Linear(hidden_size, num_class)
𝒃𝒂𝒕𝒄𝒉𝑺𝒊𝒛𝒆, 𝒔𝒆𝒒𝑳𝒆𝒏, 𝒉𝒊𝒅𝒅𝒆𝒏𝑺𝒊𝒛𝒆
def forward(self, x):
hidden = torch.zeros(num_layers, x.size(0), hidden_size)
x = self.emb(x) # (batch, seqLen, embeddingSize)
x, _ = self.rnn(x, hidden)
x = self.fc(x)
return x.view(-1, num_class)
Lecturer : Hongpu Liu Lecture 12-85 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb = torch.nn.Embedding(input_size, embedding_size)
self.rnn = torch.nn.RNN(input_size=embedding_size,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True)
self.fc = torch.nn.Linear(hidden_size, num_class) Input of FC Layer:
Lecturer : Hongpu Liu Lecture 12-86 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb = torch.nn.Embedding(input_size, embedding_size)
self.rnn = torch.nn.RNN(input_size=embedding_size,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True)
self.fc = torch.nn.Linear(hidden_size, num_class)
Lecturer : Hongpu Liu Lecture 12-87 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
# parameters
num_class = 4
input_size = 4
hidden_size = 8
embedding_size = 10
num_layers = 2
batch_size = 1
seq_len = 5
Lecturer : Hongpu Liu Lecture 12-88 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
Lecturer : Hongpu Liu Lecture 12-89 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
net = Model()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=0.05)
Lecturer : Hongpu Liu Lecture 12-90 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
_, idx = outputs.max(dim=1)
idx = idx.data.numpy()
print('Predicted: ', ''.join([idx2char[x] for x in idx]), end='')
print(', Epoch [%d/15] loss = %.3f' % (epoch + 1, loss.item()))
Lecturer : Hongpu Liu Lecture 12-91 PyTorch Tutorial @ SLAM Research Group
Example 12-3 Using embedding and linear layer
Lecturer : Hongpu Liu Lecture 12-92 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 1 Using LSTM
Lecturer : Hongpu Liu Lecture 12-93 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 1 Using LSTM
https://pytorch.org/docs/stable/nn.html#lstm
Lecturer : Hongpu Liu Lecture 12-94 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 1 Using LSTM
𝒄𝒕+𝟏
𝒉𝒕+𝟏
Lecturer : Hongpu Liu Lecture 12-95 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 1 Using LSTM
https://pytorch.org/docs/stable/nn.html#lstm
Lecturer : Hongpu Liu Lecture 12-96 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 2 Using GRU
Lecturer : Hongpu Liu Lecture 12-97 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 2 Using GRU
https://pytorch.org/docs/stable/nn.html#gru
Lecturer : Hongpu Liu Lecture 12-98 PyTorch Tutorial @ SLAM Research Group
Exercise 12 – 2 Using GRU
https://pytorch.org/docs/stable/nn.html#gru
Lecturer : Hongpu Liu Lecture 12-99 PyTorch Tutorial @ SLAM Research Group
PyTorch Tutorial
12. Basic RNN
Lecturer : Hongpu Liu Lecture 12-100 PyTorch Tutorial @ SLAM Research Group