DAY 28
0
AI & Data

## Day 28 利用transformer自己實作一個翻譯程式(十) Encoder layer

Transformer跟用attention的Seq2Seq的模型有著一樣的pattern

• 輸入的句子通過N個Encoder layer，把序列中的每一個token都標記成輸出
• Decoder用Encoder的輸出和自己的輸入來預測下一個文字

## Encoder layer

Transformer中共有N個Encoder layer

``````class EncoderLayer(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads, dff, rate=0.1):
super(EncoderLayer, self).__init__()

self.ffn = point_wise_feed_forward_network(d_model, dff)

self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)

self.dropout1 = tf.keras.layers.Dropout(rate)
self.dropout2 = tf.keras.layers.Dropout(rate)

attn_output, _ = self.mha(x, x, x, mask)  # (batch_size, input_seq_len, d_model)
attn_output = self.dropout1(attn_output, training=training)
out1 = self.layernorm1(x + attn_output)  # (batch_size, input_seq_len, d_model)

ffn_output = self.ffn(out1)  # (batch_size, input_seq_len, d_model)
ffn_output = self.dropout2(ffn_output, training=training)
out2 = self.layernorm2(out1 + ffn_output)  # (batch_size, input_seq_len, d_model)

return out2
``````
``````sample_encoder_layer = EncoderLayer(512, 8, 2048)

sample_encoder_layer_output = sample_encoder_layer(
tf.random.uniform((64, 43, 512)), False, None)

sample_encoder_layer_output.shape  # (batch_size, input_seq_len, d_model)
``````
``````TensorShape([64, 43, 512])
``````