Joel Woodfield commited on
Commit
b883adb
·
1 Parent(s): 0383d82

Fix bug in nesterov optimizer

Browse files
backend/src/optimization_logic.py CHANGED
@@ -126,7 +126,7 @@ def nesterov_univariate(
126
  else:
127
  m = momentum * (x_values[-1] - x_values[-2])
128
 
129
- x_lookahead = x - m
130
  x = x_lookahead - learning_rate * f_prime(x_lookahead)
131
 
132
  x_values.append(x)
@@ -173,8 +173,8 @@ def nesterov_bivariate(
173
  mx = momentum * (x_values[-1] - x_values[-2])
174
  my = momentum * (y_values[-1] - y_values[-2])
175
 
176
- x_lookahead = x - mx
177
- y_lookahead = y - my
178
 
179
  x = x_lookahead - learning_rate * fx(x_lookahead, y_lookahead)
180
  y = y_lookahead - learning_rate * fy(x_lookahead, y_lookahead)
@@ -300,7 +300,7 @@ def adagrad_univariate(
300
  for i in range(steps - 1):
301
  g = f_prime(x)
302
  v += g ** 2
303
- x = x - (learning_rate / (np.sqrt(v) + epsilon)) * g
304
 
305
  x_values.append(x)
306
  y_values.append(f(x))
@@ -347,8 +347,8 @@ def adagrad_bivariate(
347
  vx += gx ** 2
348
  vy += gy ** 2
349
 
350
- x = x - (learning_rate / (np.sqrt(vx) + epsilon)) * gx
351
- y = y - (learning_rate / (np.sqrt(vy) + epsilon)) * gy
352
 
353
  x_values.append(x)
354
  y_values.append(y)
@@ -387,7 +387,7 @@ def rmsprop_univariate(
387
  for i in range(steps - 1):
388
  g = f_prime(x)
389
  v = beta * v + (1 - beta) * g ** 2
390
- x = x - (learning_rate / (np.sqrt(v) + epsilon)) * g
391
 
392
  x_values.append(x)
393
  y_values.append(f(x))
@@ -435,8 +435,8 @@ def rmsprop_bivariate(
435
  vx = beta * vx + (1 - beta) * gx ** 2
436
  vy = beta * vy + (1 - beta) * gy ** 2
437
 
438
- x = x - (learning_rate / (np.sqrt(vx) + epsilon)) * gx
439
- y = y - (learning_rate / (np.sqrt(vy) + epsilon)) * gy
440
 
441
  x_values.append(x)
442
  y_values.append(y)
 
126
  else:
127
  m = momentum * (x_values[-1] - x_values[-2])
128
 
129
+ x_lookahead = x + m
130
  x = x_lookahead - learning_rate * f_prime(x_lookahead)
131
 
132
  x_values.append(x)
 
173
  mx = momentum * (x_values[-1] - x_values[-2])
174
  my = momentum * (y_values[-1] - y_values[-2])
175
 
176
+ x_lookahead = x + mx
177
+ y_lookahead = y + my
178
 
179
  x = x_lookahead - learning_rate * fx(x_lookahead, y_lookahead)
180
  y = y_lookahead - learning_rate * fy(x_lookahead, y_lookahead)
 
300
  for i in range(steps - 1):
301
  g = f_prime(x)
302
  v += g ** 2
303
+ x = x - (learning_rate / (np.sqrt(v + epsilon))) * g
304
 
305
  x_values.append(x)
306
  y_values.append(f(x))
 
347
  vx += gx ** 2
348
  vy += gy ** 2
349
 
350
+ x = x - (learning_rate / (np.sqrt(vx + epsilon))) * gx
351
+ y = y - (learning_rate / (np.sqrt(vy + epsilon))) * gy
352
 
353
  x_values.append(x)
354
  y_values.append(y)
 
387
  for i in range(steps - 1):
388
  g = f_prime(x)
389
  v = beta * v + (1 - beta) * g ** 2
390
+ x = x - (learning_rate / (np.sqrt(v + epsilon))) * g
391
 
392
  x_values.append(x)
393
  y_values.append(f(x))
 
435
  vx = beta * vx + (1 - beta) * gx ** 2
436
  vy = beta * vy + (1 - beta) * gy ** 2
437
 
438
+ x = x - (learning_rate / (np.sqrt(vx + epsilon))) * gx
439
+ y = y - (learning_rate / (np.sqrt(vy + epsilon))) * gy
440
 
441
  x_values.append(x)
442
  y_values.append(y)
dist/assets/{index-BoSq-s5V.js → index-CPl3lL5a.js} RENAMED
@@ -3843,4 +3843,4 @@ The plot is interactive:
3843
  - Click and drag to pan
3844
  - Scroll to zoom
3845
  - In bivariate mode, click **Update color scale** to rescale the heatmap so that the currently visible minimum and maximum values define the color range
3846
- `,ij={"Gradient Descent":{learningRate:"0.1",momentum:"0.0"},Nesterov:{learningRate:"0.1",momentum:"0.0"},Adam:{learningRate:"0.1",beta1:"0.9",beta2:"0.999",epsilon:"1e-8"},Adagrad:{learningRate:"0.1",epsilon:"1e-8"},RMSProp:{learningRate:"0.1",beta:"0.9",epsilon:"1e-8"},Adadelta:{beta:"0.9",epsilon:"1e-3"},Newton:{}},$6={"--Custom--":"x^2",Quadratic:"x^2","Local Minima":"x^2 - 0.1cos(20x)",Plateau:"x^4"},J6={"--Custom--":"x^2 + 3y^2",Quadratic:"x^2 + 3y^2",Small:"(0.05x)^2 + 3(0.05y)^2",Ackley:"-20exp(-0.2 sqrt(0.5(x^2 + y^2))) - exp(0.5(cos(2 pi x) + cos(2 pi y))) + e + 20",Rasteringin:"20 + (x^2 - 10cos(2 pi x)) + (y^2 - 10cos(2 pi y))",Rosenbrock:"(1 - x)^2 + 100(y - x^2)^2"};function oj({settings:Ie,setSettings:ht,onRandomInitialPoint:St,trajectoryValues:wt,onReset:lr,onNextStep:vr,onPrevStep:Lr}){const rn=["Settings","Optimize","Usage"],[xn,$r]=up.useState("Settings");function Tn(Ri,Ts){if(Ri==="algorithm"){const Si=ij[Ts];ht({...Ie,algorithm:Ts,...Si})}else ht({...Ie,[Ri]:Ts})}const[zn,xa]=up.useState("--Custom--");function Jn(Ri){xa(Ri);const Ts=Ie.mode==="Bivariate"?J6[Ri]:$6[Ri];Tn("functionExpr",Ts)}function ho(Ri){const Ts="--Custom--",Si=Ri==="Bivariate"?J6[Ts]:$6[Ts];ht({...Ie,mode:Ri,functionExpr:Si}),xa(Ts)}function Mo(Ri){return Ri&&Ri.length>0?Ri[Ri.length-1]:null}const is=Mo(wt?.x),Pi=Mo(wt?.y),Ge=Mo(wt?.derivative),ko=Mo(wt?.secondDerivative),Ml=Mo(wt?.z),xo=Mo(wt?.gradient),li=Mo(wt?.hessian);return Ro.jsxs(eM,{className:"flex flex-col h-full p-4 gap-2 min-w-0 min-h-0 overflow-auto",children:[Ro.jsx(DO,{tabs:rn,activeTab:xn,onChange:$r}),Ro.jsxs("div",{className:"flex flex-col gap-4 min-h-0 min-w-0 overflow-auto",children:[xn==="Settings"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(IO,{label:"Problem Type",options:rj,activeOption:Ie.mode,onChange:ho}),Ro.jsx(m8,{label:"Function",options:Ie.mode==="Bivariate"?Object.keys(J6):Object.keys($6),activeOption:zn,onChange:Jn}),Ro.jsx(pd,{label:"Function Expression",value:Ie.functionExpr,onChange:Ri=>Tn("functionExpr",Ri),readonly:zn!=="--Custom--",rows:3}),Ro.jsx(m8,{label:"Algorithm",options:nj,activeOption:Ie.algorithm,onChange:Ri=>Tn("algorithm",Ri)}),Ro.jsxs("div",{className:`${Ie.mode==="Bivariate"?"grid grid-cols-2 gap-2":""}`,children:[Ro.jsx(pd,{label:"Initial X",value:Ie.x0,onChange:Ri=>Tn("x0",Ri)}),Ie.mode==="Bivariate"&&Ro.jsx(pd,{label:"Initial Y",value:Ie.y0||"",onChange:Ri=>Tn("y0",Ri)})]}),Ro.jsx(ST,{label:"Random Initial Point",onClick:St}),Ro.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[["Gradient Descent","Nesterov","Adam","Adagrad","RMSProp","Adadelta"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Learning Rate",value:Ie.learningRate,onChange:Ri=>Tn("learningRate",Ri)})}),["Gradient Descent","Nesterov"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Momentum",value:Ie.momentum,onChange:Ri=>Tn("momentum",Ri)})}),Ie.algorithm==="Adam"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(pd,{label:"Beta 1",value:Ie.beta1,onChange:Ri=>Tn("beta1",Ri)}),Ro.jsx(pd,{label:"Beta 2",value:Ie.beta2,onChange:Ri=>Tn("beta2",Ri)})]}),["RMSProp","Adadelta"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Beta",value:Ie.beta,onChange:Ri=>Tn("beta",Ri)})}),["Adam","Adagrad","RMSProp","Adadelta"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Epsilon",value:Ie.epsilon,onChange:Ri=>Tn("epsilon",Ri)})})]})]}),xn==="Optimize"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsxs("div",{className:"hidden lg:flex flex-col gap-4 min-h-0 min-w-0 overflow-auto",children:[Ie.mode==="Univariate"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(pd,{label:"Current X",value:is!==null?is.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Y",value:Pi!==null?Pi.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Derivative",value:Ge!==null?Ge.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Second Derivative",value:ko!==null?ko.toFixed(4):"",readonly:!0})]}),Ie.mode==="Bivariate"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(pd,{label:"Current X",value:is!==null?is.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Y",value:Pi!==null?Pi.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Z",value:Ml!==null?Ml.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Gradient",value:xo!==null?`[${xo.map(Ri=>Ri.toFixed(4)).join(", ")}]`:"",readonly:!0}),Ro.jsx(pd,{label:"Current Hessian",value:li!==null?`[${li.map(Ri=>`[${Ri.map(Ts=>Ts.toFixed(4)).join(", ")}]`).join(", ")}]`:"",readonly:!0})]})]}),Ro.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[Ro.jsx(ST,{label:"Next Step",onClick:vr}),Ro.jsx(ST,{label:"Previous Step",onClick:Lr})]}),Ro.jsx(ST,{label:"Reset",onClick:lr})]}),xn==="Usage"&&Ro.jsx($U,{components:{h1:({children:Ri})=>Ro.jsx("h1",{className:"text-2xl font-bold mt-4 mb-2",children:Ri}),h2:({children:Ri})=>Ro.jsx("h2",{className:"text-xl font-semibold mt-4 mb-2",children:Ri}),h3:({children:Ri})=>Ro.jsx("h3",{className:"text-lg font-semibold mt-4 mb-2",children:Ri}),p:({children:Ri})=>Ro.jsx("p",{className:"leading-6 mb-3 last:mb-0",children:Ri}),ul:({children:Ri})=>Ro.jsx("ul",{className:"list-disc pl-5 mb-3",children:Ri}),ol:({children:Ri})=>Ro.jsx("ol",{className:"list-decimal pl-5 mb-3",children:Ri}),li:({children:Ri})=>Ro.jsx("li",{className:"mb-1",children:Ri})},children:aj})]})]})}function sj(Ie){return new Worker("/assets/pyodide.worker-jb0eIAfr.js",{name:Ie?.name})}function lj(Ie){const[ht,St]=up.useState({}),[wt,lr]=up.useState(!0),vr=up.useRef(null);function Lr(Tn){vr.current&&vr.current.postMessage({type:"INIT",settings:Tn})}function rn(){vr.current&&vr.current.postMessage({type:"NEXT_STEP"})}function xn(){vr.current&&vr.current.postMessage({type:"PREV_STEP"})}function $r(){vr.current&&vr.current.postMessage({type:"RESET"})}return up.useEffect(()=>{const Tn=new sj;return vr.current=Tn,Tn.onmessage=zn=>{const xa=zn.data;xa.type==="READY"?(console.log("Pyodide is ready"),lr(!1),Lr(Ie)):xa.type==="RESULT"&&St(Jn=>({functionValues:xa.data.functionValues||Jn.functionValues,trajectoryValues:xa.data.trajectoryValues||Jn.trajectoryValues}))},()=>{Tn.terminate()}},[]),{isLoading:wt,plotData:ht,sendInit:Lr,sendReset:$r,sendNextStep:rn,sendPrevStep:xn}}const uj={mode:"Univariate",functionExpr:"x^2",algorithm:"Gradient Descent",x0:"0.5",y0:"0.5",learningRate:"0.1",momentum:"0.0"},cj=[-1,1],fj=[-1,1];function hj(){const[Ie,ht]=up.useState(uj),St=up.useRef(cj),wt=up.useRef(fj),lr={...Ie,xlim:St.current,ylim:wt.current};function vr($r){ht($r),xn.sendInit({...$r,xlim:St.current,ylim:wt.current})}function Lr($r,Tn){St.current=$r,wt.current=Tn,xn.sendInit({...Ie,xlim:$r,ylim:Tn})}function rn(){const $r=St.current[1]-St.current[0],Tn=wt.current[1]-wt.current[0],zn=(St.current[0]+Math.random()*$r*.95).toFixed(2),xa=(wt.current[0]+Math.random()*Tn*.95).toFixed(2),Jn={...Ie,x0:zn,y0:xa};vr(Jn)}const xn=lj(lr);return Ro.jsxs("div",{className:"grid grid-cols-[1fr_12fr_1fr] lg:h-dvh bg-stone-50",children:[Ro.jsx("div",{}),Ro.jsxs("div",{className:"flex flex-col gap-6 p-6 min-h-0",children:[Ro.jsx("div",{className:"title",children:"Optimization Trajectory Visualizer"}),Ro.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-[2fr_1fr] gap-12 flex-1 min-h-0 overflow-auto",children:[Ro.jsx(zO,{data:xn.plotData,isLoading:xn.isLoading,xlim:St.current,ylim:wt.current,setAxisLimits:Lr}),Ro.jsx(oj,{settings:Ie,setSettings:vr,onRandomInitialPoint:rn,trajectoryValues:xn.plotData.trajectoryValues,onReset:()=>xn.sendReset(),onNextStep:()=>xn.sendNextStep(),onPrevStep:()=>xn.sendPrevStep()})]})]}),Ro.jsx("div",{})]})}AO.createRoot(document.getElementById("root")).render(Ro.jsx(up.StrictMode,{children:Ro.jsx(hj,{})}));
 
3843
  - Click and drag to pan
3844
  - Scroll to zoom
3845
  - In bivariate mode, click **Update color scale** to rescale the heatmap so that the currently visible minimum and maximum values define the color range
3846
+ `,ij={"Gradient Descent":{learningRate:"0.1",momentum:"0.0"},Nesterov:{learningRate:"0.1",momentum:"0.0"},Adam:{learningRate:"0.1",beta1:"0.9",beta2:"0.999",epsilon:"1e-8"},Adagrad:{learningRate:"0.1",epsilon:"1e-8"},RMSProp:{learningRate:"0.1",beta:"0.9",epsilon:"1e-8"},Adadelta:{beta:"0.9",epsilon:"1e-3"},Newton:{}},$6={"--Custom--":"x^2",Quadratic:"x^2","Local Minima":"x^2 - 0.1cos(20x)",Plateau:"x^4"},J6={"--Custom--":"x^2 + 3y^2",Quadratic:"x^2 + 3y^2",Small:"(0.05x)^2 + 3(0.05y)^2",Ackley:"-20exp(-0.2 sqrt(0.5(x^2 + y^2))) - exp(0.5(cos(2 pi x) + cos(2 pi y))) + e + 20",Rasteringin:"20 + (x^2 - 10cos(2 pi x)) + (y^2 - 10cos(2 pi y))",Rosenbrock:"(1 - x)^2 + 100(y - x^2)^2"};function oj({settings:Ie,setSettings:ht,onRandomInitialPoint:St,trajectoryValues:wt,onReset:lr,onNextStep:vr,onPrevStep:Lr}){const rn=["Settings","Optimize","Usage"],[xn,$r]=up.useState("Settings");function Tn(Ri,Ts){if(Ri==="algorithm"){const Si=ij[Ts];ht({...Ie,algorithm:Ts,...Si})}else ht({...Ie,[Ri]:Ts})}const[zn,xa]=up.useState("--Custom--");function Jn(Ri){xa(Ri);const Ts=Ie.mode==="Bivariate"?J6[Ri]:$6[Ri];Tn("functionExpr",Ts)}function ho(Ri){const Ts="--Custom--",Si=Ri==="Bivariate"?J6[Ts]:$6[Ts];ht({...Ie,mode:Ri,functionExpr:Si}),xa(Ts)}function Mo(Ri){return Ri&&Ri.length>0?Ri[Ri.length-1]:null}const is=Mo(wt?.x),Pi=Mo(wt?.y),Ge=Mo(wt?.derivative),ko=Mo(wt?.secondDerivative),Ml=Mo(wt?.z),xo=Mo(wt?.gradient),li=Mo(wt?.hessian);return Ro.jsxs(eM,{className:"flex flex-col h-full p-4 gap-2 min-w-0 min-h-0 overflow-auto",children:[Ro.jsx(DO,{tabs:rn,activeTab:xn,onChange:$r}),Ro.jsxs("div",{className:"flex flex-col gap-4 min-h-0 min-w-0 overflow-auto",children:[xn==="Settings"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(IO,{label:"Problem Type",options:rj,activeOption:Ie.mode,onChange:ho}),Ro.jsx(m8,{label:"Function",options:Ie.mode==="Bivariate"?Object.keys(J6):Object.keys($6),activeOption:zn,onChange:Jn}),Ro.jsx(pd,{label:"Function Expression",value:Ie.functionExpr,onChange:Ri=>Tn("functionExpr",Ri),readonly:zn!=="--Custom--",rows:3}),Ro.jsx(m8,{label:"Algorithm",options:nj,activeOption:Ie.algorithm,onChange:Ri=>Tn("algorithm",Ri)}),Ro.jsxs("div",{className:`${Ie.mode==="Bivariate"?"grid grid-cols-2 gap-2":""}`,children:[Ro.jsx(pd,{label:"Initial X",value:Ie.x0,onChange:Ri=>Tn("x0",Ri)}),Ie.mode==="Bivariate"&&Ro.jsx(pd,{label:"Initial Y",value:Ie.y0||"",onChange:Ri=>Tn("y0",Ri)})]}),Ro.jsx(ST,{label:"Random Initial Point",onClick:St}),Ro.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[["Gradient Descent","Nesterov","Adam","Adagrad","RMSProp","Adadelta"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Learning Rate",value:Ie.learningRate,onChange:Ri=>Tn("learningRate",Ri)})}),["Gradient Descent","Nesterov"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Momentum",value:Ie.momentum,onChange:Ri=>Tn("momentum",Ri)})}),Ie.algorithm==="Adam"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(pd,{label:"Beta 1",value:Ie.beta1,onChange:Ri=>Tn("beta1",Ri)}),Ro.jsx(pd,{label:"Beta 2",value:Ie.beta2,onChange:Ri=>Tn("beta2",Ri)})]}),["RMSProp","Adadelta"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Beta",value:Ie.beta,onChange:Ri=>Tn("beta",Ri)})}),["Adam","Adagrad","RMSProp","Adadelta"].includes(Ie.algorithm)&&Ro.jsx(Ro.Fragment,{children:Ro.jsx(pd,{label:"Epsilon",value:Ie.epsilon,onChange:Ri=>Tn("epsilon",Ri)})})]})]}),xn==="Optimize"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsxs("div",{className:"hidden lg:flex flex-col gap-4 min-h-0 min-w-0 overflow-auto",children:[Ie.mode==="Univariate"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(pd,{label:"Current X",value:is!==null?is.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Y",value:Pi!==null?Pi.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Derivative",value:Ge!==null?Ge.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Second Derivative",value:ko!==null?ko.toFixed(4):"",readonly:!0})]}),Ie.mode==="Bivariate"&&Ro.jsxs(Ro.Fragment,{children:[Ro.jsx(pd,{label:"Current X",value:is!==null?is.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Y",value:Pi!==null?Pi.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Z",value:Ml!==null?Ml.toFixed(4):"",readonly:!0}),Ro.jsx(pd,{label:"Current Gradient",value:xo!==null?`[${xo.map(Ri=>Ri.toFixed(4)).join(", ")}]`:"",readonly:!0}),Ro.jsx(pd,{label:"Current Hessian",value:li!==null?`[${li.map(Ri=>`[${Ri.map(Ts=>Ts.toFixed(4)).join(", ")}]`).join(", ")}]`:"",readonly:!0})]})]}),Ro.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[Ro.jsx(ST,{label:"Next Step",onClick:vr}),Ro.jsx(ST,{label:"Previous Step",onClick:Lr})]}),Ro.jsx(ST,{label:"Reset",onClick:lr})]}),xn==="Usage"&&Ro.jsx($U,{components:{h1:({children:Ri})=>Ro.jsx("h1",{className:"text-2xl font-bold mt-4 mb-2",children:Ri}),h2:({children:Ri})=>Ro.jsx("h2",{className:"text-xl font-semibold mt-4 mb-2",children:Ri}),h3:({children:Ri})=>Ro.jsx("h3",{className:"text-lg font-semibold mt-4 mb-2",children:Ri}),p:({children:Ri})=>Ro.jsx("p",{className:"leading-6 mb-3 last:mb-0",children:Ri}),ul:({children:Ri})=>Ro.jsx("ul",{className:"list-disc pl-5 mb-3",children:Ri}),ol:({children:Ri})=>Ro.jsx("ol",{className:"list-decimal pl-5 mb-3",children:Ri}),li:({children:Ri})=>Ro.jsx("li",{className:"mb-1",children:Ri})},children:aj})]})]})}function sj(Ie){return new Worker("/assets/pyodide.worker-DcyL_a_W.js",{name:Ie?.name})}function lj(Ie){const[ht,St]=up.useState({}),[wt,lr]=up.useState(!0),vr=up.useRef(null);function Lr(Tn){vr.current&&vr.current.postMessage({type:"INIT",settings:Tn})}function rn(){vr.current&&vr.current.postMessage({type:"NEXT_STEP"})}function xn(){vr.current&&vr.current.postMessage({type:"PREV_STEP"})}function $r(){vr.current&&vr.current.postMessage({type:"RESET"})}return up.useEffect(()=>{const Tn=new sj;return vr.current=Tn,Tn.onmessage=zn=>{const xa=zn.data;xa.type==="READY"?(console.log("Pyodide is ready"),lr(!1),Lr(Ie)):xa.type==="RESULT"&&St(Jn=>({functionValues:xa.data.functionValues||Jn.functionValues,trajectoryValues:xa.data.trajectoryValues||Jn.trajectoryValues}))},()=>{Tn.terminate()}},[]),{isLoading:wt,plotData:ht,sendInit:Lr,sendReset:$r,sendNextStep:rn,sendPrevStep:xn}}const uj={mode:"Univariate",functionExpr:"x^2",algorithm:"Gradient Descent",x0:"0.5",y0:"0.5",learningRate:"0.1",momentum:"0.0"},cj=[-1,1],fj=[-1,1];function hj(){const[Ie,ht]=up.useState(uj),St=up.useRef(cj),wt=up.useRef(fj),lr={...Ie,xlim:St.current,ylim:wt.current};function vr($r){ht($r),xn.sendInit({...$r,xlim:St.current,ylim:wt.current})}function Lr($r,Tn){St.current=$r,wt.current=Tn,xn.sendInit({...Ie,xlim:$r,ylim:Tn})}function rn(){const $r=St.current[1]-St.current[0],Tn=wt.current[1]-wt.current[0],zn=(St.current[0]+Math.random()*$r*.95).toFixed(2),xa=(wt.current[0]+Math.random()*Tn*.95).toFixed(2),Jn={...Ie,x0:zn,y0:xa};vr(Jn)}const xn=lj(lr);return Ro.jsxs("div",{className:"grid grid-cols-[1fr_12fr_1fr] lg:h-dvh bg-stone-50",children:[Ro.jsx("div",{}),Ro.jsxs("div",{className:"flex flex-col gap-6 p-6 min-h-0",children:[Ro.jsx("div",{className:"title",children:"Optimization Trajectory Visualizer"}),Ro.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-[2fr_1fr] gap-12 flex-1 min-h-0 overflow-auto",children:[Ro.jsx(zO,{data:xn.plotData,isLoading:xn.isLoading,xlim:St.current,ylim:wt.current,setAxisLimits:Lr}),Ro.jsx(oj,{settings:Ie,setSettings:vr,onRandomInitialPoint:rn,trajectoryValues:xn.plotData.trajectoryValues,onReset:()=>xn.sendReset(),onNextStep:()=>xn.sendNextStep(),onPrevStep:()=>xn.sendPrevStep()})]})]}),Ro.jsx("div",{})]})}AO.createRoot(document.getElementById("root")).render(Ro.jsx(up.StrictMode,{children:Ro.jsx(hj,{})}));
dist/assets/{pyodide.worker-jb0eIAfr.js → pyodide.worker-DcyL_a_W.js} RENAMED
@@ -472,7 +472,7 @@ def nesterov_univariate(
472
  else:
473
  m = momentum * (x_values[-1] - x_values[-2])
474
 
475
- x_lookahead = x - m
476
  x = x_lookahead - learning_rate * f_prime(x_lookahead)
477
 
478
  x_values.append(x)
@@ -519,8 +519,8 @@ def nesterov_bivariate(
519
  mx = momentum * (x_values[-1] - x_values[-2])
520
  my = momentum * (y_values[-1] - y_values[-2])
521
 
522
- x_lookahead = x - mx
523
- y_lookahead = y - my
524
 
525
  x = x_lookahead - learning_rate * fx(x_lookahead, y_lookahead)
526
  y = y_lookahead - learning_rate * fy(x_lookahead, y_lookahead)
@@ -646,7 +646,7 @@ def adagrad_univariate(
646
  for i in range(steps - 1):
647
  g = f_prime(x)
648
  v += g ** 2
649
- x = x - (learning_rate / (np.sqrt(v) + epsilon)) * g
650
 
651
  x_values.append(x)
652
  y_values.append(f(x))
@@ -693,8 +693,8 @@ def adagrad_bivariate(
693
  vx += gx ** 2
694
  vy += gy ** 2
695
 
696
- x = x - (learning_rate / (np.sqrt(vx) + epsilon)) * gx
697
- y = y - (learning_rate / (np.sqrt(vy) + epsilon)) * gy
698
 
699
  x_values.append(x)
700
  y_values.append(y)
@@ -733,7 +733,7 @@ def rmsprop_univariate(
733
  for i in range(steps - 1):
734
  g = f_prime(x)
735
  v = beta * v + (1 - beta) * g ** 2
736
- x = x - (learning_rate / (np.sqrt(v) + epsilon)) * g
737
 
738
  x_values.append(x)
739
  y_values.append(f(x))
@@ -781,8 +781,8 @@ def rmsprop_bivariate(
781
  vx = beta * vx + (1 - beta) * gx ** 2
782
  vy = beta * vy + (1 - beta) * gy ** 2
783
 
784
- x = x - (learning_rate / (np.sqrt(vx) + epsilon)) * gx
785
- y = y - (learning_rate / (np.sqrt(vy) + epsilon)) * gy
786
 
787
  x_values.append(x)
788
  y_values.append(y)
 
472
  else:
473
  m = momentum * (x_values[-1] - x_values[-2])
474
 
475
+ x_lookahead = x + m
476
  x = x_lookahead - learning_rate * f_prime(x_lookahead)
477
 
478
  x_values.append(x)
 
519
  mx = momentum * (x_values[-1] - x_values[-2])
520
  my = momentum * (y_values[-1] - y_values[-2])
521
 
522
+ x_lookahead = x + mx
523
+ y_lookahead = y + my
524
 
525
  x = x_lookahead - learning_rate * fx(x_lookahead, y_lookahead)
526
  y = y_lookahead - learning_rate * fy(x_lookahead, y_lookahead)
 
646
  for i in range(steps - 1):
647
  g = f_prime(x)
648
  v += g ** 2
649
+ x = x - (learning_rate / (np.sqrt(v + epsilon))) * g
650
 
651
  x_values.append(x)
652
  y_values.append(f(x))
 
693
  vx += gx ** 2
694
  vy += gy ** 2
695
 
696
+ x = x - (learning_rate / (np.sqrt(vx + epsilon))) * gx
697
+ y = y - (learning_rate / (np.sqrt(vy + epsilon))) * gy
698
 
699
  x_values.append(x)
700
  y_values.append(y)
 
733
  for i in range(steps - 1):
734
  g = f_prime(x)
735
  v = beta * v + (1 - beta) * g ** 2
736
+ x = x - (learning_rate / (np.sqrt(v + epsilon))) * g
737
 
738
  x_values.append(x)
739
  y_values.append(f(x))
 
781
  vx = beta * vx + (1 - beta) * gx ** 2
782
  vy = beta * vy + (1 - beta) * gy ** 2
783
 
784
+ x = x - (learning_rate / (np.sqrt(vx + epsilon))) * gx
785
+ y = y - (learning_rate / (np.sqrt(vy + epsilon))) * gy
786
 
787
  x_values.append(x)
788
  y_values.append(y)
dist/index.html CHANGED
@@ -5,7 +5,7 @@
5
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <title>Optimization</title>
8
- <script type="module" crossorigin src="/assets/index-BoSq-s5V.js"></script>
9
  <link rel="stylesheet" crossorigin href="/assets/index-5gsYPC1K.css">
10
  </head>
11
  <body>
 
5
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <title>Optimization</title>
8
+ <script type="module" crossorigin src="/assets/index-CPl3lL5a.js"></script>
9
  <link rel="stylesheet" crossorigin href="/assets/index-5gsYPC1K.css">
10
  </head>
11
  <body>