《PyTorch深度学习实践》13. 循环神经网络(高级篇)

循环神经网络(高级篇)

例:人名分类

数据准备

根据人名的英文拼写判断人所在的国家,数据形式如下:

image-20200927161615676

这个问题是序列输入,而只有一个输出,若采用RNN,模型形式如下:

image-20200927161458425

采用GRU的话,模型形式如下:

image-20200927161924466

数据处理:

名字序列转ASCII码值,进一步表示成one-hot

image-20200927200005921

对ASCII码表示的输入做padding,统一长度好形成一个张量

image-20200927200047083

国家表示成数字label

image-20200927200209709

模型与代码

双向神经网络图:

image-20200927214633630

注:$hidden=[h_N^f,h_N^b]$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class NameDataset(Dataset):
def __init__(self, is_train_set=True):
filename = 'data/names_train.csv.gz' if is_train_set else 'data/names_test.csv.gz'
with gzip.open(filename, 'rt') as f:
reader = csv.reader(f)
rows = list(reader)
self.names = [row[0] for row in rows]
self.len = len(self.names)
self.countries = [row[1] for row in rows]
self.country_list = list(sorted(set(self.countries)))
self.country_dict = self.getCountryDict()
self.country_num = len(self.country_list)

def __getitem__(self, index):
return self.names[index], self.country_dict[self.countries[index]]

def __len__(self):
return self.len

def getCountryDict(self):
country_dict = dict()
for idx, country_name in enumerate(self.country_list, 0):
country_dict[country_name] = idx
return country_dict

def idx2country(self, index):
return self.country_list[index]

def getCountriesNum(self):
return self.country_nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class RNNClassifier(torch.nn.Module):
def __init__(self, input_size, hidden_size, output_size, n_layers=1, bidirectional=True):
super(RNNClassifier, self).__init__()
self.hidden_size = hidden_size
self.n_layers = n_layers
self.n_directions = 2 if bidirectional else 1

# The input of Embedding Layer with shape: (seqLen, batchSize)
# The ouput of Embedding Layer with shape: (seqLen, batchSize, hiddenSize)
self.embedding = torch.nn.Embedding(input_size, hidden_size)
self.gru = torch.nn.GRU(hidden_size, hidden_size, n_layers,
bidirectional=bidirectional)
self.fc = torch.nn.Linear(hidden_size * self.n_directions, output_size)

def _init_hidden(self, batch_size):
hidden = torch.zeros(self.n_layers * self.n_directions,
batch_size, self.hidden_size)
return create_tensor(hidden)

def forward(self, input, seq_lengths):
# input shape : B x S -> S x B
input = input.t()
batch_size = input.size(1)

hidden = self._init_hidden(batch_size)
embedding = self.embedding(input)

# pack them up
gru_input = pack_padded_sequence(embedding, seq_lengths) # 压缩有padding的embedding,提高运算效率

output, hidden = self.gru(gru_input, hidden)
if self.n_directions == 2:
hidden_cat = torch.cat([hidden[-1], hidden[-2]], dim=1)
else:
hidden_cat = hidden[-1]
fc_output = self.fc(hidden_cat)
return fc_output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def trainModel():
total_loss = 0
for i, (names, countries) in enumerate(trainloader, 1):
inputs, seq_lengths, target = make_tensors(names, countries)
output = classifier(inputs, seq_lengths)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()

total_loss += loss.item()
if i % 10 == 0:
print(f'[{time_since(start)}]Epoch {epoch}', end='')
print(f'[{i * len(inputs)}/{len(trainset)}]', end='')
print(f'loss={total_loss / (i * len(inputs))}')
return total_loss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def testModel():
correct = 0
total = len(testset)
print("evaluating trained model ...")
with torch.no_grad():
for i, (names, countries) in enumerate(testloader, 1):
inputs, seq_lengths, target = make_tensors(names, countries)
output = classifier(inputs, seq_lengths)
pred = output.max(dim=1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()

percent = '%.2f' % (100 * correct / total)
print(f'Test set: Asccuracy {correct}/{total} {percent}%')

return correct / total

在Colab上运行

课程来源:《PyTorch深度学习实践》完结合集