翻译自官网手册:NLP From Scratch: Classifying Names with a Character-Level RNN
Author: Sean Robertson
原文github
本文ipynb下载
本文将建立和训练一个基础的字级RNN对单词进行分类。本教程及随后两个教程,展示了如何一步步为自然语言处理(NLP)模型处理数据,尤其是不使用’torchtext’中的很多分词方便的函数,这样可以看到如何在比较底层为NLP模型预处理数据。
字级RNN将单词作为字符的序列进行读取,每一步输出预测和隐藏状态(hidden state),并将隐藏状态传递给下一步。使用最后一个预测作为输出,如本例,单词属于哪一类。
这里,本教程将在来自18种语言的几千个名字上进行训练,并基于拼写预测一个姓来自于哪种语言,如:
$ python predict.py Hinton
(-0.47) Scottish
(-1.52) English
(-3.57) Irish
$ python predict.py Schmidhuber
(-0.19) German
(-2.48) Czech
(-2.68) Dutch
推荐阅读
本教程需要你至少安装了PyTorch、了解Python、了解张量(Tensors):
- https://pytorch.org/ 安装说明
- Deep Learning with PyTorch: A 60 Minute Blitz 初识PyTorch
- Learning PyTorch with Examples 广泛深入了解
- PyTorch for Former Torch Users 之前使用过Lua Torch
了解RNNs及其原理可以阅读:
- 循环神经网络Recurrent Neural
Networks()不可思议的效果展示了一些真实的样本。
shows a bunch of real life examples - 理解LSTM网络是关于LSTMs的,也包含了RNNs的说明。
准备数据(Preparing the Data)
注意: 数据从这个地址下载,并将其解压到当前目录
在data/names
目录下包含18个命名为“[语言].txt”的文本文件。每个文件包括一系列名字,每行一个,大部分是罗马字符(需要将其从Unicode转化为ASCII符号)。
我们将创建每种语言名字的列表组成的词典{language: [names ...]}
,普通变量"category" 、 “line”(本列中是语言和名字)将被用于后续处理。
获取数据文件列表
from __future__ import unicode_literals, print_function, division
from io import open
import glob
import os
#返回文件夹下文件列表
def findFiles(path): return glob.glob(path)
print(findFiles('data/names/*.txt'))
['data/names/Italian.txt', 'data/names/Dutch.txt', 'data/names/German.txt', 'data/names/Arabic.txt', 'data/names/Czech.txt', 'data/names/Greek.txt', 'data/names/Vietnamese.txt', 'data/names/French.txt', 'data/names/Spanish.txt', 'data/names/English.txt', 'data/names/Portuguese.txt', 'data/names/Scottish.txt', 'data/names/Chinese.txt', 'data/names/Irish.txt', 'data/names/Russian.txt', 'data/names/Japanese.txt', 'data/names/Korean.txt', 'data/names/Polish.txt']
字符串格式转换
import unicodedata
import string
all_letters = string.ascii_letters + " .,;'"
n_letters = len(all_letters)
# 将Unicode字符串转换为简单的ASCII字符,鸣谢 https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):
return ''.join(
c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'
and c in all_letters
)
print(unicodeToAscii('Ślusàrski'))
Slusarski
创建分类字典
#创建分类字典(category_lines), 每种语言对应一个名字列表
category_lines = {}
all_categories = []
# 读取一个文件并按行分割
def readLines(filename):
lines = open(filename, encoding='utf-8').read().strip().split('\n')
return [unicodeToAscii(line) for line in lines]
for filename in findFiles('data/names/*.txt'):
category = os.path.splitext(os.path.basename(filename))[0]
all_categories.append(category)
lines = readLines(filename)
category_lines[category] = lines
n_categories = len(all_categories)
现在我们得到category_lines
,一个将类别(语言)映射到行(名字)列表的字典。我们也记录了all_categories
(语言列表)和n_categories
(类别数量),后面将要使用。
print(category_lines['Italian'][:5])
['Abandonato', 'Abatangelo', 'Abatantuono', 'Abate', 'Abategiovanni']
将名字转化问张量(Turning Names into Tensors)
现在我们得到所有的名字,接下来需要把他们转化为张量(Tensors)。
我们使用维度为<1 x n_letters>
的独热向量(one-hot vector)表示一个字母。一个独热向量除了当前字母的索引填充为1外,其余位置全为0,如,"b" = <0 1 0 0 0 ...>
。
这样就可以使用一组字母对应的独热向量组成二维矩阵来表示一个单词<line_length x 1 x n_letters>
,多出来的一个维度是因为PyTorch默认所有的变量在批次(batches)中,我们这里使用的批次大小为1。
import torch
# 在查找字母索引,如:"a" = 0
def letterToIndex(letter):
return all_letters.find(letter)
# 仅用于演示,将一个字母转换为 <1 x n_letters> 大小的张量
def letterToTensor(letter):
tensor = torch.zeros(1, n_letters)
tensor[0][letterToIndex(letter)] = 1
return tensor
# 将一行数据转换为 <line_length x 1 x n_letters>
# 独热编码的字符向量组成的数组
def lineToTensor(line):
tensor = torch.zeros(len(line), 1, n_letters)
for li, letter in enumerate(line):
tensor[li][0][letterToIndex(letter)] = 1
return tensor
print(letterToTensor('J'))
print(lineToTensor('Jones').size())
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0.]])
torch.Size([5, 1, 57])
创建网络(Creating the Network)
实现自动求导(autograd)之前,在Torch中创建循环神经网络需要复制结果时间步之前每层的参数。这些层处理的隐藏状态和梯度,现在由计算图自行处理。这意味着你可以像实现前馈层(feed-forward layers)一样,很简洁的实现一个RNN。
本教程的RNN模块(主要参考the PyTorch for Torch users tutorial)只有处理输入和隐藏状态的两个线性层(linear layers),最后增加了LogSoftmax层作为最终输出。
import torch.nn as nn
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
self.i2o = nn.Linear(input_size + hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden):
combined = torch.cat((input, hidden), 1)
hidden = self.i2h(combined)
output = self.i2o(combined)
output = self.softmax(output)
return output, hidden
def initHidden(self):
return torch.zeros(1, self.hidden_size)
n_hidden = 128
rnn = RNN(n_letters, n_hidden, n_categories)
运行这个网络的一步需要传递一个输入(本例中是当前字母的张量)和上一步的隐藏状态(初始值为全0)。然后得到输出(名字是每一种语言的概率)和下一个隐藏状态(下一步中使用)。
input = letterToTensor('A')
hidden =torch.zeros(1, n_hidden)
output, next_hidden = rnn(input, hidden)
为了提高效益,不能每一步都创建新的张量,因此使用行张量lineToTensor
代替字符张量letterToTensor
并使用切片方法。此外,可以通过预计算批次张量进一步提高效率。
input = lineToTensor('Albert')
hidden = torch.zeros(1, n_hidden)
output, next_hidden = rnn(input[0], hidden)
print(output)
tensor([[-2.7939, -2.7677, -2.8841, -3.0140, -2.9501, -2.9271, -2.8262, -2.9668,
-2.8892, -2.8022, -2.9095, -2.9637, -2.8294, -2.9684, -2.7892, -2.9825,
-2.9025, -2.9092]], grad_fn=<LogSoftmaxBackward>)
可以看到输出是<1 x n_categories>
大小的张量,每一项代表名字是对应类别的可能性(值越大可能性越大)。
训练(Training)
训练前准备(Preparing for Training)
开始训练前先创建几个辅助函数。
输出解析函数
第一个是当网络输出了每一类的可能性后,对输出进行解析。可以使用Tensor.topk
获得最大几个值对应的索引。
def categoryFromOutput(output):
top_n, top_i = output.topk(1)
category_i = top_i[0].item()
return all_categories[category_i], category_i
print(categoryFromOutput(output))
('Dutch', 1)
获取训练样本函数
也需要便捷的方法获取一个训练样本(一个名字及它来自的语言)
import random
# 生成随机索引
def randomChoice(l):
return l[random.randint(0, len(l) - 1)]
# 随机获取一个样本,输出依次为类别、名字、类别张量、名字张量
def randomTrainingExample():
category = randomChoice(all_categories)
line = randomChoice(category_lines[category])
category_tensor = torch.tensor([all_categories.index(category)], dtype=torch.long)
line_tensor = lineToTensor(line)
return category, line, category_tensor, line_tensor
for i in range(10):
category, line, category_tensor, line_tensor = randomTrainingExample()
print('category =', category, '/ line =', line)
category = Polish / line = Adamczak
category = Polish / line = Kowalski
category = Irish / line = Doherty
category = Polish / line = Sokolsky
category = Spanish / line = Narvaez
category = German / line = Bayer
category = French / line = Chastain
category = Czech / line = Marik
category = Russian / line = Avseenko
category = Italian / line = Salucci
训练网络(Training the Network)
训练网络需要做的是传递给它一系列样本,让网络预测类别,然后告诉网络预测的对不对。
由于RNN的最后一层是nn.LogSoftmax
,所以损失函数使用nn.NLLLoss
比较合适。
criterion = nn.NLLLoss()
训练的每个循环包括以下几步:
- 创建输入、输出张量
- 创建置零的初始状态
- 读取每个单词并
- 为下个单词保留隐藏状态
- 比较最终输出与目标
- 后向传播(Back-propagate)
- 返回输出和损失
learning_rate = 0.005 # If you set this too high, it might explode. If too low, it might not learn
def train(category_tensor, line_tensor):
hidden = rnn.initHidden()
rnn.zero_grad()
for i in range(line_tensor.size()[0]):
output, hidden = rnn(line_tensor[i], hidden)
loss = criterion(output, category_tensor)
loss.backward()
# Add parameters' gradients to their values, multiplied by learning rate
for p in rnn.parameters():
p.data.add_(-learning_rate, p.grad.data)
return output, loss.item()
现在用一写样本运行训练函数。由于训练函数train
返回输出和损失,我们可以输出它的预测值以及记录损失并绘制图像。由于样本有几千个,我们只输出每个print_every
样本并取损失的平均值。
import time
import math
n_iters = 100000
print_every = 5000
plot_every = 1000
# 记录损失用于绘图
current_loss = 0
all_losses = []
def timeSince(since):
now = time.time()
s = now - since
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
start = time.time()
for iter in range(1, n_iters + 1):
category, line, category_tensor, line_tensor = randomTrainingExample()
output, loss = train(category_tensor, line_tensor)
current_loss += loss
# 输出迭代编号、损失、姓名、预测值
if iter % print_every == 0:
guess, guess_i = categoryFromOutput(output)
correct = '✓' if guess == category else '✗ (%s)' % category
print('%d %d%% (%s) %.4f %s / %s %s' % (iter, iter / n_iters * 100, timeSince(start), loss, line, guess, correct))
# 将当前损失的平均值添加到损失列表
if iter % plot_every == 0:
all_losses.append(current_loss / plot_every)
current_loss = 0
5000 5% (0m 12s) 2.5393 Akita / Japanese ✓
10000 10% (0m 25s) 3.2100 Purdes / Portuguese ✗ (Czech)
15000 15% (0m 37s) 0.2392 Batsakis / Greek ✓
20000 20% (0m 50s) 1.1636 Vu / Vietnamese ✓
25000 25% (1m 3s) 1.5789 Shalhoub / Japanese ✗ (Arabic)
30000 30% (1m 16s) 1.4143 Gomez / Spanish ✓
35000 35% (1m 29s) 0.0491 Vamvakidis / Greek ✓
40000 40% (1m 42s) 1.1951 Riagan / Irish ✓
45000 45% (1m 54s) 1.3614 Mendes / Portuguese ✓
50000 50% (2m 7s) 2.8686 Adam / Arabic ✗ (Irish)
55000 55% (2m 19s) 0.6292 Eng / Chinese ✓
60000 60% (2m 32s) 2.2846 Kava / Czech ✗ (Polish)
65000 65% (2m 44s) 0.2395 Dinh / Vietnamese ✓
70000 70% (2m 57s) 1.5488 Kassis / Greek ✗ (Arabic)
75000 75% (3m 9s) 0.2410 Ly / Vietnamese ✓
80000 80% (3m 22s) 0.5356 Yu / Korean ✓
85000 85% (3m 34s) 0.8349 Saliba / Arabic ✓
90000 90% (3m 46s) 1.3427 Zuraw / Polish ✓
95000 95% (3m 59s) 0.2652 Vesninov / Russian ✓
100000 100% (4m 12s) 0.6455 Michalovicova / Czech ✓
绘制结果(Plotting the Results)
绘制来自all_losses
的历史损失,展示网络学习过程:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
plt.figure()
plt.plot(all_losses)
输出图片如下:
验证结果(Evaluating the Results)
为了查看网络在不同分类上的效果,我们创建了混淆矩阵,展示了对于每一个实际语言(rows)网络预测了哪一种语言(columns)。为了计算混淆矩阵,使用了网络的evaluate()
(相当于train()
函数去掉后向传播)函数预测了一些样本。
# 在混淆矩阵中记录正确的预测
confusion = torch.zeros(n_categories, n_categories)
n_confusion = 10000
# 输入名字返回预测结果
def evaluate(line_tensor):
hidden = rnn.initHidden()
for i in range(line_tensor.size()[0]):
output, hidden = rnn(line_tensor[i], hidden)
return output
# 输入一些样本并记录哪些预测正确
for i in range(n_confusion):
category, line, category_tensor, line_tensor = randomTrainingExample()
output = evaluate(line_tensor)
guess, guess_i = categoryFromOutput(output)
category_i = all_categories.index(category)
confusion[category_i][guess_i] += 1
# 通过每行的总数进行标准化
for i in range(n_categories):
confusion[i] = confusion[i] / confusion[i].sum()
# 设置绘图参数
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(confusion.numpy())
fig.colorbar(cax)
# 设置坐标轴
ax.set_xticklabels([''] + all_categories, rotation=90)
ax.set_yticklabels([''] + all_categories)
# 为每一刻度设置标记
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
# sphinx_gallery_thumbnail_number = 2
plt.show()
输出图片如下:
由图可以看到,图像对角线主轴之外的高亮方格代表预测错误的语言,比如,中文(Chinese)和韩文(Korean)、西班牙语(Spanish)和意大利语(Italian)。模型在希腊语(Greek)上表现比较好,在英语(English)上表现很差(可能是由于其与其它语言重复率比较高)。
预测用户输入(Running on User Input)
def predict(input_line, n_predictions=3):
print('\n> %s' % input_line)
with torch.no_grad():
output = evaluate(lineToTensor(input_line))
# 获取概率最大的N个类别
topv, topi = output.topk(n_predictions, 1, True)
predictions = []
for i in range(n_predictions):
value = topv[0][i].item()
category_index = topi[0][i].item()
print('(%.2f) %s' % (value, all_categories[category_index]))
predictions.append([value, all_categories[category_index]])
predict('Dovesky')
predict('Jackson')
predict('Satoshi')
> Dovesky
(-0.80) Russian
(-0.99) Czech
(-2.48) English
> Jackson
(-0.03) Scottish
(-3.99) English
(-5.48) Russian
> Satoshi
(-1.06) Arabic
(-1.06) Japanese
(-2.14) Italian
最终版的代码在PyTorch实践库Practical PyTorch
repo,将以上代码分割到了几个文件:
split the above code into a few files:
data.py
(读取文件)model.py
(定义 RNN)train.py
(进行训练)predict.py
(根据命令行参数运行predict()
)server.py
(使用bottle.py
将预测包装为JSON API)
运行 train.py
训练并保存网络。
运行 predict.py
输入名字并查看预测结果:
$ python predict.py Hazaki
(-0.42) Japanese
(-1.39) Polish
(-3.51) Czech
运行server.py
并访问 http://localhost:5533/Yourname 获取预测值的JSON输出。
练习题(Exercises)
- 使用其它分类(line->category)数据集进行训练,如:
- Any word -> language(任何单词->语言)
- First name -> gender(名字->性别)
- Character name -> writer(角色名称->作者)
- Page title -> blog or subreddit(文章标题->博客还是社交新闻)
- 使用规模更大或者设计更好的模型优化结果:
- 增加更多线性层
- 尝试
nn.LSTM
和nn.GRU
层 - 将多个RNN层组合为更高级的网络