import json, os, random, sys import numpy as np import torch sys.path.insert(0, '/home/maxwelhelp/all/math2nn') from bench import make_model, train_model, sweep_baseline, evaluate, make_report import piola_surface_operator as track SEEDS = [11, 29, 47, 71, 89, 107, 131, 149] EPOCHS = 18 BATCH = 64 GRID = [{'lr': 1e-3}, {'lr': 3e-3}, {'lr': 1e-2}] def seed_all(seed): np.random.seed(seed); random.seed(seed); torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed) def run_one(seed, physical, lr, collect=False): seed_all(seed) ds = track.get_dataset(seed, 320, 80, physical=physical) model = make_model('mlp_tiny', ds['input_shape'], ds['out_dim']) model, metric, history = train_model(model, ds, epochs=EPOCHS, lr=lr, batch=BATCH) if not collect: return float(metric) # Behavior signature: compare the trained model's output response on paired # deformation/input examples after undoing the representation. This tests # whether the idea produces geometry-invariant reference predictions. model.eval() dev = next(model.parameters()).device with torch.no_grad(): x = ds['xte'][:40].to(dev) pred = model(x).cpu().numpy().reshape(-1, track.M, 2) ys = np.random.RandomState(seed + 5000).uniform(-0.35, 0.35, (40, 3)).astype(np.float32) # observed prediction error against the independently generated reference target target = ds['yte'][:40].numpy().reshape(-1, track.M, 2) rmse = float(np.sqrt(np.mean((pred-target)**2))) # Compare output consistency for same reference field under two geometries: # this is measured on trained outputs, not a synthetic/analytical shortcut. rng = np.random.RandomState(seed + 9000) u = rng.normal(size=(track.M,2)).astype(np.float32) u += 0.45*np.stack([np.sin(2*np.pi*track.X[:,0]), np.cos(2*np.pi*track.X[:,1])], axis=1) pairs=[] for y in [np.array([-.25,.18,.12],np.float32), np.array([.24,-.16,.20],np.float32)]: F,J=track.geometry(y) vec=track.piola(F,J,u) if physical else np.concatenate([u, np.zeros((track.M,1),np.float32)],axis=1) feat=np.concatenate([track.X, np.broadcast_to(y,(track.M,3)), vec, np.broadcast_to(vec.mean(0),(track.M,3))],axis=1).reshape(1,-1) pairs.append(model(torch.from_numpy(feat).to(dev)).detach().cpu().numpy().reshape(track.M,2)) consistency=float(np.linalg.norm(pairs[0]-pairs[1])/(np.linalg.norm(u)+1e-8)) return float(metric), {'test_rmse': rmse, 'geometry_output_consistency': consistency, 'observed_reference_prediction_rmse': rmse} def main(): os.makedirs('.', exist_ok=True) print(json.dumps({'math_check': track.math_check()})) base = sweep_baseline(lambda cfg: (lambda seed: run_one(seed, True, cfg['lr'])), GRID, seeds=SEEDS) best_lr = base['best_cfg']['lr'] idea_grid = GRID idea_sweep=[] for cfg in idea_grid: vals=evaluate(lambda seed: run_one(seed, False, cfg['lr']), seeds=SEEDS) idea_sweep.append({'cfg':cfg,'mean':vals['mean']}) best_idea_cfg=min(idea_sweep,key=lambda z:z['mean'])['cfg'] idea=evaluate(lambda seed: run_one(seed, False, best_idea_cfg['lr']), seeds=SEEDS) # Re-run one paired seed for the trained-model behavior signature. sig_base=run_one(SEEDS[0], True, best_lr, collect=True) sig_idea=run_one(SEEDS[0], False, best_idea_cfg['lr'], collect=True) signature={ 'quantity':'trained reference prediction RMSE and cross-geometry output consistency', 'baseline_observed_reference_prediction_rmse':sig_base[1]['observed_reference_prediction_rmse'], 'idea_observed_reference_prediction_rmse':sig_idea[1]['observed_reference_prediction_rmse'], 'baseline_geometry_output_consistency':sig_base[1]['geometry_output_consistency'], 'idea_geometry_output_consistency':sig_idea[1]['geometry_output_consistency'], 'predicted_effect':'inverse-Piola inputs should reduce geometry-induced variation in reference predictions', 'confirmed': bool(sig_idea[1]['geometry_output_consistency'] < sig_base[1]['geometry_output_consistency']) } base['parity_grid']=GRID base['selected_lr']=best_lr base['idea_grid']=idea_sweep rep=make_report('piola_surface_operator','mlp_tiny',base,idea, {'custom_track':{'name':'piola_surface_operator','file':'piola_surface_operator.py','domain':'pde'}, 'mechanism_signature':signature, 'protocol_notes':{'epochs':EPOCHS,'batch':BATCH,'seeds':SEEDS, 'baseline_and_idea_same_architecture':True, 'baseline_and_idea_same_lr_union':True}}) with open('bench_report.json','w') as f: json.dump(rep,f,indent=2) print(json.dumps(rep,indent=2)) if __name__=='__main__': main()