编码器是一个RNN,因为任务是机器翻译,所以是双向的
解码器用另外一个RNN输出
之前看过Transformer对这个很熟悉
需要注意的一点是,在做训练
和做推理
的时候是有区别的
在训练时,我们是有正确的翻译的,所以解码器的每次输入都是正确的翻译
而在推理时,智能用我们预测的词当作解码器RNN下一个时间步的输入
那么现在我实在预测一个句子序列,而不是和之前一样预测一个词了,那么怎么衡量一个句子序列的好坏呢
比如 p 2 p_2 p2,预测序列一共有AB,BB,BC,CD四种,在标签序列中出现过的只有AB,BC,CD,所以 p 2 p_2 p2=3/4
因为p是小于1的值,长的匹配有更高的权重并且预测越短,取exp以后就越小,产生惩罚效果
import collections
import math
import torch
from torch import nn
from d2l import torch as d2l
#@save
class Seq2SeqEncoder(d2l.Encoder):
"""用于序列到序列学习的循环神经网络编码器"""
def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,
dropout=0, **kwargs):
super(Seq2SeqEncoder, self).__init__(**kwargs)
# 嵌入层
self.embedding = nn.Embedding(vocab_size, embed_size)
self.rnn = nn.GRU(embed_size, num_hiddens, num_layers,
dropout=dropout)
def forward(self, X, *args):
# 输出'X'的形状:(batch_size,num_steps,embed_size)
X = self.embedding(X)
# 在循环神经网络模型中,第一个轴对应于时间步
X = X.permute(1, 0, 2)
# 如果未提及状态,则默认为0
output, state = self.rnn(X)
# output的形状:(num_steps,batch_size,num_hiddens)
# state[0]的形状:(num_layers,batch_size,num_hiddens)
return output, state
embedding词嵌入,把one-hot编码映射到词嵌入矩阵,博客没写过,自己知道就行,词嵌入矩阵可以理解为一个词表
测试一下
encoder = Seq2SeqEncoder(vocab_size=10, embed_size=8, num_hiddens=16,num_layers=2)
encoder.eval()
X = torch.zeros((4, 7), dtype=torch.long)
output, state = encoder(X)
output.shape
查看output和state的大小
torch.Size([7, 4, 16])
(时间步数,批量大小,隐藏单元数)
state.shape
torch.Size([2, 4, 16])
(隐藏层的数量,批量大小,隐藏单元数)
output是RNN模型输出的,编码器是没有最后Linear输出层的,所以大小 =(隐藏层的数量,批量大小,隐藏单元数)
state,双隐藏层的编码器,本来隐藏状态的大小 = (批量大小,隐藏单元数)
class Seq2SeqDecoder(d2l.Decoder):
"""用于序列到序列学习的循环神经网络解码器"""
def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,
dropout=0, **kwargs):
super(Seq2SeqDecoder, self).__init__(**kwargs)
self.embedding = nn.Embedding(vocab_size, embed_size)
self.rnn = nn.GRU(embed_size + num_hiddens, num_hiddens, num_layers,
dropout=dropout)
self.dense = nn.Linear(num_hiddens, vocab_size)
def init_state(self, enc_outputs, *args):
return enc_outputs[1]
def forward(self, X, state):
# 输出'X'的形状:(batch_size,num_steps,embed_size)
# permute(1, 0, 2)把'num_steps'和'num_steps'换一下
X = self.embedding(X).permute(1, 0, 2)
# 广播context,使其具有与X相同的num_steps
context = state[-1].repeat(X.shape[0], 1, 1)
X_and_context = torch.cat((X, context), 2)
output, state = self.rnn(X_and_context, state)
output = self.dense(output).permute(1, 0, 2)
# output的形状:(batch_size,num_steps,vocab_size)
# state[0]的形状:(num_layers,batch_size,num_hiddens)
return output, state
self.dense = nn.Linear(num_hiddens, vocab_size)
解码器就有输出层了
self.rnn = nn.GRU(
embed_size + num_hiddens
, num_hiddens, num_layers, dropout=dropout)这个一会解释
def init_state(self, enc_outputs, *args):
拿到编码器的输出:
enc_outputs[1]
拿到编码器的state
context = state[-1].repeat(X.shape[0], 1, 1)
上下文,state[-1]拿到RNN输出的最后一层的最后一个隐藏状态,可以说是浓缩了整个输入的精华
看一下X,context,X_and_context的维度
起初我们输入的X是(4, 7) (批量大小,时间步)
经过
embedding(X)
变成(4,7,8),经过permute(1, 0, 2)
变成(7,4,8)(时间步,批量大小,每个词的维度)
state[-1]
(1,4,16)
context = state[-1].repeat(X.shape[0], 1, 1)
复制成和X一样维度的大小(7,4,16)
X_and_context
(7,4,24)这也解释了上面rnn的参数(
embed_size + num_hiddens
)是隐藏状态信息H和解码器自己输入X拼接的也就是说,解码器有两个地方用到了编码器的state
初始化state是编码器输出的state
解码器的输入是编码器输出的state的信息和自己输入的拼接
(如果没有第二点的话,解码器编码器架构也就相当于两个RNN拼接起来成一个RNN)
测试一下
decoder = Seq2SeqDecoder(vocab_size=10, embed_size=8, num_hiddens=16,num_layers=2)
decoder.eval()
state = decoder.init_state(encoder(X))
output, state = decoder(X, state)
output.shape, state.shape
查看output和state的大小
(torch.Size([4, 7, 10]), torch.Size([2, 4, 16]))
这里变回了(4, 7, 10)是因为解码器
output = self.dense(output).permute(1, 0, 2)
把批量大小和时间步换回来了另外注意最后输出的词的维度 = 10(vocab_size)即词表里所有词的可能性
还记得我们上一节提到的,valid_length么,假设我们裁剪的一个句子长度(时间步)为10,那些长度不够10而加进来的填充项就系要屏蔽掉
#@save
def sequence_mask(X, valid_len, value=0):
"""在序列中屏蔽不相关的项"""
maxlen = X.size(1)
mask = torch.arange((maxlen), dtype=torch.float32,
device=X.device)[None, :] < valid_len[:, None]
X[~mask] = value
return X
X = torch.tensor([[1, 2, 3], [4, 5, 6]])
sequence_mask(X, torch.tensor([1, 2]))
sequence_mask的作用:告诉我合法长度,我把剩下的置为value
tensor([[1, 0, 0],
[4, 5, 0]])
X = torch.ones(2, 3, 4)
sequence_mask(X, torch.tensor([1, 2]), value=-1)
tensor([[[ 1., 1., 1., 1.],
[-1., -1., -1., -1.],
[-1., -1., -1., -1.]],
[[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[-1., -1., -1., -1.]]])
现在我们计算损失的情况,对每一个样本的每一个时间步都输出一个vocab_size大小的预测,但其实,有的时间步是无意义的填充,我们并不需要对这一部分计算损失
#@save
class MaskedSoftmaxCELoss(nn.CrossEntropyLoss):
"""带遮蔽的softmax交叉熵损失函数"""
# pred的形状:(batch_size,num_steps,vocab_size)
# label的形状:(batch_size,num_steps)
# valid_len的形状:(batch_size,)
def forward(self, pred, label, valid_len):
weights = torch.ones_like(label)
weights = sequence_mask(weights, valid_len)
self.reduction='none'
unweighted_loss = super(MaskedSoftmaxCELoss, self).forward(
pred.permute(0, 2, 1), label)
weighted_loss = (unweighted_loss * weights).mean(dim=1)
return weighted_loss
self.reduction='none'
损失函数先不做mean或者sum
pred.permute(0, 2, 1)
这里转一下,是因为pytorch需要把预测值放中间变成(批量大小,vocab_size,时间步)
weighted_loss = (unweighted_loss * weights).mean(dim=1)
dim = 1,对每一个样本(每一句话)做loss的均值
loss = MaskedSoftmaxCELoss()
loss(torch.ones(3, 4, 10), torch.ones((3, 4), dtype=torch.long),torch.tensor([4, 2, 0]))
pred (3, 4, 10) 3个样本,每个样本4个词,每个词10个维度
label (3, 4) 3个样本,每个样本4个词
torch.tensor([4, 2, 0]) :
第一个样本四个词都有用,
第二个样本两个填充,
第三个样本全是填充。
tensor([2.3026, 1.1513, 0.0000])
#@save
def train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device):
"""训练序列到序列模型"""
def xavier_init_weights(m):
if type(m) == nn.Linear:
nn.init.xavier_uniform_(m.weight)
if type(m) == nn.GRU:
for param in m._flat_weights_names:
if "weight" in param:
nn.init.xavier_uniform_(m._parameters[param])
net.apply(xavier_init_weights)
net.to(device)
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
loss = MaskedSoftmaxCELoss()
net.train()
animator = d2l.Animator(xlabel='epoch', ylabel='loss',
xlim=[10, num_epochs])
for epoch in range(num_epochs):
timer = d2l.Timer()
metric = d2l.Accumulator(2) # 训练损失总和,词元数量
for batch in data_iter:
optimizer.zero_grad()
X, X_valid_len, Y, Y_valid_len = [x.to(device) for x in batch]
bos = torch.tensor([tgt_vocab['' ]] * Y.shape[0],
device=device).reshape(-1, 1)
dec_input = torch.cat([bos, Y[:, :-1]], 1) # 强制教学
Y_hat, _ = net(X, dec_input, X_valid_len)
l = loss(Y_hat, Y, Y_valid_len)
l.sum().backward() # 损失函数的标量进行“反向传播”
d2l.grad_clipping(net, 1)
num_tokens = Y_valid_len.sum()
optimizer.step()
with torch.no_grad():
metric.add(l.sum(), num_tokens)
if (epoch + 1) % 10 == 0:
animator.add(epoch + 1, (metric[0] / metric[1],))
print(f'loss {metric[0] / metric[1]:.3f}, {metric[1] / timer.stop():.1f} '
f'tokens/sec on {str(device)}')
dec_input = torch.cat([bos, Y[:, :-1]], 1)
# 强制教学前面也讲过,训练时解码器的输入都是正确的翻译,且输入前还要加一个句子开始符
embed_size, num_hiddens, num_layers, dropout = 32, 32, 2, 0.1
batch_size, num_steps = 64, 10
lr, num_epochs, device = 0.005, 300, d2l.try_gpu()
train_iter, src_vocab, tgt_vocab = d2l.load_data_nmt(batch_size, num_steps)
encoder = Seq2SeqEncoder(len(src_vocab), embed_size, num_hiddens, num_layers,
dropout)
decoder = Seq2SeqDecoder(len(tgt_vocab), embed_size, num_hiddens, num_layers,
dropout)
net = d2l.EncoderDecoder(encoder, decoder)
train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device)
loss 0.019, 7038.1 tokens/sec on cuda:0
在预测时,解码器有真实的句子,在推理时没有,解码器只能把上一个输出当作下一个输入,去预测下一个输出
#@save
def predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps,
device, save_attention_weights=False):
"""序列到序列模型的预测"""
# 在预测时将net设置为评估模式
net.eval()
src_tokens = src_vocab[src_sentence.lower().split(' ')] + [
src_vocab['' ]]
enc_valid_len = torch.tensor([len(src_tokens)], device=device)
src_tokens = d2l.truncate_pad(src_tokens, num_steps, src_vocab['' ])
# 添加批量轴
enc_X = torch.unsqueeze(
torch.tensor(src_tokens, dtype=torch.long, device=device), dim=0)
enc_outputs = net.encoder(enc_X, enc_valid_len)
dec_state = net.decoder.init_state(enc_outputs, enc_valid_len)
# 添加批量轴
dec_X = torch.unsqueeze(torch.tensor(
[tgt_vocab['' ]], dtype=torch.long, device=device), dim=0)
output_seq, attention_weight_seq = [], []
for _ in range(num_steps):
Y, dec_state = net.decoder(dec_X, dec_state)
# 我们使用具有预测最高可能性的词元,作为解码器在下一时间步的输入
dec_X = Y.argmax(dim=2)
pred = dec_X.squeeze(dim=0).type(torch.int32).item()
# 保存注意力权重(稍后讨论)
if save_attention_weights:
attention_weight_seq.append(net.decoder.attention_weights)
# 一旦序列结束词元被预测,输出序列的生成就完成了
if pred == tgt_vocab['' ]:
break
output_seq.append(pred)
return ' '.join(tgt_vocab.to_tokens(output_seq)), attention_weight_seq
def bleu(pred_seq, label_seq, k): #@save
"""计算BLEU"""
pred_tokens, label_tokens = pred_seq.split(' '), label_seq.split(' ')
len_pred, len_label = len(pred_tokens), len(label_tokens)
score = math.exp(min(0, 1 - len_label / len_pred))
for n in range(1, k + 1):
num_matches, label_subs = 0, collections.defaultdict(int)
for i in range(len_label - n + 1):
label_subs[' '.join(label_tokens[i: i + n])] += 1
for i in range(len_pred - n + 1):
if label_subs[' '.join(pred_tokens[i: i + n])] > 0:
num_matches += 1
label_subs[' '.join(pred_tokens[i: i + n])] -= 1
score *= math.pow(num_matches / (len_pred - n + 1), math.pow(0.5, n))
return score
engs = ['go .', "i lost .", 'he\'s calm .', 'i\'m home .']
fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .']
for eng, fra in zip(engs, fras):
translation, attention_weight_seq = predict_seq2seq(
net, eng, src_vocab, tgt_vocab, num_steps, device)
print(f'{eng} => {translation}, bleu {bleu(translation, fra, k=2):.3f}')
go . => va !, bleu 1.000
i lost . => j'ai perdu emporté ?, bleu 0.447
he's calm . => il est mouillé malade ., bleu 0.548
i'm home . => je suis chez chez chez nous ., bleu 0.517