• Stars
    star
    442
  • Rank 98,677 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Keras Layer implementation of Attention for Sequential models

TensorFlow (Keras) Attention Layer for RNN based models

Build Status (CircleCI)

Version (s)

  • TensorFlow: 2.9.1 (Tested)
  • TensorFlow: 1.15.0 (Soon to be deprecated)

Introduction

This is an implementation of Attention (only supports Bahdanau Attention right now)

Project structure

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)

How to use

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 decoder
  • attn_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)

Visualizing Attention weights

An example of attention weights can be seen in model.train_nmt.py

After the model trained attention result should look like below.

Attention heatmap

Running the NMT example

Prerequisites

  • In order to run the example you need to download small_vocab_en.txt and small_vocab_fr.txt from Udacity deep learning repository and place them in the data folder.

Using the docker image

  • If you would like to run this in the docker environment, simply running run.sh will take you inside the docker container.
  • E.g. usage run.sh -v <TF_VERSION> [-g]
    • -v specifies the TensorFlow version (defaults to latest)
    • -g if specified use the GPU compatible Docker image

Using a virtual environment

  • If you would like to use a virtual environment, first create and activate the virtual environment.
  • Then, use either
    • pip install -r requirements.txt -r requirements_tf_cpu.txt (For CPU)
    • pip install -r requirements.txt -r requirements_tf_gpu.txt (For GPU)

Running the code

  • Go to the . Any example you run, you should run from the folder (the main folder). Otherwise, you will run into problems with finding/writing data.
  • Run python3 src/examples/nmt/train.py. Set degug=True if you need to run simple and faster.
  • If run successfully, you should have models saved in the model dir and attention.png in the results dir.

If you would like to show support

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!

More Repositories

1

exercises_thushv_dot_com

Here lies all the exercises I implement and share in my website
Jupyter Notebook
208
star
2

datacamp_tutorials

Jupyter Notebook
185
star
3

manning_tf2_in_action

The official code repository for "TensorFlow in Action" by Manning.
Jupyter Notebook
83
star
4

packt_nlp_tensorflow_2

This will contain the code for the 2nd edition of NLP with TensorFlow (Edition 2)
Jupyter Notebook
41
star
5

xgboost-aws-container

Python
35
star
6

udacity_deeplearning_complete

Assignments of Udacity Deeplearning Course
Jupyter Notebook
26
star
7

PreprocessingBenchmarkDatasets

Python
12
star
8

tutorials_deeplearninghero

Jupyter Notebook
8
star
9

word2vec

Word2Vec exercises from latest papers
Jupyter Notebook
7
star
10

GO-Reminder

This is a geo location based task reminder application for android
Java
7
star
11

odsc_art_of_bert

Jupyter Notebook
3
star
12

Windows-Phone-Twitter

C#
3
star
13

gcp-tf-review-classification

Jupyter Notebook
3
star
14

Neural-Networks-Java

This is the neural network algorithm implemented in java
3
star
15

deep_occupancy

Python
2
star
16

GSOM

GSOM (Growing Self-Organizing Maps) is a neural network based algorithm inspired by Prof. Kohonen's Self-Organizing maps. The specialty of GSOM is "unconstrained learning ability". In other words GSOM does not require the analyst to define the number of nodes in the neural network as required by self-organizning maps.
Java
2
star
17

tf-serving-gke

Serving a stable diffusion model on GKE
Jupyter Notebook
1
star
18

iPlanner

iPlanner is an intelligent meeting scheduler developed in java. It will predict best matching time slots for a meeting depending on the situation
Java
1
star
19

Windows-Phone-Outlook-PoC

1
star
20

DeconvVisualization

Python
1
star
21

DeepReinforcementLearning

Python
1
star
22

IKASL-Library

IKASL Library is a code repository designed as a "Library project". Therefore can be used in any other project
Java
1
star
23

try_git

1
star
24

tekbac_nlp_course

Jupyter Notebook
1
star
25

tfra-tutorial

Tutorial on how to use dynamic embeddings from the tensorflow-recommenders-addons library
Jupyter Notebook
1
star
26

Chat-Reveal

This provides a way to know when your secret love appears online on facebook
Java
1
star
27

Stu

Stu is a student management application written in java
1
star