# Gated cosine EEG–envelope match on stem_ms96

One fine-tune of the published `stem_ms96` logit stem. The four sw2 classifier logits, prep `eaP_n5_t60`, and the stem weights stay in place. A new branch maps the EEG and each speaker envelope with causal convolutions (kernel 17, 16 channels, no bias) into one space, takes the time-wise cosine at each 0.25 s decision, and adds speaker A minus speaker B through a scalar gate that starts at 0. Step 0 therefore matches the loaded stem. Corpus envelopes use the same synthetic column swaps as the classifier folds. AASD and Carta keep their real envelopes.

No all-five stem checkpoint exists. The external model starts from `stem_ms96__held-AVGC.pt` and is then fine-tuned on all five corpus sets. Each LODO fold starts from `stem_ms96__held-<dataset>.pt` and trains on the other four. 1000 steps, batch 512. The stem learning rate is 2e-5, the match 1e-3, the gate 1e-2. HMM is the pre-specified pair p_stay 0.994, emission scale 0.8. Emissions for a held-out corpus set come from that fold's other four datasets. Emissions for AASD and Carta come from the all-five model's corpus streams. No test-label tuning.

The residual `env_xattn` run and the envelope-only attention run were not resumed.

## Forward check

CPU/GPU forward, no dataset-wide train, real KUL window, stem checkpoint `stem_ms96__held-AVGC.pt`.

| Check | Value |
|---|---:|
| Gate 0, max abs logit difference vs the unmodified stem | 0 |
| Gate forced to 1, mean abs logit change when the EEG is zeroed | 0.2552 |
| Speaker-swap residual of the match | 0 |
| Future-sample residual of the match | 0 |

Zeroing the EEG removes the match term. The change is the match itself and is above 0.05.

## Step 50

Held-out AVGC, fresh train batches, eval mode. Loss 0.2593 versus the frozen stem 0.2604. Gate 0.079. The loss is far below 0.693, so the run continued. Final EMA gates are about 0.3 in magnitude on every fold.

## Command

```bash
cd /root/disk_3tb/eeg-thao
export PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 OMP_NUM_THREADS=8 MKL_NUM_THREADS=8
flock -n /root/disk_3tb/eeg-thao/multi_agent_opt/gpu.lock \
  /root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/.venv/bin/python -u stem_envelope_match.py
```

Code: `stem_envelope_match.py`. Log: `experiments/stem_envelope_match.log`. Scores: `results/research/stem_envelope_match.json`. Checkpoints: `models/stem_envelope_match/held-{AVGC,DTU,KUL,UHD,USTC}.pt` and `all5.pt`. The stem checkpoints under `models/logit_tf_v2/` are unchanged.

## Metrics

H is the official harmonic mean (fraction). Acc is subject-mean sample accuracy after the HMM (percent). This run uses p_stay 0.994 and scale 0.8 on every row. `stem_ms96` numbers are the published nested LODO rows (USTC used p_stay 0.996; the other four used 0.994 / 0.8). `linear_hp30` train rows are the published official nested HMM; its AASD and Carta rows are the preset 0.994 / 0.8.

| Dataset | match H | match Acc | stem_ms96 H | stem_ms96 Acc | linear_hp30 H | linear_hp30 Acc |
|---|---:|---:|---:|---:|---:|---:|
| AVGC | 0.8004 | 74.31 | 0.7945 | 74.06 | 0.7414 | 66.48 |
| DTU | 0.9062 | 89.78 | 0.9061 | 89.85 | 0.8101 | 69.42 |
| KUL | 0.8777 | 82.40 | 0.8755 | 82.33 | 0.8266 | 72.05 |
| UHD | 0.8442 | 79.52 | 0.8407 | 79.47 | 0.7686 | 69.77 |
| USTC | 0.9185 | 86.18 | 0.9219 | 86.36 | 0.8031 | 68.33 |
| mean of 5 | 0.8694 | 82.44 | 0.8677 | 82.42 | 0.7899 | 69.21 |
| AASD | 0.6195 | 51.05 | 0.6158 | 51.54 | 0.6422 | 53.12 |
| Carta | 0.5505 | 49.36 | 0.5695 | 51.03 | 0.6520 | 60.54 |

## Open targets

AVGC moved toward both bars and still misses them: H 0.8004 versus 0.85 (stem_ms96 was 0.7945) and Acc 74.31 versus 78 (stem_ms96 was 74.06).

UHD H moved toward 0.85 and still misses it: 0.8442 versus the bar, from stem_ms96 0.8407. UHD Acc 79.52 stays above 78.

AASD accuracy is 51.05. That is farther from 60 than linear_hp30 at 53.12, and below stem_ms96 at 51.54. AASD H is 0.6195, below the 0.63 bar (linear_hp30 is 0.6422).

Carta stays with linear_hp30 (H 0.6520, Acc 60.54). This run scores Carta at H 0.5505 and Acc 49.36.
