├── README.md ├── transformer.py └── Transformer学习总结附TF2.0代码实现.md /README.md: -------------------------------------------------------------------------------- 1 | # transformer 2 | 通过Tensorflow 2.0 实现transformer的构建过程 3 | 4 | transformer详细实现过程,参考https://blog.csdn.net/qq_43079023/article/details/103301846 5 | -------------------------------------------------------------------------------- /transformer.py: -------------------------------------------------------------------------------- 1 | # !/user/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # @Time : 2019/11/30 0030 14:42 4 | # @Author : yuenobel 5 | # @File : transformer.py 6 | # @Software : PyCharm 7 | 8 | 9 | import tensorflow as tf 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | 13 | 14 | def positional_encoding(pos, d_model): 15 | ''' 16 | :param pos: 词在句子中的位置,句子上的维族;(i是d_model上的维度) 17 | :param d_model: 隐状态的维度,相当于num_units 18 | :return: 位置编码 shape=[1, position_num, d_model], 其中第一个维度是为了匹配batch_size 19 | ''' 20 | def get_angles(position, i): 21 | # 这里的i相当于公式里面的2i或2i+1 22 | # 返回shape=[position_num, d_model] 23 | return position / np.power(10000., 2. * (i // 2.) / np.float(d_model)) 24 | 25 | angle_rates = get_angles(np.arange(pos)[:, np.newaxis], 26 | np.arange(d_model)[np.newaxis, :]) 27 | # 2i位置使用sin编码,2i+1位置使用cos编码 28 | pe_sin = np.sin(angle_rates[:, 0::2]) 29 | pe_cos = np.cos(angle_rates[:, 1::2]) 30 | pos_encoding = np.concatenate([pe_sin, pe_cos], axis=-1) 31 | pos_encoding = tf.cast(pos_encoding[np.newaxis, ...], tf.float32) 32 | return pos_encoding 33 | 34 | # # 演示positional_encoding 35 | # pos_encoding = positional_encoding(50, 512) 36 | # print(pos_encoding.shape) 37 | # plt.pcolormesh(pos_encoding[0], cmap='RdBu') 38 | # plt.xlabel('Depth') 39 | # plt.xlim((0, 512)) 40 | # plt.ylabel('Position') 41 | # plt.colorbar() 42 | # plt.show() 43 | 44 | 45 | '''*************** 第一部分: Scaled dot-product attention ***************''' 46 | def scaled_dot_product_attention(q, k, v, mask): 47 | '''attention(Q, K, V) = softmax(Q * K^T / sqrt(dk)) * V''' 48 | # query 和 Key相乘 49 | matmul_qk = tf.matmul(q, k, transpose_b=True) 50 | # 使用dk进行缩放 51 | dk = tf.cast(tf.shape(q)[-1], tf.float32) 52 | scaled_attention =matmul_qk / tf.math.sqrt(dk) 53 | # 掩码mask 54 | if mask is not None: 55 | # 这里将mask的token乘以-1e-9,这样与attention相加后,mask的位置经过softmax后就为0 56 | # padding位置 mask=1 57 | scaled_attention += mask * -1e-9 58 | # 通过softmax获取attention权重, mask部分softmax后为0 59 | attention_weights = tf.nn.softmax(scaled_attention) # shape=[batch_size, seq_len_q, seq_len_k] 60 | # 乘以value 61 | outputs = tf.matmul(attention_weights, v) # shape=[batch_size, seq_len_q, depth] 62 | return outputs, attention_weights 63 | 64 | '''*************** 第二部分: Multi-Head Attention ***************''' 65 | ''' 66 | multi-head attention包含3部分: - 线性层与分头 - 缩放点积注意力 - 头连接 - 末尾线性层 67 | 每个多头注意块有三个输入; Q(查询),K(密钥),V(值)。 它们通过第一层线性层并分成多个头。 68 | 注意:点积注意力时需要使用mask, 多头输出需要使用tf.transpose调整各维度。 69 | Q,K和V不是一个单独的注意头,而是分成多个头,因为它允许模型共同参与来自不同表征空间的不同信息。 70 | 在拆分之后,每个头部具有降低的维度,总计算成本与具有全维度的单个头部注意力相同。 71 | ''' 72 | class MultiHeadAttention(tf.keras.layers.Layer): 73 | def __init__(self, d_model, num_heads): 74 | super(MultiHeadAttention, self).__init__() 75 | self.num_heads = num_heads 76 | self.d_model = d_model 77 | # d_model必须可以正确分成多个头 78 | assert d_model % num_heads == 0 79 | # 分头之后维度 80 | self.depth = d_model // num_heads 81 | self.wq = tf.keras.layers.Dense(d_model) 82 | self.wk = tf.keras.layers.Dense(d_model) 83 | self.wv = tf.keras.layers.Dense(d_model) 84 | self.dense = tf.keras.layers.Dense(d_model) 85 | 86 | def split_heads(self, x, batch_size): 87 | # 分头,将头个数的维度,放到seq_len前面 x输入shape=[batch_size, seq_len, d_model] 88 | x = tf.reshape(x, [batch_size, -1, self.num_heads, self.depth]) 89 | return tf.transpose(x, perm=[0, 2, 1, 3]) 90 | 91 | def call(self, q, k, v, mask): 92 | batch_size = tf.shape(q)[0] 93 | # 分头前的前向网络,根据q,k,v的输入,计算Q, K, V语义 94 | q = self.wq(q) # shape=[batch_size, seq_len_q, d_model] 95 | k = self.wq(k) 96 | v = self.wq(v) 97 | # 分头 98 | q = self.split_heads(q, batch_size) # shape=[batch_size, num_heads, seq_len_q, depth] 99 | k = self.split_heads(k, batch_size) 100 | v = self.split_heads(v, batch_size) 101 | # 通过缩放点积注意力层 102 | # scaled_attention shape=[batch_size, num_heads, seq_len_q, depth] 103 | # attention_weights shape=[batch_size, num_heads, seq_len_q, seq_len_k] 104 | scaled_attention, attention_weights = scaled_dot_product_attention(q, k, v, mask) 105 | # 把多头维度后移 106 | scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # shape=[batch_size, seq_len_q, num_heads, depth] 107 | # 把多头合并 108 | concat_attention = tf.reshape(scaled_attention, (batch_size, -1, self.d_model)) # shape=[batch_size, seq_len_q, d_model] 109 | # 全连接重塑 110 | output = self.dense(concat_attention) 111 | return output, attention_weights 112 | 113 | # # 测试multi-head attention 114 | # temp_mha = MultiHeadAttention(d_model=512, num_heads=8) 115 | # y = tf.random.uniform((1, 60, 512)) 116 | # output, att = temp_mha(y, y, y, None) 117 | # print(output.shape, att.shape) 118 | 119 | 120 | class LayerNormalization(tf.keras.layers.Layer): 121 | def __init__(self, epsilon=1e-8, **kwargs): 122 | super(LayerNormalization, self).__init__(**kwargs) 123 | self.epsilon = epsilon 124 | def build(self, input_shape): 125 | self.gamma = self.add_weight(name='gamma', 126 | shape=input_shape[-1:], 127 | initializer=tf.ones_initializer(), 128 | trainable=True) 129 | self.beta = self.add_weight(name='beta', 130 | shape=input_shape[-1:], 131 | initializer=tf.zeros_initializer(), 132 | trainable=True) 133 | super(LayerNormalization, self).build(input_shape) 134 | def call(self, x): # x shape=[batch_size, seq_len, d_model] 135 | mean = tf.keras.backend.mean(x, axis=-1, keepdims=True) 136 | std = tf.keras.backend.std(x, axis=-1, keepdims=True) 137 | return self.gamma * (x - mean) / (std + self.epsilon) + self.beta 138 | 139 | # point wise 前向网络 140 | def point_wise_feed_forward(d_model, diff): 141 | return tf.keras.Sequential([ 142 | tf.keras.layers.Dense(diff, activation=tf.nn.relu), 143 | tf.keras.layers.Dense(d_model) 144 | ]) 145 | 146 | 147 | '''encoder layer: 148 | 每个编码层包含以下子层 - Multi-head attention(带掩码) - Point wise feed forward networks 149 | 每个子层中都有残差连接,并最后通过一个正则化层。残差连接有助于避免深度网络中的梯度消失问题。 150 | 每个子层输出是LayerNorm(x + Sublayer(x)),规范化是在d_model维的向量上。Transformer一共有n个编码层。 151 | ''' 152 | class EncoderLayer(tf.keras.layers.Layer): 153 | def __init__(self, d_model, num_heads, dff, dropout_rate=0.1): 154 | super(EncoderLayer, self).__init__() 155 | self.mha = MultiHeadAttention(d_model, num_heads) 156 | self.ffn = point_wise_feed_forward(d_model, dff) 157 | self.layernorm1 = LayerNormalization() 158 | self.layernorm2 = LayerNormalization() 159 | self.dropout1 = tf.keras.layers.Dropout(dropout_rate) 160 | self.dropout2 = tf.keras.layers.Dropout(dropout_rate) 161 | def call(self, inputs, training, mask): 162 | # multi head attention (encoder时Q = K = V) 163 | att_output, _ = self.mha(inputs, inputs, inputs, mask) 164 | att_output = self.dropout1(att_output, training=training) 165 | output1 = self.layernorm1(inputs + att_output) # shape=[batch_size, seq_len, d_model] 166 | # feed forward network 167 | ffn_output = self.ffn(output1) 168 | ffn_output = self.dropout2(ffn_output, training=training) 169 | output2 = self.layernorm2(output1 + ffn_output) # shape=[batch_size, seq_len, d_model] 170 | return output2 171 | 172 | class Encoder(tf.keras.layers.Layer): 173 | def __init__(self, d_model, num_layers, num_heads, dff, 174 | input_vocab_size, max_seq_len, dropout_rate=0.1): 175 | super(Encoder, self).__init__() 176 | self.num_layers = num_layers 177 | self.d_model = d_model 178 | self.emb = tf.keras.layers.Embedding(input_vocab_size, d_model) # shape=[batch_size, seq_len, d_model] 179 | self.pos_encoding = positional_encoding(max_seq_len, d_model) # shape=[1, max_seq_len, d_model] 180 | self.encoder_layer = [EncoderLayer(d_model, num_heads, dff, dropout_rate) 181 | for _ in range(num_layers)] 182 | self.dropout = tf.keras.layers.Dropout(dropout_rate) 183 | def call(self, inputs, training, mask): 184 | # 输入部分;inputs shape=[batch_size, seq_len] 185 | seq_len = inputs.shape[1] # 句子真实长度 186 | word_embedding = self.emb(inputs) # shape=[batch_size, seq_len, d_model] 187 | word_embedding *= tf.math.sqrt(tf.cast(self.d_model, tf.float32)) 188 | emb= word_embedding + self.pos_encoding[:, :seq_len, :] 189 | x = self.dropout(emb, training=training) 190 | for i in range(self.num_layers): 191 | x = self.encoder_layer[i](x, training, mask) 192 | return x # shape=[batch_size, seq_len, d_model] 193 | 194 | # # 编码器测试 195 | # sample_encoder = Encoder(512, 2, 8, 1024, 5000, 200) 196 | # sample_encoder_output = sample_encoder(tf.random.uniform((64, 120)), False, None) 197 | # print(sample_encoder_output.shape) 198 | 199 | 200 | # padding mask 201 | def create_padding_mask(seq): 202 | '''为了避免输入中padding的token对句子语义的影响,需要将padding位mark掉, 203 | 原来为0的padding项的mask输出为1; encoder和decoder过程都会用到''' 204 | seq = tf.cast(tf.math.equal(seq, 0), tf.float32) 205 | # 扩充维度以便于使用attention矩阵;seq输入shape=[batch_size, seq_len];输出shape=[batch_siz, 1, 1, seq_len] 206 | return seq[:, np.newaxis, np.newaxis, :] 207 | 208 | # look-ahead mask 209 | def create_look_ahead_mask(size): 210 | '''用于对未预测的token进行掩码 这意味着要预测第三个单词,只会使用第一个和第二个单词。 211 | 要预测第四个单词,仅使用第一个,第二个和第三个单词,依此类推。只有decoder过程用到''' 212 | # 产生一个上三角矩阵,上三角的值全为0。把这个矩阵作用在每一个序列上,就可以达到我们的目的。 213 | mask = 1 - tf.linalg.band_part(tf.ones((size, size)), -1, 0) 214 | return mask # shape=[seq_len, seq_len] 215 | 216 | def create_mask(inputs, targets): 217 | # 编码器只有padding_mask 218 | encoder_padding_mask = create_padding_mask(inputs) 219 | # 解码器decoder_padding_mask,用于第二层multi-head attention 220 | decoder_padding_mask = create_padding_mask(inputs) 221 | # seq_mask mask掉未预测的词 222 | seq_mask = create_look_ahead_mask(tf.shape(targets)[1]) 223 | # decoder_targets_padding_mask 解码层的输入padding mask 224 | decoder_targets_padding_mask = create_padding_mask(targets) 225 | # 合并解码层mask,用于第一层masked multi-head attention 226 | look_ahead_mask = tf.maximum(decoder_targets_padding_mask, seq_mask) 227 | return encoder_padding_mask, look_ahead_mask, decoder_padding_mask 228 | 229 | ''' 230 | decoder layer: 231 | 每个编码层包含以下子层: - Masked muti-head attention(带padding掩码和look-ahead掩码 232 | - Muti-head attention(带padding掩码)value和key来自encoder输出, 233 | query来自Masked muti-head attention层输出 - Point wise feed forward network 234 | 每个子层中都有残差连接,并最后通过一个正则化层。残差连接有助于避免深度网络中的梯度消失问题。 235 | 每个子层输出是LayerNorm(x + Sublayer(x)),规范化是在d_model维的向量上。Transformer一共有n个解码层。 236 | 当Q从解码器的第一个注意块接收输出,并且K接收编码器输出时,注意权重表示基于编码器输出给予解码器输入的重要性。 237 | 换句话说,解码器通过查看编码器输出并自我关注其自己的输出来预测下一个字。 238 | ps:因为padding在后面所以look-ahead掩码同时掩padding 239 | ''' 240 | class DecoderLayer(tf.keras.layers.Layer): 241 | def __init__(self, d_model, num_heads, dff, dropout_rate=0.1): 242 | super(DecoderLayer, self).__init__() 243 | self.mha1 = MultiHeadAttention(d_model, num_heads) 244 | self.mha2 = MultiHeadAttention(d_model, num_heads) 245 | self.ffn = point_wise_feed_forward(d_model, dff) 246 | self.layernorm1 = LayerNormalization() 247 | self.layernorm2 = LayerNormalization() 248 | self.layernorm3 = LayerNormalization() 249 | self.dropout1 = tf.keras.layers.Dropout(dropout_rate) 250 | self.dropout2 = tf.keras.layers.Dropout(dropout_rate) 251 | self.dropout3 = tf.keras.layers.Dropout(dropout_rate) 252 | def call(self, inputs, encoder_out, training, look_ahead_mask, padding_mask): 253 | # masked multi-head attention: Q = K = V 254 | att_out1, att_weight1 = self.mha1(inputs, inputs, inputs, look_ahead_mask) 255 | att_out1 = self.dropout1(att_out1, training=training) 256 | att_out1 = self.layernorm1(inputs + att_out1) 257 | # multi-head attention: Q=att_out1, K = V = encoder_out 258 | att_out2, att_weight2 = self.mha2(att_out1, encoder_out, encoder_out, padding_mask) 259 | att_out2 = self.dropout2(att_out2, training=training) 260 | att_out2 = self.layernorm2(att_out1 + att_out2) 261 | # feed forward network 262 | ffn_out = self.ffn(att_out2) 263 | ffn_out = self.dropout3(ffn_out, training=training) 264 | output = self.layernorm3(att_out2 + ffn_out) 265 | return output, att_weight1, att_weight2 266 | 267 | class Decoder(tf.keras.layers.Layer): 268 | def __init__(self, d_model, num_layers, num_heads, dff, 269 | target_vocab_size, max_seq_len, dropout_rate=0.1): 270 | super(Decoder, self).__init__() 271 | self.seq_len = tf.shape 272 | self.d_model = d_model 273 | self.num_layers = num_layers 274 | self.word_embedding = tf.keras.layers.Embedding(target_vocab_size, d_model) 275 | self.pos_encoding = positional_encoding(max_seq_len, d_model) 276 | self.decoder_layers = [DecoderLayer(d_model, num_heads, dff, dropout_rate) 277 | for _ in range(num_layers)] 278 | self.dropout = tf.keras.layers.Dropout(dropout_rate) 279 | def call(self, inputs, encoder_out, training, look_ahead_mask, padding_mask): 280 | seq_len = inputs.shape[1] 281 | attention_weights = {} 282 | word_embedding = self.word_embedding(inputs) 283 | word_embedding *= tf.math.sqrt(tf.cast(self.d_model, tf.float32)) 284 | emb = word_embedding + self.pos_encoding[:, :seq_len, :] 285 | x = self.dropout(emb, training=training) 286 | for i in range(self.num_layers): 287 | x, att1, att2 = self.decoder_layers[i](x, encoder_out, training, 288 | look_ahead_mask, padding_mask) 289 | attention_weights['decoder_layer{}_att_w1'.format(i+1)] = att1 290 | attention_weights['decoder_layer{}_att_w2'.format(i + 1)] = att2 291 | return x, attention_weights 292 | 293 | # # 解码器测试 294 | # sample_decoder = Decoder(512, 2, 8, 1024, 5000, 200) 295 | # sample_decoder_output, attn = sample_decoder(tf.random.uniform((64, 100)), 296 | # sample_encoder_output, False, None, None) 297 | # print(sample_decoder_output.shape) 298 | # print(attn['decoder_layer1_att_w2'].shape) 299 | 300 | 301 | '''Transformer包含编码器、解码器和最后的线性层,解码层的输出经过线性层后得到Transformer的输出''' 302 | class Transformer(tf.keras.Model): 303 | def __init__(self, d_model, num_layers, num_heads, dff, 304 | input_vocab_size, target_vocab_size, max_seq_len, dropout_rate=0.1): 305 | super(Transformer, self).__init__() 306 | self.encoder = Encoder(d_model, num_layers, num_heads, dff, input_vocab_size, max_seq_len, dropout_rate) 307 | self.decoder = Decoder(d_model, num_layers, num_heads, dff, target_vocab_size, max_seq_len, dropout_rate) 308 | self.final_layer = tf.keras.layers.Dense(target_vocab_size) 309 | def call(self, inputs, targets, training, encoder_padding_mask, 310 | look_ahead_mask, decoder_padding_mask): 311 | # 首先encoder过程,输出shape=[batch_size, seq_len_input, d_model] 312 | encoder_output = self.encoder(inputs, training, encoder_padding_mask) 313 | # 再进行decoder, 输出shape=[batch_size, seq_len_target, d_model] 314 | decoder_output, att_weights = self.decoder(targets, encoder_output, training, 315 | look_ahead_mask, decoder_padding_mask) 316 | # 最后映射到输出层 317 | final_out = self.final_layer(decoder_output) # shape=[batch_size, seq_len_target, target_vocab_size] 318 | return final_out, att_weights 319 | 320 | # # transformer测试 321 | # sample_transformer = Transformer( 322 | # num_layers=2, d_model=512, num_heads=8, dff=1024, 323 | # input_vocab_size=8500, target_vocab_size=8000, max_seq_len=120 324 | # ) 325 | # temp_input = tf.random.uniform((64, 62)) 326 | # temp_target = tf.random.uniform((64, 26)) 327 | # fn_out, att = sample_transformer(temp_input, temp_target, training=False, 328 | # encoder_padding_mask=None, 329 | # look_ahead_mask=None, 330 | # decoder_padding_mask=None, 331 | # ) 332 | # print(fn_out.shape) 333 | # print(att['decoder_layer1_att_w1'].shape) 334 | # print(att['decoder_layer1_att_w2'].shape) 335 | 336 | -------------------------------------------------------------------------------- /Transformer学习总结附TF2.0代码实现.md: -------------------------------------------------------------------------------- 1 | @[TOC](Transformer学习总结附TF2.0代码实现) 2 | 3 | # Transformer 4 | 5 | 谷歌发表的论文《Attention is all you need》中提出的一种新的深度学习架构transformer,其用于自然语言处理的重要性相信大家都有很深刻的认识,这里不再赘述,下文直接上干货。 6 | 7 | # 1.Transformer详解 8 | 9 | 个人对transformer学习后,认为实现transformer总体流程如下: 10 | 1. 输入部分:word embedding + positional ecoding; 11 | 2. Multi-Headed Attention; 12 | 3. Add and Layer normalization; 13 | 4. Feed Forward; 14 | 5. Decoder层; 15 | 6. 输出层 16 | 后文按照以上顺序,分别进行详细描述,每一部分理论完结都跟随相应代码实现。 17 | 18 | ## 1.1 transformer总体架构 19 | 20 | 和Seq2Seq模型一样,Transformer模型中也采用了 encoer-decoder 架构。但其结构相比于Seq2Seq更加复杂,论文中encoder层由6个encoder堆叠在一起,decoder层也一样。 21 | 每一个encoder和decoder的内部简版结构如下图 22 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191128215032693.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 23 | 对于encoder,包含两层,一个self-attention层和一个前馈神经网络,self-attention能帮助当前节点不仅仅只关注当前的词,从而能获取到上下文的语义。decoder也包含encoder提到的两层网络,但是在这两层中间还有一层attention层,帮助当前节点获取到当前需要关注的重点内容。 24 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191128215050872.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 25 | 现在我们知道了模型的主要组件,接下来我们看下模型的内部细节。首先,模型需要对输入的数据进行一个embedding操作,(也可以理解为类似w2c的操作),enmbedding结束之后,输入到encoder层,self-attention处理完数据后把数据送给前馈神经网络,前馈神经网络的计算可以并行,得到的输出会输入到下一个encoder。 26 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191128215058613.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 27 | 28 | ## 1.2 输入部分 29 | transformer的输入部分使用的是word embedding和Positional Encoding的结合。为了解释输入序列中单词顺序,transformer给encoder层和decoder层的输入添加了一个额外的向量Positional Encoding,维度和word embedding的维度一样,这个向量采用了一种很独特的方法来让模型学习到这个值,这个向量能决定当前词的位置,或者说在一个句子中不同的词之间的距离。这个位置向量的具体计算方法有很多种,论文中的计算方法如下: 30 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/2019113014491261.png) 31 | 其中pos是指当前词在句子中的位置,i是指向量中每个值的index,可以看出,在偶数位置,使用正弦编码,在奇数位置,使用余弦编码。最后把这个Positional Encoding与embedding的值相加,作为输入送到下一层。 32 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130145045768.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 33 | 为了让模型捕捉到单词的顺序信息,我们添加位置编码向量信息(POSITIONAL ENCODING),位置编码向量不需要训练,它有一个规则的产生方式(上图公式)。 34 | 35 | 如果我们的嵌入维度为4,那么实际上的位置编码就如下图所示: 36 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130145125145.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 37 | 那么生成位置向量需要遵循怎样的规则呢? 38 | 39 | 观察下面的图形,每一行都代表着对一个矢量的位置编码。因此第一行就是我们输入序列中第一个字的嵌入向量,每行都包含512个值,每个值介于1和-1之间。我们用颜色来表示1,-1之间的值,这样方便可视化的方式表现出来: 40 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130145150364.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 41 | 这是一个20个字(行)的(512)列位置编码示例。你会发现它咋中心位置被分为了2半,这是因为左半部分的值是一由一个正弦函数生成的,而右半部分是由另一个函数(余弦)生成。然后将它们连接起来形成每个位置编码矢量。 42 | 43 | Positianal Encoding代码实现实例如下: 44 | ```javascript 45 | def positional_encoding(pos, d_model): 46 | ''' 47 | :param pos: 词在句子中的位置,句子上的维族;(i是d_model上的维度) 48 | :param d_model: 隐状态的维度,相当于num_units 49 | :return: 位置编码 shape=[1, position_num, d_model], 其中第一个维度是为了匹配batch_size 50 | ''' 51 | def get_angles(position, i): 52 | # 这里的i相当于公式里面的2i或2i+1 53 | # 返回shape=[position_num, d_model] 54 | return position / np.power(10000., 2. * (i // 2.) / np.float(d_model)) 55 | 56 | angle_rates = get_angles(np.arange(pos)[:, np.newaxis], 57 | np.arange(d_model)[np.newaxis, :]) 58 | # 2i位置使用sin编码,2i+1位置使用cos编码 59 | pe_sin = np.sin(angle_rates[:, 0::2]) 60 | pe_cos = np.cos(angle_rates[:, 1::2]) 61 | pos_encoding = np.concatenate([pe_sin, pe_cos], axis=-1) 62 | pos_encoding = tf.cast(pos_encoding[np.newaxis, ...], tf.float32) 63 | return pos_encoding 64 | 65 | # 演示positional_encoding 66 | pos_encoding = positional_encoding(50, 512) 67 | print(pos_encoding.shape) 68 | plt.pcolormesh(pos_encoding[0], cmap='RdBu') 69 | plt.xlabel('Depth') 70 | plt.xlim((0, 512)) 71 | plt.ylabel('Position') 72 | plt.colorbar() 73 | plt.show() 74 | ``` 75 | 76 | ## 1.3 Multi-Head Attention 77 | 接下来我们详细看一下self-attention,其思想和attention类似,但是self-attention是Transformer用来将其他相关单词的“理解”转换成我们正常理解的单词的一种思路,我们看个例子: 78 | The animal didn't cross the street because it was too tired 79 | 这里的it到底代表的是animal还是street呢,对于我们来说能很简单的判断出来,但是对于机器来说,是很难判断的,self-attention就能够让机器把it和animal联系起来 80 | 81 | 接下来我们看下详细的处理过程。 82 | 83 | 1、首先,self-attention会计算出三个新的向量,在论文中,向量的维度是512维,我们把这三个向量分别称为Query、Key、Value,这三个向量是用embedding向量与一个矩阵相乘得到的结果,这个矩阵是随机初始化的,维度为(64,512)注意第二个维度需要和embedding的维度一样,其值在BP的过程中会一直进行更新,得到的这三个向量的维度是64低于embedding维度的。 84 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194620852.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 85 | 那么Query、Key、Value这三个向量又是什么呢?这三个向量对于attention来说很重要,当你理解了下文后,你将会明白这三个向量扮演者什么的角色。 86 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194641605.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 87 | 2、计算self-attention的分数值,该分数值决定了当我们在某个位置encode一个词时,对输入句子的其他部分的关注程度。这个分数值的计算方法是Query与Key做点乘,以下图为例,首先我们需要针对Thinking这个词,计算出其他词对于该词的一个分数值,首先是针对于自己本身即q1·k1,然后是针对于第二个词即q1·k2 88 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194651500.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 89 | 3、接下来,把点成的结果除以一个常数,这里我们除以8,这个值一般是采用上文提到的矩阵的第一个维度的开方即64的开方8,当然也可以选择其他的值,然后把得到的结果做一个softmax的计算。得到的结果即是每个词对于当前位置的词的相关性大小,当然,当前位置的词相关性肯定会会很大 90 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194659530.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 91 | 4、下一步就是把Value和softmax得到的值进行相乘,并相加,得到的结果即是self-attetion在当前节点的值。 92 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194730440.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 93 | 在实际的应用场景,为了提高计算速度,我们采用的是矩阵的方式,直接计算出Query, Key, Value的矩阵,然后把embedding的值与三个矩阵直接相乘,把得到的新矩阵Q与K相乘,乘以一个常数,做softmax操作,最后乘上V矩阵 94 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194741154.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 95 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194752458.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 96 | 这种通过 query 和 key 的相似性程度来确定 value 的权重分布的方法被称为scaled dot-product attention。其实scaled dot-Product attention就是我们常用的使用点积进行相似度计算的attention,只是多除了一个(为K的维度)起到调节作用,使得内积不至于太大。 97 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130194756645.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 98 | 这篇论文更厉害的地方是给self-attention加入了另外一个机制,被称为“multi-headed” attention,该机制理解起来很简单,就是说不仅仅只初始化一组Q、K、V的矩阵,而是初始化多组,tranformer是使用了8组,所以最后得到的结果是8个矩阵。 99 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195347312.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 100 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195355990.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 101 | 这给我们留下了一个小的挑战,前馈神经网络没法输入8个矩阵呀,这该怎么办呢?所以我们需要一种方式,把8个矩阵降为1个,首先,我们把8个矩阵连在一起,这样会得到一个大的矩阵,再随机初始化一个矩阵和这个组合好的矩阵相乘,最后得到一个最终的矩阵。 102 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195410270.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 103 | 这就是multi-headed attention的全部流程了,这里其实已经有很多矩阵了,我们把所有的矩阵放到一张图内看一下总体的流程。 104 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195418401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 105 | 多头attention(Multi-head attention)整个过程可以简述为:Query,Key,Value首先进过一个线性变换,然后输入到放缩点积attention(注意这里要做h次,其实也就是所谓的多头,每一次算一个头,而且每次Q,K,V进行线性变换的参数W是不一样的),然后将h次的放缩点积attention结果进行拼接,再进行一次线性变换得到的值作为多头attention的结果。可以看到,google提出来的多头attention的不同之处在于进行了h次计算而不仅仅算一次,论文中说到这样的好处是可以允许模型在不同的表示子空间里学习到相关的信息,后面还会根据attention可视化来验证。 106 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195423663.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 107 | 那么在整个模型中,是如何使用attention的呢?如下图,首先在编码器到解码器的地方使用了多头attention进行连接,K,V,Q分别是编码器的层输出(这里K=V)和解码器中都头attention的输入。其实就和主流的机器翻译模型中的attention一样,利用解码器和编码器attention来进行翻译对齐。然后在编码器和解码器中都使用了多头自注意力self-attention来学习文本的表示。Self-attention即K=V=Q,例如输入一个句子,那么里面的每个词都要和该句子中的所有词进行attention计算。目的是学习句子内部的词依赖关系,捕获句子的内部结构。 108 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195434718.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 109 | 对于使用自注意力机制的原因,论文中提到主要从三个方面考虑(每一层的复杂度,是否可以并行,长距离依赖学习),并给出了和RNN,CNN计算复杂度的比较。可以看到,如果输入序列n小于表示维度d的话,每一层的时间复杂度self-attention是比较有优势的。当n比较大时,作者也给出了一种解决方案self-attention(restricted)即每个词不是和所有词计算attention,而是只与限制的r个词去计算attention。在并行方面,多头attention和CNN一样不依赖于前一时刻的计算,可以很好的并行,优于RNN。在长距离依赖上,由于self-attention是每个词和所有词都要计算attention,所以不管他们中间有多长距离,最大的路径长度也都只是1。可以捕获长距离依赖关系。 110 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195446426.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 111 | 现在我们已经接触了attention的header,让我们重新审视我们之前的例子,看看例句中的“it”这个单词在不同的attention header情况下会有怎样不同的关注点(这里不同颜色代表attention不同头的结果,颜色越深attention值越大)。 112 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195452641.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 113 | 当我们对“it”这个词进行编码时,一个注意力的焦点主要集中在“animal”上,而另一个注意力集中在“tired”(两个heads) 114 | 但是,如果我们将所有注意力添加到图片中,可能有点难理解: 115 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130195456828.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 116 | Multi-Head Attention代码实现实例如下: 117 | ```javascript 118 | '''*************** 第一部分: Scaled dot-product attention ***************''' 119 | def scaled_dot_product_attention(q, k, v, mask): 120 | '''attention(Q, K, V) = softmax(Q * K^T / sqrt(dk)) * V''' 121 | # query 和 Key相乘 122 | matmul_qk = tf.matmul(q, k, transpose_b=True) 123 | # 使用dk进行缩放 124 | dk = tf.cast(tf.shape(q)[-1], tf.float32) 125 | scaled_attention =matmul_qk / tf.math.sqrt(dk) 126 | # 掩码mask 127 | if mask is not None: 128 | # 这里将mask的token乘以-1e-9,这样与attention相加后,mask的位置经过softmax后就为0 129 | # padding位置 mask=1 130 | scaled_attention += mask * -1e-9 131 | # 通过softmax获取attention权重, mask部分softmax后为0 132 | attention_weights = tf.nn.softmax(scaled_attention) # shape=[batch_size, seq_len_q, seq_len_k] 133 | # 乘以value 134 | outputs = tf.matmul(attention_weights, v) # shape=[batch_size, seq_len_q, depth] 135 | return outputs, attention_weights 136 | 137 | '''*************** 第二部分: Multi-Head Attention ***************''' 138 | ''' 139 | multi-head attention包含3部分: - 线性层与分头 - 缩放点积注意力 - 头连接 - 末尾线性层 140 | 每个多头注意块有三个输入; Q(查询),K(密钥),V(值)。 它们通过第一层线性层并分成多个头。 141 | 注意:点积注意力时需要使用mask, 多头输出需要使用tf.transpose调整各维度。 142 | Q,K和V不是一个单独的注意头,而是分成多个头,因为它允许模型共同参与来自不同表征空间的不同信息。 143 | 在拆分之后,每个头部具有降低的维度,总计算成本与具有全维度的单个头部注意力相同。 144 | ''' 145 | class MultiHeadAttention(tf.keras.layers.Layer): 146 | def __init__(self, d_model, num_heads): 147 | super(MultiHeadAttention, self).__init__() 148 | self.num_heads = num_heads 149 | self.d_model = d_model 150 | # d_model必须可以正确分成多个头 151 | assert d_model % num_heads == 0 152 | # 分头之后维度 153 | self.depth = d_model // num_heads 154 | self.wq = tf.keras.layers.Dense(d_model) 155 | self.wk = tf.keras.layers.Dense(d_model) 156 | self.wv = tf.keras.layers.Dense(d_model) 157 | self.dense = tf.keras.layers.Dense(d_model) 158 | 159 | def split_heads(self, x, batch_size): 160 | # 分头,将头个数的维度,放到seq_len前面 x输入shape=[batch_size, seq_len, d_model] 161 | x = tf.reshape(x, [batch_size, -1, self.num_heads, self.depth]) 162 | return tf.transpose(x, perm=[0, 2, 1, 3]) 163 | 164 | def call(self, q, k, v, mask): 165 | batch_size = tf.shape(q)[0] 166 | # 分头前的前向网络,根据q,k,v的输入,计算Q, K, V语义 167 | q = self.wq(q) # shape=[batch_size, seq_len_q, d_model] 168 | k = self.wq(k) 169 | v = self.wq(v) 170 | # 分头 171 | q = self.split_heads(q, batch_size) # shape=[batch_size, num_heads, seq_len_q, depth] 172 | k = self.split_heads(k, batch_size) 173 | v = self.split_heads(v, batch_size) 174 | # 通过缩放点积注意力层 175 | # scaled_attention shape=[batch_size, num_heads, seq_len_q, depth] 176 | # attention_weights shape=[batch_size, num_heads, seq_len_q, seq_len_k] 177 | scaled_attention, attention_weights = scaled_dot_product_attention(q, k, v, mask) 178 | # 把多头维度后移 179 | scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # shape=[batch_size, seq_len_q, num_heads, depth] 180 | # 把多头合并 181 | concat_attention = tf.reshape(scaled_attention, (batch_size, -1, self.d_model)) # shape=[batch_size, seq_len_q, d_model] 182 | # 全连接重塑 183 | output = self.dense(concat_attention) 184 | return output, attention_weights 185 | 186 | # 测试multi-head attention 187 | temp_mha = MultiHeadAttention(d_model=512, num_heads=8) 188 | y = tf.random.uniform((1, 60, 512)) 189 | output, att = temp_mha(y, y, y, None) 190 | print(output.shape, att.shape) 191 | ``` 192 | 193 | ## 1.4 Add and Layer normalization 194 | 在transformer中,每一个子层(self-attetion,ffnn)之后都会接一个残差模块,并且有一个Layer normalization 195 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130211821336.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 196 | 在进一步探索其内部计算方式,我们可以将上面图层可视化为下图: 197 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130211828544.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 198 | 残差模块相信大家都很清楚了,这里不再讲解,主要讲解下Layer normalization。Normalization有很多种,但是它们都有一个共同的目的,那就是把输入转化成均值为0方差为1的数据。我们在把数据送入激活函数之前进行normalization(归一化),因为我们不希望输入数据落在激活函数的饱和区。 199 | 说到 normalization,那就肯定得提到 Batch Normalization。BN的主要思想就是:在每一层的每一批数据上进行归一化。我们可能会对输入数据进行归一化,但是经过该网络层的作用后,我们的数据已经不再是归一化的了。随着这种情况的发展,数据的偏差越来越大,我的反向传播需要考虑到这些大的偏差,这就迫使我们只能使用较小的学习率来防止梯度消失或者梯度爆炸。 200 | BN的具体做法就是对每一小批数据,在批这个方向上做归一化。如下图所示: 201 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130212251185.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 202 | 可以看到,右半边求均值是沿着数据 batch_size的方向进行的,其计算公式如下: 203 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130212303189.png) 204 | 那么什么是 Layer normalization 呢?它也是归一化数据的一种方式,不过 LN 是在每一个样本上计算均值和方差,而不是BN那种在批方向计算均值和方差! 205 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130212340647.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 206 | 下面看一下 LN 的公式: 207 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130212016617.png) 208 | Layer normalization 代码实现实例如下: 209 | ```javascript 210 | class LayerNormalization(tf.keras.layers.Layer): 211 | def __init__(self, epsilon=1e-8, **kwargs): 212 | super(LayerNormalization, self).__init__(**kwargs) 213 | self.epsilon = epsilon 214 | def build(self, input_shape): 215 | self.gamma = self.add_weight(name='gamma', 216 | shape=input_shape[-1:], 217 | initializer=tf.ones_initializer(), 218 | trainable=True) 219 | self.beta = self.add_weight(name='beta', 220 | shape=input_shape[-1:], 221 | initializer=tf.zeros_initializer(), 222 | trainable=True) 223 | super(LayerNormalization, self).build(input_shape) 224 | def call(self, x): # x shape=[batch_size, seq_len, d_model] 225 | mean = tf.keras.backend.mean(x, axis=-1, keepdims=True) 226 | std = tf.keras.backend.std(x, axis=-1, keepdims=True) 227 | return self.gamma * (x - mean) / (std + self.epsilon) + self.beta 228 | ``` 229 | 230 | 231 | ## 1.5 Feed Forward 232 | 模型经过multi-head attention后,经过一层feed forward层。模型中的前向反馈层,采用的是一种posion-wise feed-forward的方法,具体公式如下:FFN(x) = max(0, xW1 + b1)W2 + b2 233 | 此处方法容易理解,先对输入加一个全连接网络,之后使用Relu激活,之后再加一个全连接网络。 234 | 235 | Feed Forward 代码实现实例如下: 236 | ```javascript 237 | def point_wise_feed_forward(d_model, diff): 238 | return tf.keras.Sequential([ 239 | tf.keras.layers.Dense(diff, activation=tf.nn.relu), 240 | tf.keras.layers.Dense(d_model) 241 | ]) 242 | ``` 243 | 244 | ## 1.6 Encoder层 245 | 到这里为止就是全部encoders的内容了,如果把两个encoders叠加在一起就是这样的结构,在self-attention需要强调的最后一点是其采用了残差网络中的short-cut结构,目的是解决深度学习中的梯度弥散问题。 246 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130221716218.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 247 | 整个 Encoder 过程代码实现实例如下: 248 | ```javascript 249 | '''encoder layer: 250 | 每个编码层包含以下子层 - Multi-head attention(带掩码) - Point wise feed forward networks 251 | 每个子层中都有残差连接,并最后通过一个正则化层。残差连接有助于避免深度网络中的梯度消失问题。 252 | 每个子层输出是LayerNorm(x + Sublayer(x)),规范化是在d_model维的向量上。Transformer一共有n个编码层。 253 | ''' 254 | class EncoderLayer(tf.keras.layers.Layer): 255 | def __init__(self, d_model, num_heads, dff, dropout_rate=0.1): 256 | super(EncoderLayer, self).__init__() 257 | self.mha = MultiHeadAttention(d_model, num_heads) 258 | self.ffn = point_wise_feed_forward(d_model, dff) 259 | self.layernorm1 = LayerNormalization() 260 | self.layernorm2 = LayerNormalization() 261 | self.dropout1 = tf.keras.layers.Dropout(dropout_rate) 262 | self.dropout2 = tf.keras.layers.Dropout(dropout_rate) 263 | def call(self, inputs, training, mask): 264 | # multi head attention (encoder时Q = K = V) 265 | att_output, _ = self.mha(inputs, inputs, inputs, mask) 266 | att_output = self.dropout1(att_output, training=training) 267 | output1 = self.layernorm1(inputs + att_output) # shape=[batch_size, seq_len, d_model] 268 | # feed forward network 269 | ffn_output = self.ffn(output1) 270 | ffn_output = self.dropout2(ffn_output, training=training) 271 | output2 = self.layernorm2(output1 + ffn_output) # shape=[batch_size, seq_len, d_model] 272 | return output2 273 | 274 | class Encoder(tf.keras.layers.Layer): 275 | def __init__(self, d_model, num_layers, num_heads, dff, 276 | input_vocab_size, max_seq_len, dropout_rate=0.1): 277 | super(Encoder, self).__init__() 278 | self.num_layers = num_layers 279 | self.d_model = d_model 280 | self.emb = tf.keras.layers.Embedding(input_vocab_size, d_model) # shape=[batch_size, seq_len, d_model] 281 | self.pos_encoding = positional_encoding(max_seq_len, d_model) # shape=[1, max_seq_len, d_model] 282 | self.encoder_layer = [EncoderLayer(d_model, num_heads, dff, dropout_rate) 283 | for _ in range(num_layers)] 284 | self.dropout = tf.keras.layers.Dropout(dropout_rate) 285 | def call(self, inputs, training, mask): 286 | # 输入部分;inputs shape=[batch_size, seq_len] 287 | seq_len = inputs.shape[1] # 句子真实长度 288 | word_embedding = self.emb(inputs) # shape=[batch_size, seq_len, d_model] 289 | word_embedding *= tf.math.sqrt(tf.cast(self.d_model, tf.float32)) 290 | emb= word_embedding + self.pos_encoding[:, :seq_len, :] 291 | x = self.dropout(emb, training=training) 292 | for i in range(self.num_layers): 293 | x = self.encoder_layer[i](x, training, mask) 294 | return x # shape=[batch_size, seq_len, d_model] 295 | 296 | # 编码器测试 297 | sample_encoder = Encoder(512, 2, 8, 1024, 5000, 200) 298 | sample_encoder_output = sample_encoder(tf.random.uniform((64, 120)), False, None) 299 | print(sample_encoder_output.shape) 300 | ``` 301 | 302 | ## 1.7 Decoder层 303 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191130225948981.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMDc5MDIz,size_16,color_FFFFFF,t_70) 304 | 上图是transformer的一个详细结构,相比本文一开始结束的结构图会更详细些,接下来,我们会按照这个结构图讲解下decoder部分。 305 | 可以看到decoder部分其实和encoder部分大同小异,不过在最下面额外多了一个masked mutil-head attetion,这里的mask也是transformer一个很关键的技术,我们一起来看一下。 306 | ### Mask 307 | mask 表示掩码,它对某些值进行掩盖,使其在参数更新时不产生效果。Transformer 模型里面涉及两种 mask,分别是 padding mask 和 sequence mask。 308 | 其中,padding mask 在所有的 scaled dot-product attention 里面都需要用到,而 sequence mask 只有在 decoder 的 self-attention 里面用到。 309 | ### Padding Mask 310 | 什么是 padding mask 呢?因为每个批次输入序列长度是不一样的也就是说,我们要对输入序列进行对齐。具体来说,就是给在较短的序列后面填充 0。但是如果输入的序列太长,则是截取左边的内容,把多余的直接舍弃。因为这些填充的位置,其实是没什么意义的,所以我们的attention机制不应该把注意力放在这些位置上,所以我们需要进行一些处理。 311 | 具体的做法是,把这些位置的值加上一个非常大的负数(负无穷),这样的话,经过 softmax,这些位置的概率就会接近0! 312 | 而我们的 padding mask 实际上是一个张量,每个值都是一个Boolean,值为 false 的地方就是我们要进行处理的地方。 313 | ### Sequence mask 314 | 文章前面也提到,sequence mask 是为了使得 decoder 不能看见未来的信息。也就是对于一个序列,在 time_step 为 t 的时刻,我们的解码输出应该只能依赖于 t 时刻之前的输出,而不能依赖 t 之后的输出。因此我们需要想一个办法,把 t 之后的信息给隐藏起来。 315 | 那么具体怎么做呢?也很简单:产生一个上三角矩阵,上三角的值全为0。把这个矩阵作用在每一个序列上,就可以达到我们的目的。 316 | 对于 decoder 的 self-attention,里面使用到的 scaled dot-product attention,同时需要padding mask 和 sequence mask 作为 attn_mask,具体实现就是两个mask相加作为attn_mask。 317 | 其他情况,attn_mask 一律等于 padding mask。 318 | 编码器通过处理输入序列启动。然后将顶部编码器的输出转换为一组注意向量k和v。每个解码器将在其“encoder-decoder attention”层中使用这些注意向量,这有助于解码器将注意力集中在输入序列中的适当位置: 319 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/201911302301063.gif) 320 | 完成编码阶段后,我们开始解码阶段。解码阶段的每个步骤从输出序列(本例中为英语翻译句)输出一个元素。 321 | 以下步骤重复此过程,一直到达到表示解码器已完成输出的符号。每一步的输出在下一个时间步被送入底部解码器,解码器像就像我们对编码器输入所做操作那样,我们将位置编码嵌入并添加到这些解码器输入中,以表示每个字的位置。 322 | 323 | 整个Decoder 过程代码实现实例如下: 324 | ```javascript 325 | # padding mask 326 | def create_padding_mask(seq): 327 | '''为了避免输入中padding的token对句子语义的影响,需要将padding位mark掉, 328 | 原来为0的padding项的mask输出为1; encoder和decoder过程都会用到''' 329 | seq = tf.cast(tf.math.equal(seq, 0), tf.float32) 330 | # 扩充维度以便于使用attention矩阵;seq输入shape=[batch_size, seq_len];输出shape=[batch_siz, 1, 1, seq_len] 331 | return seq[:, np.newaxis, np.newaxis, :] 332 | 333 | # look-ahead mask 334 | def create_look_ahead_mask(size): 335 | '''用于对未预测的token进行掩码 这意味着要预测第三个单词,只会使用第一个和第二个单词。 336 | 要预测第四个单词,仅使用第一个,第二个和第三个单词,依此类推。只有decoder过程用到''' 337 | # 产生一个上三角矩阵,上三角的值全为0。把这个矩阵作用在每一个序列上,就可以达到我们的目的。 338 | mask = 1 - tf.linalg.band_part(tf.ones((size, size)), -1, 0) 339 | return mask # shape=[seq_len, seq_len] 340 | 341 | def create_mask(inputs, targets): 342 | # 编码器只有padding_mask 343 | encoder_padding_mask = create_padding_mask(inputs) 344 | # 解码器decoder_padding_mask,用于第二层multi-head attention 345 | decoder_padding_mask = create_padding_mask(inputs) 346 | # seq_mask mask掉未预测的词 347 | seq_mask = create_look_ahead_mask(tf.shape(targets)[1]) 348 | # decoder_targets_padding_mask 解码层的输入padding mask 349 | decoder_targets_padding_mask = create_padding_mask(targets) 350 | # 合并解码层mask,用于第一层masked multi-head attention 351 | look_ahead_mask = tf.maximum(decoder_targets_padding_mask, seq_mask) 352 | return encoder_padding_mask, look_ahead_mask, decoder_padding_mask 353 | 354 | ''' 355 | decoder layer: 356 | 每个编码层包含以下子层: - Masked muti-head attention(带padding掩码和look-ahead掩码 357 | - Muti-head attention(带padding掩码)value和key来自encoder输出, 358 | query来自Masked muti-head attention层输出 - Point wise feed forward network 359 | 每个子层中都有残差连接,并最后通过一个正则化层。残差连接有助于避免深度网络中的梯度消失问题。 360 | 每个子层输出是LayerNorm(x + Sublayer(x)),规范化是在d_model维的向量上。Transformer一共有n个解码层。 361 | 当Q从解码器的第一个注意块接收输出,并且K接收编码器输出时,注意权重表示基于编码器输出给予解码器输入的重要性。 362 | 换句话说,解码器通过查看编码器输出并自我关注其自己的输出来预测下一个字。 363 | ps:因为padding在后面所以look-ahead掩码同时掩padding 364 | ''' 365 | class DecoderLayer(tf.keras.layers.Layer): 366 | def __init__(self, d_model, num_heads, dff, dropout_rate=0.1): 367 | super(DecoderLayer, self).__init__() 368 | self.mha1 = MultiHeadAttention(d_model, num_heads) 369 | self.mha2 = MultiHeadAttention(d_model, num_heads) 370 | self.ffn = point_wise_feed_forward(d_model, dff) 371 | self.layernorm1 = LayerNormalization() 372 | self.layernorm2 = LayerNormalization() 373 | self.layernorm3 = LayerNormalization() 374 | self.dropout1 = tf.keras.layers.Dropout(dropout_rate) 375 | self.dropout2 = tf.keras.layers.Dropout(dropout_rate) 376 | self.dropout3 = tf.keras.layers.Dropout(dropout_rate) 377 | def call(self, inputs, encoder_out, training, look_ahead_mask, padding_mask): 378 | # masked multi-head attention: Q = K = V 379 | att_out1, att_weight1 = self.mha1(inputs, inputs, inputs, look_ahead_mask) 380 | att_out1 = self.dropout1(att_out1, training=training) 381 | att_out1 = self.layernorm1(inputs + att_out1) 382 | # multi-head attention: Q=att_out1, K = V = encoder_out 383 | att_out2, att_weight2 = self.mha2(att_out1, encoder_out, encoder_out, padding_mask) 384 | att_out2 = self.dropout2(att_out2, training=training) 385 | att_out2 = self.layernorm2(att_out1 + att_out2) 386 | # feed forward network 387 | ffn_out = self.ffn(att_out2) 388 | ffn_out = self.dropout3(ffn_out, training=training) 389 | output = self.layernorm3(att_out2 + ffn_out) 390 | return output, att_weight1, att_weight2 391 | 392 | class Decoder(tf.keras.layers.Layer): 393 | def __init__(self, d_model, num_layers, num_heads, dff, 394 | target_vocab_size, max_seq_len, dropout_rate=0.1): 395 | super(Decoder, self).__init__() 396 | self.seq_len = tf.shape 397 | self.d_model = d_model 398 | self.num_layers = num_layers 399 | self.word_embedding = tf.keras.layers.Embedding(target_vocab_size, d_model) 400 | self.pos_encoding = positional_encoding(max_seq_len, d_model) 401 | self.decoder_layers = [DecoderLayer(d_model, num_heads, dff, dropout_rate) 402 | for _ in range(num_layers)] 403 | self.dropout = tf.keras.layers.Dropout(dropout_rate) 404 | def call(self, inputs, encoder_out, training, look_ahead_mask, padding_mask): 405 | seq_len = inputs.shape[1] 406 | attention_weights = {} 407 | word_embedding = self.word_embedding(inputs) 408 | word_embedding *= tf.math.sqrt(tf.cast(self.d_model, tf.float32)) 409 | emb = word_embedding + self.pos_encoding[:, :seq_len, :] 410 | x = self.dropout(emb, training=training) 411 | for i in range(self.num_layers): 412 | x, att1, att2 = self.decoder_layers[i](x, encoder_out, training, 413 | look_ahead_mask, padding_mask) 414 | attention_weights['decoder_layer{}_att_w1'.format(i+1)] = att1 415 | attention_weights['decoder_layer{}_att_w2'.format(i + 1)] = att2 416 | return x, attention_weights 417 | 418 | # 解码器测试 419 | sample_decoder = Decoder(512, 2, 8, 1024, 5000, 200) 420 | sample_decoder_output, attn = sample_decoder(tf.random.uniform((64, 100)), 421 | sample_encoder_output, False, None, None) 422 | print(sample_decoder_output.shape) 423 | print(attn['decoder_layer1_att_w2'].shape) 424 | ``` 425 | 426 | ## 1.8 Transformer 427 | 当decoder层全部执行完毕后,怎么把得到的向量映射为我们需要的词呢,很简单,只需要在结尾再添加一个全连接层和softmax层,假如我们的词典是1w个词,那最终softmax会输入1w个词的概率,概率值最大的对应的词就是我们最终的结果。 428 | 429 | 最后整个transformer 过程代码实现实例如下: 430 | ```javascript 431 | '''Transformer包含编码器、解码器和最后的线性层,解码层的输出经过线性层后得到Transformer的输出''' 432 | class Transformer(tf.keras.Model): 433 | def __init__(self, d_model, num_layers, num_heads, dff, 434 | input_vocab_size, target_vocab_size, max_seq_len, dropout_rate=0.1): 435 | super(Transformer, self).__init__() 436 | self.encoder = Encoder(d_model, num_layers, num_heads, dff, input_vocab_size, max_seq_len, dropout_rate) 437 | self.decoder = Decoder(d_model, num_layers, num_heads, dff, target_vocab_size, max_seq_len, dropout_rate) 438 | self.final_layer = tf.keras.layers.Dense(target_vocab_size) 439 | def call(self, inputs, targets, training, encoder_padding_mask, 440 | look_ahead_mask, decoder_padding_mask): 441 | # 首先encoder过程,输出shape=[batch_size, seq_len_input, d_model] 442 | encoder_output = self.encoder(inputs, training, encoder_padding_mask) 443 | # 再进行decoder, 输出shape=[batch_size, seq_len_target, d_model] 444 | decoder_output, att_weights = self.decoder(targets, encoder_output, training, 445 | look_ahead_mask, decoder_padding_mask) 446 | # 最后映射到输出层 447 | final_out = self.final_layer(decoder_output) # shape=[batch_size, seq_len_target, target_vocab_size] 448 | return final_out, att_weights 449 | 450 | # transformer测试 451 | sample_transformer = Transformer( 452 | num_layers=2, d_model=512, num_heads=8, dff=1024, 453 | input_vocab_size=8500, target_vocab_size=8000, max_seq_len=120 454 | ) 455 | temp_input = tf.random.uniform((64, 62)) 456 | temp_target = tf.random.uniform((64, 26)) 457 | fn_out, att = sample_transformer(temp_input, temp_target, training=False, 458 | encoder_padding_mask=None, 459 | look_ahead_mask=None, 460 | decoder_padding_mask=None, 461 | ) 462 | print(fn_out.shape) 463 | print(att['decoder_layer1_att_w1'].shape) 464 | print(att['decoder_layer1_att_w2'].shape) 465 | ``` 466 | 467 | 至此,整个Transformer的构建过程结束! 468 | --------------------------------------------------------------------------------