(query, key, value, i)
| 191 | |
| 192 | # i is layer number 0,1,2... |
| 193 | def bert_module(query, key, value, i): |
| 194 | # Multi headed self-attention |
| 195 | attention_output = layers.MultiHeadAttention( |
| 196 | num_heads=config.NUM_HEAD, |
| 197 | key_dim=config.EMBED_DIM // config.NUM_HEAD, |
| 198 | name="encoder_{}/multiheadattention".format(i), |
| 199 | )(query, key, value) |
| 200 | attention_output = layers.Dropout(0.1, name="encoder_{}/att_dropout".format(i))(attention_output) |
| 201 | attention_output = layers.LayerNormalization( |
| 202 | epsilon=1e-6, name="encoder_{}/att_layernormalization".format(i) |
| 203 | )(query + attention_output) |
| 204 | |
| 205 | # Feed-forward layer |
| 206 | ffn = keras.Sequential( |
| 207 | [ |
| 208 | layers.Dense(config.FF_DIM, activation="relu"), |
| 209 | layers.Dense(config.EMBED_DIM), |
| 210 | ], |
| 211 | name="encoder_{}/ffn".format(i), |
| 212 | ) |
| 213 | ffn_output = ffn(attention_output) |
| 214 | ffn_output = layers.Dropout(0.1, name="encoder_{}/ffn_dropout".format(i))( |
| 215 | ffn_output |
| 216 | ) |
| 217 | sequence_output = layers.LayerNormalization( |
| 218 | epsilon=1e-6, name="encoder_{}/ffn_layernormalization".format(i) |
| 219 | )(attention_output + ffn_output) |
| 220 | return sequence_output |
| 221 | |
| 222 | def get_pos_encoding_matrix(max_len, d_emb): |
| 223 | pos_enc = np.array( |
no outgoing calls
no test coverage detected