Skip to content

Commit 2e44fb6

Browse files
committed
Updated slides
1 parent 4ff9227 commit 2e44fb6

17 files changed

+102
-8
lines changed

08_2_dataset_loade_logistic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class DiabetesDataset(Dataset):
1212

1313
# Initialize your data, download, etc.
1414
def __init__(self):
15-
xy = np.loadtxt('./data/diabetes.csv.gz', delimiter=',', dtype=np.float32)
15+
xy = np.loadtxt('./data/diabetes.csv.gz',
16+
delimiter=',', dtype=np.float32)
1617
self.len = xy.shape[0]
1718
self.x_data = torch.from_numpy(xy[:, 0:-1])
1819
self.y_data = torch.from_numpy(xy[:, [-1]])

12_4_hello_rnn_emb.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Model(nn.Module):
3030
def __init__(self):
3131
super(Model, self).__init__()
3232
self.embedding = nn.Embedding(input_size, embedding_size)
33-
self.rnn = nn.RNN(input_size=embedding_size, hidden_size=5, batch_first=True)
33+
self.rnn = nn.RNN(input_size=embedding_size,
34+
hidden_size=5, batch_first=True)
3435
self.fc = nn.Linear(hidden_size, num_classes)
3536

3637
def forward(self, x):

13_1_rnn_classification_basics.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Original code is from https://github.com/spro/practical-pytorch
2+
import time
3+
import math
4+
import torch
5+
import torch.nn as nn
6+
from torch.autograd import Variable
7+
from torch.utils.data import DataLoader
8+
9+
from name_dataset import NameDataset
10+
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
11+
12+
# Parameters and DataLoaders
13+
HIDDEN_SIZE = 100
14+
N_CHARS = 128 # ASCII
15+
N_CLASSES = 18
16+
17+
18+
class RNNClassifier(nn.Module):
19+
20+
def __init__(self, input_size, hidden_size, output_size, n_layers=1):
21+
super(RNNClassifier, self).__init__()
22+
self.hidden_size = hidden_size
23+
self.n_layers = n_layers
24+
25+
self.embedding = nn.Embedding(input_size, hidden_size)
26+
self.gru = nn.GRU(hidden_size, hidden_size, n_layers)
27+
self.fc = nn.Linear(hidden_size, output_size)
28+
29+
def forward(self, input):
30+
# Note: we run this all at once (over the whole input sequence)
31+
32+
# input = B x S . size(0) = B
33+
batch_size = input.size(0)
34+
35+
# input: B x S -- (transpose) --> S x B
36+
input = input.t()
37+
38+
# Embedding S x B -> S x B x I (embedding size)
39+
print(" input", input.size())
40+
embedded = self.embedding(input)
41+
print(" embedding", embedded.size())
42+
43+
# Make a hidden
44+
hidden = self._init_hidden(batch_size)
45+
46+
output, hidden = self.gru(embedded, hidden)
47+
print(" gru hidden output", hidden.size())
48+
# Use the last layer output as FC's input
49+
# No need to unpack, since we are going to use hidden
50+
fc_output = self.fc(hidden)
51+
print(" fc output", fc_output.size())
52+
return fc_output
53+
54+
def _init_hidden(self, batch_size):
55+
hidden = torch.zeros(self.n_layers, batch_size, self.hidden_size)
56+
return Variable(hidden)
57+
58+
# Help functions
59+
60+
61+
def str2ascii_arr(msg):
62+
arr = [ord(c) for c in msg]
63+
return arr, len(arr)
64+
65+
# pad sequences and sort the tensor
66+
def pad_sequences(vectorized_seqs, seq_lengths):
67+
seq_tensor = torch.zeros((len(vectorized_seqs), seq_lengths.max())).long()
68+
for idx, (seq, seq_len) in enumerate(zip(vectorized_seqs, seq_lengths)):
69+
seq_tensor[idx, :seq_len] = torch.LongTensor(seq)
70+
return seq_tensor
71+
72+
# Create necessary variables, lengths, and target
73+
def make_variables(names):
74+
sequence_and_length = [str2ascii_arr(name) for name in names]
75+
vectorized_seqs = [sl[0] for sl in sequence_and_length]
76+
seq_lengths = torch.LongTensor([sl[1] for sl in sequence_and_length])
77+
return pad_sequences(vectorized_seqs, seq_lengths)
78+
79+
80+
if __name__ == '__main__':
81+
names = ['adylov', 'solan', 'hard', 'san']
82+
classifier = RNNClassifier(N_CHARS, HIDDEN_SIZE, N_CLASSES)
83+
84+
for name in names:
85+
arr, _ = str2ascii_arr(name)
86+
inp = Variable(torch.LongTensor([arr]))
87+
out = classifier(inp)
88+
print("in", inp.size(), "out", out.size())
89+
90+
91+
inputs = make_variables(names)
92+
out = classifier(inputs)
93+
print("batch in", inputs.size(), "batch out", out.size())
94+
95+

13_1_rnn_classification.py 13_2_rnn_classification.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
batch_size=BATCH_SIZE, shuffle=True)
2626

2727
N_COUNTRIES = len(train_dataset.get_countries())
28+
print(N_COUNTRIES, "countries")
2829
N_CHARS = 128 # ASCII
2930

3031

@@ -90,9 +91,7 @@ class RNNClassifier(nn.Module):
9091

9192
def __init__(self, input_size, hidden_size, output_size, n_layers=1, bidirectional=True):
9293
super(RNNClassifier, self).__init__()
93-
self.input_size = input_size
9494
self.hidden_size = hidden_size
95-
self.output_size = output_size
9695
self.n_layers = n_layers
9796
self.n_directions = int(bidirectional) + 1
9897

13_2_char_rnn.py 13_3_char_rnn.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def train_teacher_forching(line):
103103

104104
return loss.data[0] / len(input)
105105

106+
106107
def train(line):
107108
input = str2tensor(line[:-1])
108109
target = str2tensor(line[1:])
@@ -144,5 +145,3 @@ def train(line):
144145
print('[(%d %d%%) loss: %.4f]' %
145146
(epoch, epoch / n_epochs * 100, loss))
146147
print(generate(decoder, 'Wh', 100), '\n')
147-
148-

14_3_pack_pad.py 13_4_pack_pad.py

File renamed without changes.

14_2_seq2seq_att.py

-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ def translate(enc_input='thisissungkim.iloveyou.', predict_len=100, temperature=
120120
optimizer = torch.optim.Adam(params, lr=0.001)
121121
criterion = nn.CrossEntropyLoss()
122122

123-
124123
train_loader = DataLoader(dataset=TextDataset(),
125124
batch_size=BATCH_SIZE,
126125
shuffle=True,

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PyTorchZeroToAll
22
Quick 3~4 day lecture materials for HKUST students.
33

4-
## Video Lectures: RNN TBA]
4+
## Video Lectures: (RNN TBA)
55
* [Youtube](http://bit.ly/PyTorchVideo)
66
* [Bilibili](https://www.bilibili.com/video/av15823922/)
77

slides/Lecture 01: Overview.pdf

0 Bytes
Binary file not shown.

slides/Lecture 02: Linear Model.pdf

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

slides/Lecture 07: Wide & Deep.pdf

0 Bytes
Binary file not shown.

slides/Lecture 08: DataLoader.pdf

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)