Singularity-Enriched Neural Ansatz / check_signature.py

✓✓ Beats tuned baseline

Raw ⬇ ZIP
 1import sys, os, json, numpy as np, torch
 2sys.path.insert(0,'.'); sys.path.insert(0,'/home/maxwelhelp/all/math2nn')
 3from run_bench import seed_all, make_ds, PlainMLP
 4from bench import train_model
 5seed_all(0); ds=make_ds(0); net=PlainMLP(enriched=True)
 6net,_,_=train_model(net,ds,epochs=24,lr=.006,batch=128,log=lambda *_:None)
 7net.eval()
 8r=np.logspace(-2.0,-0.7,160)
 9th=np.pi/4
10x=torch.tensor(np.c_[r*np.cos(th),r*np.sin(th)],dtype=torch.float32)
11with torch.no_grad(): p=net(x.to(next(net.parameters()).device)).detach().cpu().numpy().ravel()
12# Fit away from zeros; use local positive ray where singular mode is nonzero.
13mask=np.isfinite(p)&(np.abs(p)>1e-5)
14slope=float(np.polyfit(np.log(r[mask]),np.log(np.abs(p[mask])),1)[0])
15out=json.load(open('bench_report.json'))
16out['mechanism_signature'].update({'observed_nn_loglog_slope':slope,'slope_abs_error':abs(slope-.5),'probe_points':int(mask.sum()),'confirmed':bool(abs(slope-.5)<.15)})
17json.dump(out,open('bench_report.json','w'),indent=2)
18print(json.dumps(out['mechanism_signature'],indent=2))