This is an implementation of Attention (only supports Bahdanau Attention right now)
data (Download data and place it here)
|--- small_vocab_en.txt
|--- small_vocab_fr.txt
src
|--- layers
|--- attention.py (Attention implementation)
|--- examples
|--- nmt
|--- model.py (NMT model defined with Attention)
|--- train.py ( Code for training/inferring/plotting attention with NMT model)
|--- nmt_bidirectional
|--- model.py (NMT birectional model defined with Attention)
|--- train.py ( Code for training/inferring/plotting attention with NMT model)
Just like you would use any other tensoflow.python.keras.layers
object.
from attention_keras.src.layers.attention import AttentionLayer
attn_layer = AttentionLayer(name='attention_layer')
attn_out, attn_states = attn_layer([encoder_outputs, decoder_outputs])
Here,
encoder_outputs
- Sequence of encoder ouptputs returned by the RNN/LSTM/GRU (i.e. with return_sequences=True
)decoder_outputs
- The above for the decoderattn_out
- Output context vector sequence for the decoder. This is to be concat with the output of decoder (refer model/nmt.py
for more details)attn_states
- Energy values if you like to generate the heat map of attention (refer model.train_nmt.py
for usage)An example of attention weights can be seen in model.train_nmt.py
After the model trained attention result should look like below.
small_vocab_en.txt
and small_vocab_fr.txt
from Udacity deep learning repository and place them in the data
folder.run.sh
will take you inside the docker container.run.sh -v <TF_VERSION> [-g]
-v
specifies the TensorFlow version (defaults to latest
)-g
if specified use the GPU compatible Docker imagepip install -r requirements.txt -r requirements_tf_cpu.txt
(For CPU)pip install -r requirements.txt -r requirements_tf_gpu.txt
(For GPU)python3 src/examples/nmt/train.py
. Set degug=True
if you need to run simple and faster.attention.png
in the results
dir.If you'd like to show your appreciation you can buy me a coffee. No stress! It's totally optional. The support I recieved would definitely an added benefit to maintain the repository and continue on my other contributions.
If you have improvements (e.g. other attention mechanisms), contributions are welcome!