import sys, json, math, random import numpy as np import torch import torch.nn as nn sys.path.insert(0, '/home/maxwelhelp/all/math2nn') from bench import get_dataset, make_model, train_model, make_report from bench.protocol import evaluate, sweep_baseline SEEDS=tuple(range(8)); SWEEP_SEEDS=tuple(range(4)); NTR=400; NTE=200; EPOCHS=5; BATCH=128 # CPU is selected intentionally: this workload is tiny and avoids shared-GPU fallback variability. DEVICE='cpu' def seed_all(s): random.seed(s); np.random.seed(s); torch.manual_seed(s) def estimate_gains(net,x,device): z=x[:32].detach().to(device).requires_grad_(True) out=net(z).sum(); (gr,)=torch.autograd.grad(out,z) a=gr.detach().abs().reshape(-1,8,3).mean((0,2)).cpu().numpy()+1e-8 return np.outer(a,1.0/a) def greedy_clusters(G,max_size=3): cs=[{i} for i in range(8)] while True: best=None for a in range(len(cs)): for b in range(a): if len(cs[a]|cs[b])>max_size: continue score=max((math.log(float(G[i,j])+1e-8)+math.log(float(G[j,i])+1e-8) for i in cs[a] for j in cs[b]),default=-1e99) if best is None or score>best[0]: best=(score,a,b) if best is None or best[0]