Free SKILL.md scraped from GitHub. Clone the repo or copy the file directly into your Claude Code skills directory.
npx versuz@latest install hiyenwong-ai-collection-collection-skills-attention-residualsgit clone https://github.com/hiyenwong/ai_collection.gitcp ai_collection/SKILL.MD ~/.claude/skills/hiyenwong-ai-collection-collection-skills-attention-residuals/SKILL.md---
name: attention-residuals
version: 1.0.0
description: |
注意力残差(AttnRes)方法论。改进 Transformer 注意力机制的残差连接。
提升模型性能和训练稳定性。
触发词:注意力残差、AttnRes、注意力机制、残差连接、Transformer优化、
attention residuals, attention mechanism, residual connection。
---
# Attention Residuals (AttnRes)
## 核心方法论
### 问题定义
**背景:** Transformer 中的残差连接对模型性能至关重要,但传统残差连接在注意力层存在优化问题。
**解决方案:** Attention Residuals (AttnRes) - 改进的注意力残差连接方式
---
## 关键概念
### 1. 传统残差连接
Transformer 中的标准残差连接:
$$\text{Output} = \text{LayerNorm}(x + \text{Attention}(x))$$
**问题:**
- 注意力输出与输入直接相加
- 可能导致梯度流动不稳定
- 深层网络训练困难
### 2. Attention Residuals
**核心思想:** 重新设计注意力层的残差连接方式
**优势:**
- 更好的梯度流动
- 提升训练稳定性
- 改善模型性能
---
## 技术要点
### 残差连接改进
| 方法 | 公式 | 特点 |
|------|------|------|
| 标准残差 | $x + \text{Attn}(x)$ | 简单直接 |
| Pre-Norm | $\text{LN}(x + \text{Attn}(x))$ | 训练稳定 |
| **AttnRes** | 改进的残差连接 | 最优性能 |
### 实现细节
```python
# 标准 Transformer 残差
def standard_residual(x, attention_output):
return x + attention_output
# AttnRes 改进残差
def attention_residuals(x, attention_output, scale=None):
# 改进的残差连接方式
if scale is None:
scale = learnable_parameter()
return x + scale * attention_output
```
---
## 应用场景
| 场景 | 说明 |
|------|------|
| **大语言模型** | 提升训练稳定性和性能 |
| **视觉 Transformer** | 改善图像理解能力 |
| **多模态模型** | 优化跨模态融合 |
| **长序列处理** | 增强长距离依赖建模 |
---
## 性能特点
| 指标 | 改进 |
|------|------|
| 训练稳定性 | ✅ 提升 |
| 收敛速度 | ✅ 加快 |
| 最终性能 | ✅ 提高 |
| 深层网络 | ✅ 更易训练 |
---
## 技术实现
### PyTorch 示例
```python
import torch
import torch.nn as nn
class AttentionResiduals(nn.Module):
def __init__(self, d_model, n_heads, dropout=0.1):
super().__init__()
self.attention = nn.MultiheadAttention(d_model, n_heads, dropout=dropout)
self.norm = nn.LayerNorm(d_model)
self.residual_scale = nn.Parameter(torch.ones(1))
def forward(self, x):
# 注意力计算
attn_output, _ = self.attention(x, x, x)
# AttnRes 残差连接
residual = self.residual_scale * attn_output
output = self.norm(x + residual)
return output
```
---
## 与传统方法对比
| 方法 | 训练稳定性 | 收敛速度 | 最终性能 |
|------|------------|----------|----------|
| Post-Norm | 一般 | 快 | 一般 |
| Pre-Norm | 好 | 中等 | 好 |
| **AttnRes** | 最好 | 快 | 最好 |
---
## 相关技能
- `evolutionary-prompt-learning` - 进化式提示学习
- `gnn-transformer-fusion` - 多模态数据融合
---
## 来源
- **论文:** Attention Residuals (AttnRes)
- **arXiv:** 2603.15031
- **团队:** Kimi Team
- **效用评分:** 0.92
- **学习日期:** 2026-03-21
## Activation Keywords
- 脑网络分析
- 神经科学方法
- 计算神经科学
- 脑连接建模
## Tools Used
- **read**: Read skill documentation and references
- **exec**: Run analysis scripts and data processing
- **web_fetch**: Fetch papers and resources
## Instructions for Agents
1. Read the skill documentation carefully
2. Understand the methodology and key concepts
3. Apply the techniques to the specific problem
4. Document results and insights
## Examples
```python
# Example usage of the skill methodology
# Refer to the Technical Implementation section for details
```