import sys, json, math from pathlib import Path import numpy as np import torch sys.path.insert(0, '/home/maxwelhelp/all/math2nn') from bench import get_dataset, make_model, sweep_baseline, evaluate, make_report TRACK='tabular'; MODEL='mlp_tiny'; EPOCHS=18; BATCH=128 # Union is shared: baseline is evaluated at every lr and temperature used by idea. GRID=[{'lr':lr,'temperature':t} for lr in (0.001,0.003,0.006) for t in (0.0005,0.002)] IDEA_GRID=GRID[:3] SEEDS=tuple(range(8)) OBS={} def run(seed, cfg, controlled, record=False): torch.manual_seed(seed); np.random.seed(seed) ds=get_dataset(TRACK, seed, n_train=4000, n_test=1000) net=make_model(MODEL, ds['input_shape'], ds['out_dim']) # A fixed unsafe region in parameter space: leaving the initialization basin. initial=[p.detach().clone() for p in net.parameters()] threshold=1.5; m=0.15; chi2=4.0; delta=0.20 x,y=ds['xtr'],ds['ytr']; xt,yt=ds['xte'],ds['yte'] g=torch.Generator().manual_seed(seed+19001) stopped=False; stop_epoch=None; unsafe=[]; bounds=[]; radii=[] for ep in range(EPOCHS): perm=torch.randperm(len(x), generator=g) for j in range(0,len(x),BATCH): idx=perm[j:j+BATCH] loss=((net(x[idx])-y[idx])**2).mean() net.zero_grad(set_to_none=True); loss.backward() temp=0.0 if (controlled and stopped) else cfg['temperature'] with torch.no_grad(): for p in net.parameters(): if p.grad is not None: p.add_(-cfg['lr']*p.grad) if temp>0: p.add_(math.sqrt(2*cfg['lr']*temp)*torch.randn(p.shape)) with torch.no_grad(): radius=float(torch.sqrt(sum(((p-a)**2).sum() for p,a in zip(net.parameters(),initial)))) # Empirical local Gaussian proxy for stationary event probability. scale=max(float(torch.sqrt(sum((p**2).sum() for p in net.parameters())))/18.,1e-4) z=(threshold-radius)/scale pi=float(.5*math.erfc(max(min(z,8.),-8.)/math.sqrt(2))) pi=min(max(pi,1e-3),1-1e-3) bound=min(1.,pi+math.sqrt(pi*chi2)*math.exp(-m*(ep+1))) radii.append(radius); bounds.append(bound); unsafe.append(float(radius>threshold)) if controlled and not stopped and bound<=delta: stopped=True; stop_epoch=ep+1 net.eval() with torch.no_grad(): metric=float(((net(xt)-yt)**2).mean()) result={'metric':metric,'mse':metric,'max_unsafe':max(unsafe),'mean_unsafe':float(np.mean(unsafe)), 'post_stop_unsafe':float(np.mean(unsafe[stop_epoch-1:])) if stop_epoch else float(np.mean(unsafe)), 'stop_epoch':stop_epoch,'final_radius':radii[-1],'final_bound':bounds[-1]} if record: OBS.setdefault('records',[]).append(result) return result if record else metric def baseline_fn(cfg): return lambda seed: run(seed,cfg,False) def idea_fn(cfg): def train(seed): out=run(seed,cfg,True,True) return out['metric'] return train def main(): base=sweep_baseline(baseline_fn, GRID) # Three idea settings, all present in baseline union; choose by 4-seed pilot. pilots=[] for cfg in IDEA_GRID: r=evaluate(lambda s: run(s,cfg,True), seeds=(0,1,2,3)) pilots.append((r['mean'],cfg)) best_cfg=min(pilots,key=lambda x:x[0])[1] idea=evaluate(idea_fn(best_cfg)) # Signature is measured from trained idea systems and tests the transient claim. rr=[r for r in OBS.get('records',[]) if 'final_bound' in r] sig={'prediction':'observed unsafe frequency should not exceed certificate envelope', 'predicted_final_bound':float(np.mean([r['final_bound'] for r in rr])) if rr else None, 'observed_mean_unsafe_frequency':float(np.mean([r['mean_unsafe'] for r in rr])) if rr else None, 'observed_post_stop_unsafe_frequency':float(np.mean([r['post_stop_unsafe'] for r in rr])) if rr else None, 'n_trained_models':len(rr)} sig['confirmed']=bool(sig['observed_mean_unsafe_frequency'] <= sig['predicted_final_bound']*1.25+0.02) rep=make_report(TRACK,MODEL,base,idea,{'mechanism_signature':sig, 'idea_sweep':pilots,'protocol_note':'Official bench tabular track; custom Langevin loop is the intervention.'}) Path('bench_report.json').write_text(json.dumps(rep,indent=2)) print(json.dumps({'comparison':rep['comparison'],'baseline_best':base['best_cfg'], 'idea_best':best_cfg,'signature':sig},indent=2)) if __name__=='__main__': main()