summaryrefslogtreecommitdiff
path: root/note.md
diff options
context:
space:
mode:
authorYurenHao0426 <Blackhao0426@gmail.com>2026-02-16 14:44:42 -0600
committerYurenHao0426 <Blackhao0426@gmail.com>2026-02-16 14:44:42 -0600
commit09d50e47860da0035e178a442dc936028808a0b3 (patch)
tree9d651b0c7d289a9a0405953f2da989a3c431f147 /note.md
parentc90b48e3f8da9dd0f8d2ae82ddf977436bb0cfc3 (diff)
Add memory centering, grid search experiments, and energy visualizationsHEADmaster
- Add centering support to MemoryBank (center_query, apply_centering, mean persistence in save/load) to remove centroid attractor in Hopfield dynamics - Add center flag to MemoryBankConfig, device field to PipelineConfig - Grid search scripts: initial (β≤8), residual, high-β, and centered grids with dedup-based LLM caching (89-91% call savings) - Energy landscape visualization: 2D contour, 1D profile, UMAP, PCA heatmap comparing centered vs uncentered dynamics - Experiment log (note.md) documenting 4 rounds of results and root cause analysis of centroid attractor problem - Key finding: β_critical ≈ 37.6 for centered memory; best configs beat FAISS baseline by +3-4% F1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'note.md')
-rw-r--r--note.md181
1 files changed, 181 insertions, 0 deletions
diff --git a/note.md b/note.md
new file mode 100644
index 0000000..5abf740
--- /dev/null
+++ b/note.md
@@ -0,0 +1,181 @@
+# HAG 实验记录
+
+## 实验环境
+
+- Memory bank: HotpotQA, 1311 passages, dim=768 (Contriever-MSMARCO)
+- Generator: Llama-3.1-8B-Instruct, temperature=0 (greedy)
+- Evaluation: 100 questions, EM + F1
+- Hardware: NVIDIA RTX A6000 × 4
+
+---
+
+## Round 1: 初始 Grid Search (β ≤ 8)
+
+**日期**: 2026-02-15
+**脚本**: `scripts/run_grid_search.py`
+**Grid**: β=[0.25, 0.5, 1.0, 2.0, 3.0, 5.0, 8.0] × iter=[1,2,3,5,8,15], top_k=5
+**Dedup**: 381 LLM calls / 4300 grid evals (91.1% saving)
+
+| 方法 | EM | F1 |
+|---|---|---|
+| FAISS baseline | 0.320 | 0.438 |
+| Best HAG (β=8, iter=1) | 0.210 | 0.294 |
+
+**结论**: 全军覆没。所有 42 个 HAG 配置均低于 FAISS baseline。
+
+**问题诊断**:
+- Entropy ≈ 7.17 / 7.18 (≈log(1311)),attention 接近均匀分布
+- FAISS overlap 极低 (0.2%-6.8%)
+- iter>1 普遍更差,所有 query 被吸到同一个 centroid
+
+---
+
+## Root Cause Analysis: Centroid Attractor
+
+### 发现 1: 能量面结构问题
+
+E(q) = -1/β · logsumexp(β · qᵀM) + 1/2 · ‖q‖²
+
+| β | E(centroid) | E(memory_i) | 谁更低? |
+|---|---|---|---|
+| 1.0 | **-7.375** | -7.068 | centroid |
+| 8.0 | **-1.097** | -0.812 | centroid |
+| 50.0 | -0.357 | **-0.500** | memory |
+
+**原因**: ‖centroid‖=0.63 vs ‖memory‖=1.0,norm 惩罚项 1/2‖q‖² 让 centroid 白省 0.3 能量。β<50 时 centroid 是全局最低能量点。
+
+### 发现 2: 一步迭代即崩溃
+
+β=8 时:
+- t=0: query 正常,top3 是正确 passages
+- t=1: cos(q, centroid) = 0.993,‖q‖从 1.36 骤降到 0.64
+- t=2: cos(q, centroid) = 0.999,所有 query 收敛到同一点
+
+**机制**: softmax(β·qᵀM) 近均匀 → q_new = Σ αᵢmᵢ ≈ centroid → norm 缩小 → 下一步更均匀 → 恶性循环
+
+### 发现 3: 去掉 norm 项不够
+
+即使 normalize 每步 update(‖q‖=1),β≤8 时仍收敛到 normalized centroid 方向。因为 softmax averaging 本身就把 query 拉向 centroid 方向,与 norm 项无关。
+
+### 发现 4: Memory bank 结构
+
+- 1311 passages pairwise cosine sim: mean=0.392, std=0.065
+- Centroid 与所有 memory 的 cosine sim: mean=0.627 (很高)
+- β 需要 ≈50+ 才能让 softmax 区分 passages(entropy 从 99% 降到 10%)
+
+### 关键洞察
+
+**max_iter=1 在代码中意味着做 1 次 Hopfield 更新**(不是 0 次)。Grid search 从未测试 iter=0(纯 softmax top-k = FAISS)。这解释了为什么 "iter=1" 已经比 FAISS 差很多。
+
+---
+
+## Round 2: 20-question 方案探索
+
+在 20 个问题上快速测试 4 种修复方案(带 LLM 生成):
+
+### A. 高 β(50, 100)纯 Hopfield
+
+| 配置 | EM | F1 |
+|---|---|---|
+| FAISS | 0.300 | 0.456 |
+| β=50 iter=1 | 0.300 | 0.417 |
+| β=100 iter=1 | 0.300 | 0.417 |
+
+高 β 使初始 attention 变尖锐,但迭代后 query 仍偏移,F1 反而下降。
+
+### B. Pre-filter K + Hopfield
+
+| 配置 | EM | F1 |
+|---|---|---|
+| PreFilter K=20 → β=5 iter=1 | **0.350** | **0.500** |
+| PreFilter K=20 → β=20 iter=1 | 0.350 | 0.462 |
+| PreFilter K=50 → β=5 iter=1 | 0.350 | 0.462 |
+
+最优方案,但本质是 FAISS 初筛 + Hopfield 重排,削弱了 "用 Hopfield 替代 FAISS" 的叙事。
+
+### C. Residual Connection
+
+q_{t+1} = λ · q_t + (1-λ) · M @ softmax(β · Mᵀ · q_t)
+
+| 配置 | EM | F1 |
+|---|---|---|
+| β=20 λ=0.9 iter=3 | 0.350 | 0.473 |
+| β=50 λ=0.9 iter=3 | 0.350 | 0.473 |
+| β=50 λ=0.7 iter=3 | 0.350 | 0.433 |
+
+λ=0.9(保留 90% 原始 query)有效防止 centroid 崩塌。
+
+### D. Pre-filter + Residual
+
+| 配置 | EM | F1 |
+|---|---|---|
+| PF K=20 + β=5 λ=0.9 iter=3 | 0.350 | 0.473 |
+| PF K=50 + β=10 λ=0.9 iter=3 | 0.350 | 0.473 |
+
+没有比单独用 Residual 更好。
+
+**决策**: 选择 pure Hopfield + Residual 路线(不依赖 FAISS pre-filter)。
+
+---
+
+## Round 3: Residual Grid (100 questions)
+
+**脚本**: `scripts/eval_residual_grid.py`
+**Grid**: β=[5,10,20,50,100] × λ=[0.5,0.7,0.8,0.9,0.95] × iter=[1,3,5,8]
+**Dedup**: 1666 unique LLM calls
+
+| 排名 | 配置 | EM | F1 | FAISS overlap |
+|---|---|---|---|---|
+| - | FAISS baseline | 0.320 | 0.438 | 1.000 |
+| 1 | **β=5 λ=0.7 iter=1** | **0.360** | **0.481** | 0.902 |
+| 2 | β=5 λ=0.9 iter=3 | 0.360 | 0.481 | 0.912 |
+| 3 | β=10 λ=0.7 iter=1 | 0.360 | 0.481 | 0.900 |
+| 4 | β=10 λ=0.95 iter=8 | 0.360 | 0.480 | 0.886 |
+| 5 | β=5 λ=0.95 iter=8 | 0.350 | 0.470 | 0.886 |
+
+**55/100 configs beat FAISS F1**。Residual 路线 robust。
+
+---
+
+## Round 4: High-β Grid (100 questions)
+
+**脚本**: `scripts/eval_highbeta_grid.py`
+**Grid**: β=[20,50,100,200,500] × iter=[0,1,2,3,5,8] × mode=[standard, normalized, residual_0.9, residual_0.95]
+**Dedup**: 1379 unique LLM calls
+
+| 排名 | 配置 | EM | F1 | FAISS overlap |
+|---|---|---|---|---|
+| - | FAISS baseline | 0.320 | 0.438 | 1.000 |
+| 1 | **β=20 iter=1 standard** | **0.380** | **0.469** | 0.480 |
+| 2 | β=50 iter=1 standard | 0.360 | 0.457 | 0.508 |
+| 3 | β=20 iter=1 residual_0.9 | 0.340 | 0.455 | 0.966 |
+| 4 | β=20 iter=2 residual_0.95 | 0.340 | 0.455 | 0.966 |
+| 5 | β=500 iter=1 residual_0.9 | 0.360 | 0.455 | 0.692 |
+| 6 | β=50 iter=1 normalized | 0.370 | 0.454 | 0.464 |
+
+**31/105 configs beat FAISS F1**。Standard 高 β 也能 work 但 overlap 低(~0.5,换了一半 passages)。
+
+---
+
+## 综合结论
+
+### 最优配置
+
+| 方法 | EM | F1 | vs FAISS |
+|---|---|---|---|
+| FAISS baseline | 0.320 | 0.438 | - |
+| Residual β=5 λ=0.7 iter=1 | 0.360 | 0.481 | **+4.3% F1** |
+| Standard β=20 iter=1 | 0.380 | 0.469 | **+3.1% F1** |
+
+### 规律
+
+1. **iter=1 普遍最优**,多次迭代有害(centroid 吸引)
+2. **低 β + residual > 高 β + standard**:保守修正比激进替换好
+3. **Residual 更 robust**: 55% configs beat FAISS vs 30% for high-β
+4. **FAISS overlap 高 = 好**: 最优 residual configs overlap ≈ 0.9(只换 10% passages 即提升)
+
+### 未解决问题
+
+- 只做了 1 步迭代就最优,"iterative refinement" 的叙事受限
+- Centroid attractor 在低 β 时仍存在,需要根本性解决方案
+- 100 questions 样本量偏小,需要在 500+ 上验证