Gradient Descent Demo
Gradient Descent Demo
Commands for implementing gradient descent on a fixed function.
In[]:=
contPlot=ContourPlot[-Sin[x+.001]Sin[y-.001]-0.1(x-.5)^2,{x,-3,3},{y,-3,3}]
Out[]=
In[]:=
f[x_,y_]=-Sin[x+.001]Sin[y-.001]-0.1(x-.5)^2
Out[]=
-0.1+Sin[0.001+x]Sin[0.001-y]
2
(-0.5+x)
In[]:=
g[x_,y_]=Grad[f[x,y],{x,y}];g[0,0]
Out[]=
{0.101,-0.000999999}
In[]:=
step=0.1;{x,y}+step*g[0,0]
In[]:=
{x,y}={-1.5,1};points={};Do[{{xn,yn}={x,y}-0.4g[x,y];AppendTo[points,{x,y}];{x,y}={xn,yn};},{15}]pointsPlot=ListPlot[points,PlotStyleRed,PlotMarkers{Automatic,7}];Show[contPlot,pointsPlot]