Datasets:
Formats:
parquet
Languages:
English
Size:
1K - 10K
Tags:
tabular
datasets
optimization
numerical-linear-algebra
meta-learning
recursive-self-improvement
License:
Download reproducibility/endogenous.py from PureOne/Xevarin-RSI: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/datasets/PureOne/Xevarin-RSI/resolve/main/reproducibility/endogenous.py
- Command line
-
hf download hf://datasets/PureOne/Xevarin-RSI/reproducibility/endogenous.py
-
curl -L -o endogenous.py https://huggingface.co/datasets/PureOne/Xevarin-RSI/resolve/main/reproducibility/endogenous.py
2.58 kB
| """Endogenous training test. A learned curvature subspace persists along Newton iterates.""" | |
| import numpy as np | |
| from scipy.optimize import minimize | |
| from scipy.special import expit | |
| from xevarin import ResidualMeter, fit | |
| def experiment(x, y, xt, yt, rng_for, assess): | |
| n,d=x.shape | |
| k=4 | |
| v=np.linalg.svd(x,full_matrices=False)[2][:k].T | |
| emb=x@v | |
| y=(y>=5).astype(float) | |
| yt=(yt>=5).astype(float) | |
| base=.015*np.eye(d)+.025*x.T@x/n | |
| def evaluate(theta): | |
| z=emb@(v.T@theta) | |
| p=expit(z) | |
| objective=float(np.mean(np.logaddexp(0,z)-y*z)+.5*theta@base@theta) | |
| grad=base@theta+v@(emb.T@(p-y)/n) | |
| hess=base+v@((emb.T*(p*(1-p)))@emb/n)@v.T | |
| return objective,grad,hess | |
| theta=np.zeros(d) | |
| exact=np.zeros(d) | |
| parent=np.linalg.solve(base,np.eye(d)) | |
| lm=rm=None | |
| rows=[] | |
| stop_tol=1e-9 | |
| for j in range(1,16): | |
| loss,grad,h=evaluate(theta) | |
| if np.linalg.norm(grad)<stop_tol: | |
| break | |
| meter=ResidualMeter(h,parent) | |
| model=fit(meter.block,d,k,rng_for(7,0,10*j),lm,rm) | |
| child=model.compile(parent) | |
| ok,na=assess(h,child,rng_for(7,0,10*j+1),j,tau=.03) | |
| assert ok | |
| _,ge,he=evaluate(exact) | |
| exact-=np.linalg.solve(he,ge) | |
| theta-=child@grad | |
| new_loss,new_g,_=evaluate(theta) | |
| rows.append(dict(generation=j,calibration=meter.count,audit=na,loss_before=loss,loss_after=new_loss, | |
| grad_after=float(np.linalg.norm(new_g)),direct_step_gap=float(np.linalg.norm(theta-exact)), | |
| test_accuracy=float(np.mean(((xt@v)@(v.T@theta)>=0)==yt)), | |
| change_operator=float(np.linalg.norm(np.eye(d)-h@parent,2)))) | |
| parent=child | |
| lm,rm=model.left,model.right | |
| # Gradient-only, public-data comparator. The objective/gradient below does not compute a Hessian. | |
| def value_grad(theta): | |
| z=emb@(v.T@theta) | |
| p=expit(z) | |
| return (float(np.mean(np.logaddexp(0,z)-y*z)+.5*theta@base@theta), | |
| base@theta+v@(emb.T@(p-y)/n)) | |
| baseline=minimize(value_grad,np.zeros(d),jac=True,method='L-BFGS-B', | |
| options=dict(gtol=1e-10,ftol=1e-15,maxiter=200)) | |
| return dict(rank=k,dimension=d,tol=stop_tol,steps=rows, | |
| lbfgs=dict(iterations=int(baseline.nit),function_gradient_calls=int(baseline.nfev), | |
| objective=float(baseline.fun),grad_norm=float(np.linalg.norm(baseline.jac)), | |
| test_accuracy=float(np.mean(((xt@v)@(v.T@baseline.x)>=0)==yt)))) | |