Skip to content

Instantly share code, notes, and snippets.

@bryanlimy
Created May 20, 2019 15:08
Show Gist options
  • Save bryanlimy/6d92e1329bfbd750f4d62abb34388bec to your computer and use it in GitHub Desktop.
Save bryanlimy/6d92e1329bfbd750f4d62abb34388bec to your computer and use it in GitHub Desktop.
def encoder_layer(units, d_model, num_heads, dropout, name="encoder_layer"):
inputs = tf.keras.Input(shape=(None, d_model), name="inputs")
padding_mask = tf.keras.Input(shape=(1, 1, None), name="padding_mask")
attention = MultiHeadAttention(
d_model, num_heads, name="attention")({
'query': inputs,
'key': inputs,
'value': inputs,
'mask': padding_mask
})
attention = tf.keras.layers.Dropout(rate=dropout)(attention)
attention = tf.keras.layers.LayerNormalization(
epsilon=1e-6)(inputs + attention)
outputs = tf.keras.layers.Dense(units=units, activation='relu')(attention)
outputs = tf.keras.layers.Dense(units=d_model)(outputs)
outputs = tf.keras.layers.Dropout(rate=dropout)(outputs)
outputs = tf.keras.layers.LayerNormalization(
epsilon=1e-6)(attention + outputs)
return tf.keras.Model(
inputs=[inputs, padding_mask], outputs=outputs, name=name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment