File size: 2,720 Bytes
ae1ed83 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #!/usr/bin/env python3
"""Phase A - E-pre: ECHO target signal analysis (feeds E00-E02).
Zero-training analysis of the ECHO target R = Z(t+H) - Z(t) in the
frozen pooled feature space (main camera), for horizons H in
{1, 4, 8, 16} control steps, over all valid (episode, t) pairs of
the feature cache with a stride of 4.
Reports per horizon:
- mean |R| (signal magnitude) and the Huber loss (delta = 1) of the
zero-residual prediction, which is the baseline ECHO must beat;
- spatial concentration: mean share of total |R| held by the top 8
of 64 patches;
- Pearson r between per-sample |R| and the EE displacement
|state[t+H] - state[t]| - does the residual track real motion?
"""
import json
from pathlib import Path
import torch
EXP = Path.home() / "sera_v4_experiments"
OUT = EXP / "trace_validation" / "results" / "epre_echo.json"
HORIZONS = [1, 4, 8, 16]
def main():
cache = torch.load(EXP / "trace_vla" / "feature_cache.pt",
weights_only=False)
feats, states, episodes = cache["features"], cache["states"], \
cache["episodes"]
report = {}
for h in HORIZONS:
mags, hubers, concs, motions = [], [], [], []
for ep in episodes:
s, L = ep["start"], ep["length"]
z = feats[s:s + L, 0].float() # [L, 64, 768]
st = states[s:s + L]
for t in range(0, L - h, 4):
r = (z[t + h] - z[t]) # [64, 768]
a = r.abs()
mags.append(float(a.mean()))
hub = torch.where(a <= 1.0, 0.5 * r ** 2,
a - 0.5).mean()
hubers.append(float(hub))
per_patch = a.mean(-1) # [64]
top8 = per_patch.topk(8).values.sum()
concs.append(float(top8 / per_patch.sum().clamp(min=1e-9)))
motions.append(float((st[t + h, :3] - st[t, :3])
.norm()))
mags_t = torch.tensor(mags)
mot_t = torch.tensor(motions)
r_pearson = float(torch.corrcoef(
torch.stack([mags_t, mot_t]))[0, 1])
report[f"H{h}"] = {
"n_samples": len(mags),
"mean_abs_residual": round(float(mags_t.mean()), 5),
"zero_baseline_huber": round(float(torch.tensor(hubers).mean()), 5),
"top8_patch_share": round(float(torch.tensor(concs).mean()), 3),
"pearson_absres_vs_ee_motion": round(r_pearson, 3),
}
print(f"H{h}", report[f"H{h}"])
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_text(json.dumps(report, indent=1))
print("saved", OUT)
if __name__ == "__main__":
main()
|