keras-attention
Guides use of the Keras Attention Layer attention package for TensorFlow/Keras sequence models, save/load checks, debug-mode attention weights, and example workflow troubleshooting.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- keras-attention
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Guides use of the Keras Attention Layer attention package for TensorFlow/Keras sequence models, save/load checks, debug-mode attention weights, and example workflow troubleshooting.
- disable-model-invocation
- true
02 资源文件 8
- 参考文档references/api-reference.md5.5 KB
- 参考文档references/compatibility.md3.0 KB
- 参考文档references/examples.md5.6 KB
- 参考文档references/repo-provenance.md3.5 KB
- 参考文档references/repo-routing-metadata.json352 B
- 参考文档references/troubleshooting.md6.4 KB
- 脚本scripts/check_example_dependencies.py3.7 KB
- 脚本scripts/smoke_attention.py5.2 KB
03 SKILL.md 正文
Keras Attention Layer
Use this repo skill when a task involves the attention Python package, the
Keras Attention Layer project, from attention import Attention, Luong or
Bahdanau score functions in TensorFlow/Keras sequence models, attention-weight
visualization with KERAS_ATTENTION_DEBUG, or troubleshooting the package's
example workflows.
The package surface is small and the skill is intentionally root-only: all runtime guidance is in this skill directory, with detailed API, examples, compatibility, and troubleshooting notes in bundled references.
Quick routing
| User need | Read or run |
|---|---|
| Install and smoke-test the package | Start here, then run scripts/smoke_attention.py. |
| Add an attention layer after an RNN/LSTM/GRU | Read references/api-reference.md. |
Choose score="luong" vs score="bahdanau" or set units |
Read references/api-reference.md. |
Save and reload a model that contains Attention |
Read references/api-reference.md and run scripts/smoke_attention.py --save-format h5. |
| Enable/debug attention weights or adapt the visualization demos | Read references/examples.md and references/troubleshooting.md; run scripts/check_example_dependencies.py. |
| Decide whether a TensorFlow/Keras/Python version is compatible | Read references/compatibility.md. |
| Diagnose install/import, Keras, Graphviz, dataset, or GPU-warning issues | Read references/troubleshooting.md. |
| Check whether this skill is stale for a checkout | Read references/repo-provenance.md. |
Minimal package contract
Install from PyPI when using the public package:
python -m pip install attention
Then verify the import:
from attention import Attention
The core layer pattern is:
from tensorflow.keras import Input
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.models import Model
from attention import Attention
model_input = Input(shape=(time_steps, input_dim))
x = LSTM(64, return_sequences=True)(model_input)
x = Attention(units=32, score="luong")(x) # or score="bahdanau"
x = Dense(output_dim)(x)
model = Model(model_input, x)
Important facts to preserve when helping users:
- Distribution name:
attention; import module:attention; public class:Attention. Attention(units=128, score="luong", **kwargs)expects a 3D sequence tensor(batch_size, timesteps, input_dim)and returns(batch_size, units).- Valid score functions are exactly
"luong"and"bahdanau"; other values raiseValueError. - Put
return_sequences=Trueon the preceding recurrent layer soAttentionreceives all timesteps, not only the final state. - When loading a saved model, pass
custom_objects={"Attention": Attention}. - Set
KERAS_ATTENTION_DEBUG=1before importingattentiononly when you need intermediate attention tensors; debug mode changesAttentionso it is no longer a normal KerasLayer.
Bundled scripts
Run these scripts from the generated skill directory or address them by path from another working directory. They do not read the original repository checkout.
python scripts/smoke_attention.py --score both --save-format h5 --force-cpu
smoke_attention.py builds tiny Luong and Bahdanau models, checks prediction
shapes, verifies get_config(), and optionally save/loads the model with the
proper custom_objects mapping. Use it after installing the package or when a
TensorFlow/Keras upgrade may have changed behavior.
python scripts/check_example_dependencies.py
check_example_dependencies.py verifies optional packages and the Graphviz
dot executable needed by the visualization-oriented examples; it does not run
long training or download datasets.
Boundaries and safety
- This is a package-use skill, not a maintainer workflow. For editing lint, release, or CI configuration, use a repository-maintenance workflow instead.
- The package has no public CLI and no dataset/config schema beyond ordinary TensorFlow/Keras tensors.
- The basic bundled smoke is safe and CPU-capable. The IMDB and visualization demos are long-running, may download data or write plots, and should not be treated as default smoke tests.
- Do not point users back to source-checkout examples or scripts. Use the bundled references and scripts here as the self-contained operating context.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).