Apache License 2.0

项目介绍

philipperemy/keras-attention 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,采用 Apache License 2.0 许可证,目前获得 2.8k 个 Star、644 次 Fork。

仓库全名
philipperemy/keras-attention
开源许可证
Apache License 2.0
Star 数
2.8k
Fork 数
644
未关闭 Issue
2

README 摘要

Keras Attention Layer Attention Layer for Keras. Supports the score functions of Luong and Bahdanau. Tested with Tensorflow 2.8, 2.9, 2.10, 2.11, 2.12, 2.13 and 2.14 (Sep 26, 2023). Installation PyPI Attention Layer Arguments units: Integer. The number of (output) units in the attention vector ($at$). score: String. The score function $score(ht, \bar{hs})$. Possible values are luong or bahdanau. Luong's…

完整内容见 GitHub 原始 README 或本页 。

常见问题

keras-attention 用什么语言开发?

主要使用 Python 编写,采用 Apache License 2.0 许可证,目前有 2.8k 个 Star。

keras-attention 可以免费商用吗?

仓库采用 Apache License 2.0 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 keras-attention 的源码?

可以直接克隆仓库:git clone https://github.com/philipperemy/keras-attention.git,默认分支为 master;安装与运行步骤见仓库 README。

keras-attention 最近还在维护吗?

仓库最近一次更新为 3月12日,当前有 2 个未关闭 Issue;Star 数为 2.8k,可作为社区活跃度的参考。

简要分析

热度分
4174
价值分
1887
活跃状态
活跃

语言占比

Python

README

Keras Attention Layer

Downloads Downloads license dep1

Attention Layer for Keras. Supports the score functions of Luong and Bahdanau.

Tested with Tensorflow 2.8, 2.9, 2.10, 2.11, 2.12, 2.13 and 2.14 (Sep 26, 2023).

Installation

PyPI

pip install attention

Attention Layer

Attention(
    units=128,
    score='luong',
    **kwargs
)

Arguments

  • units: Integer. The number of (output) units in the attention vector ($a_t$).

  • score: String. The score function $score(h_t, \bar{h_s})$. Possible values are luong or bahdanau.

    • Luong’s multiplicative style. Link to paper.
    • Bahdanau’s additive style. Link to paper.

Input shape

3D tensor with shape (batch_size, timesteps, input_dim).

Output shape

  • 2D tensor with shape (batch_size, num_units) ($a_t$).

If you want to visualize the attention weights, refer to this example examples/add_two_numbers.py.

Example

import numpy as np
from tensorflow.keras import Input
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.models import load_model, Model

from attention import Attention


def main():
    # Dummy data. There is nothing to learn in this example.
    num_samples, time_steps, input_dim, output_dim = 100, 10, 1, 1
    data_x = np.random.uniform(size=(num_samples, time_steps, input_dim))
    data_y = np.random.uniform(size=(num_samples, output_dim))

    # Define/compile the model.
    model_input = Input(shape=(time_steps, input_dim))
    x = LSTM(64, return_sequences=True)(model_input)
    x = Attention(units=32)(x)
    x = Dense(1)(x)
    model = Model(model_input, x)
    model.compile(loss='mae', optimizer='adam')
    model.summary()

    # train.
    model.fit(data_x, data_y, epochs=10)

    # test save/reload model.
    pred1 = model.predict(data_x)
    model.save('test_model.h5')
    model_h5 = load_model('test_model.h5', custom_objects={'Attention': Attention})
    pred2 = model_h5.predict(data_x)
    np.testing.assert_almost_equal(pred1, pred2)
    print('Success.')


if __name__ == '__main__':
    main()

Other Examples

Browse examples.

Install the requirements before running the examples: pip install -r examples/examples-requirements.txt.

IMDB Dataset

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)
MAX Accuracy 88.22 88.76
AVG Accuracy 87.02 87.62
STDDEV Accuracy 0.18 0.14

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.

References

评论

暂无评论