Keras Simple Attention
Keras Attention Mechanism
Many-to-one attention mechanism for Keras.
Installation via pip
pip install attention
Import in the source code
from attention import Attention # [...] m = Sequential([ LSTM(128, input_shape=(seq_length, 1), return_sequences=True), Attention(), # <--------- here. Dense(1, activation='linear') ])
Install the requirements before running the examples:
pip install -r requirements.txt.
In this experiment, we demonstrate that using attention yields a higher accuracy on the IMDB dataset. We consider two LSTM networks: one with this attention layer and the other one with a fully connected layer. Both have the same number of parameters for a fair comparison (250K).
Here are the results on 10 runs. For every run, we record the max accuracy on the test set for 10 epochs.
|Measure||No Attention (250K params)||Attention (250K params)|
As expected, there is a boost in accuracy for the model with attention. It also reduces the variability between the runs, which is something nice to have.
Adding two numbers
Let's consider the task of adding two numbers that come right after some delimiters (0 in this case):
x = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8]. Result is
y = 4 + 7 = 11.
The attention is expected to be the highest after the delimiters. An overview of the training is shown below, where the top represents the attention map and the bottom the ground truth. As the training progresses, the model learns the task and the attention map converges to the ground truth.
Finding max of a sequence
We consider many 1D sequences of the same length. The task is to find the maximum of each sequence.
We give the full sequence processed by the RNN layer to the attention layer. We expect the attention layer to focus on the maximum of each sequence.
After a few epochs, the attention layer converges perfectly to what we expected.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size attention-4.0-py2.py3-none-any.whl (7.6 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
Hashes for attention-4.0-py2.py3-none-any.whl