百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
K

keras-attention

> 编程语言
开源

Keras Attention Layer (Luong 和 Bahdanau 评分)。

2.8K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Keras Attention Layer (Luong 和 Bahdanau 评分)。

# Keras Attention Layer [](https://www.tensorflow.org/) 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* ```bash pip install attention ``` ## Attention Layer ```python 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](https://arxiv.org/abs/1508.04025). - Bahdanau's additive style. Link to [paper](https://arxiv.org/abs/1409.0473). ### 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](examples/add_two_numbers.py). ## Example ``` … ``` ## Other Examples Browse [examples](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 - [Hierarchical Attention Networks for Document Classification](https://www.cs.cmu.edu/~./hovy/papers/16HLT-hierarchical-attention-networks.pdf) - [Effective Approaches to Attention-based Neural Machine Translation](https://arxiv.org/abs/1508.04025) - [Neural Machine Translation by Jointly Learning to Align and Translate](https://arxiv.org/abs/1409.0473)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonattention-mechanismattention-modeldeep-learningkeras

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言