import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
sns.despine()
<Figure size 432x288 with 0 Axes>
= 2
init_x = 5 init_y
%matplotlib inline
import numpy as np
4.1*4.1
16.81
4.1-0.2*2*4.1
2.46
= 4.1
x = 0.2
alpha = 10
iterations
# for i in range(10):
for i in range(iterations):
= x- (alpha*2*x)
x print("(",round(x, 2),"," ,round(x*x, 2),")")
( 2.46 , 6.05 )
( 1.48 , 2.18 )
( 0.89 , 0.78 )
( 0.53 , 0.28 )
( 0.32 , 0.1 )
( 0.19 , 0.04 )
( 0.11 , 0.01 )
( 0.07 , 0.0 )
( 0.04 , 0.0 )
( 0.02 , 0.0 )
= 4.1
x = 0.05
alpha = 20
iterations
# for i in range(10):
for i in range(iterations):
= x- (alpha*2*x)
x = """
st
\begin{frame}{Iteration %d}
\begin{columns}
\begin{column}{0.6\textwidth}
\begin{adjustbox}{max totalsize={\textwidth},center}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel=$y$,
xmin=-4.2,
xmax=4.2,
axis x line*=bottom,
axis y line*=left,
xtick align=outside,
ytick align=outside,
legend pos=outer north east
]
\addplot[mark=none, gray] {x^2};\addlegendentry{$y=x^2$}
\addplot[only marks, mark=*]
coordinates{ % plot 1 data set
(%s,%s)
};
\end{axis}
\end{tikzpicture}
\end{adjustbox}
\end{column}
\begin{column}{0.5\textwidth}
\begin{adjustbox}{max totalsize={\textwidth},center}
\begin{tikzpicture}
\begin{axis}
[
title={Contour plot, view from top},
view={0}{90},
xlabel=$x$,
ylabel=$y$,
axis x line*=bottom,
axis y line*=left,
xtick align=outside,
ytick align=outside,
unit vector ratio*=1 1 1,
]
\addplot3[
contour gnuplot={number=14,}
]
{x^2};
\addplot[only marks, mark=*]
coordinates{ % plot 1 data set
(%f,%f)
};
\end{axis}
\end{tikzpicture}
\end{adjustbox}
\end{column}
\end{columns}
\end{frame}
""" %(i, i, i, i, i)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-10-3f4f6db3ccf0> in <module> 71 72 \end{frame} ---> 73 """ %(i, i, i, i, i) ValueError: unsupported format character 'p' (0x70) at index 793
= ""
s for i in range(iterations):
+="\\begin{frame}{Gradient Descent}\n"
s+=" \\begin{center}\n"
s+=" \includegraphics[totalheight=6cm]{gradient-descent/iteration-"+str(i+1)+".eps}\n"
s+=" \end{center}\n"
s+="\end{frame}\n\n" s
print (s)
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-1.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-2.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-3.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-4.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-5.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-6.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-7.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-8.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-9.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-10.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-11.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-12.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-13.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-14.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-15.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-16.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-17.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-18.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-19.eps}
\end{center}
\end{frame}
\begin{frame}{Gradient Descent}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/iteration-20.eps}
\end{center}
\end{frame}
8.2*4.1
33.62
def func(x):
return np.sin(x) + np.sin(x/2) + np.sin(x/3)
= plt.subplots()
fig, ax = np.linspace(-10,10,100)
x = x[x<=0]
x = func(x)
y
latexify()= -7.2
val
='k')
plt.scatter([val],func(np.array([val])), color'Local minima', xy=(val, func(val)), xytext=(val, 1),
ax.annotate(=dict(facecolor='grey', shrink=0.0001))
arrowprops
plt.xticks([])
plt.yticks([])='grey')
plt.plot(x,y, color
format_axes(plt.gca())"x")
plt.xlabel("y=f(x)")
plt.ylabel("../gradient-descent/local-minima.eps", format='eps',transparent=True, bbox_inches="tight") plt.savefig(
import sys
"../") sys.path.append(
from latexify import format_axes, latexify
= np.linspace(-5,5,1000)
x = x**2
y
= 4.1
p = 0.95
alpha = 10
iterations
latexify()# for i in range(10):
for i in range(iterations):
plt.figure()'k')
plt.plot(x,y,= p
prev = p - (alpha*2*p)
p **2,p-prev,p**2-prev**2,head_width=0.5, color='grey')
plt.arrow(prev,prev**2],s=40, color='grey')
plt.scatter([prev],[prev"x")
plt.xlabel("y")
plt.ylabel("Iteration "+str(i+1)+" (lr: "+str(alpha)+")")
plt.title(
format_axes(plt.gca())"../gradient-descent/overshooting-"+str(i+1)+".eps", format='eps',transparent=True, bbox_inches="tight") plt.savefig(
= ""
s for i in range(iterations):
+="\\begin{frame}{Overshooting}\n"
s+=" \\begin{center}\n"
s+=" \includegraphics[totalheight=6cm]{gradient-descent/overshooting-"+str(i+1)+".eps}\n"
s+=" \end{center}\n"
s+="\end{frame}\n\n" s
print (s)
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-1.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-2.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-3.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-4.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-5.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-6.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-7.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-8.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-9.eps}
\end{center}
\end{frame}
\begin{frame}{Overshooting}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/overshooting-10.eps}
\end{center}
\end{frame}
= np.linspace(-5,5,1000)
x = x**2
y
= 4.1
p = .01
alpha = 10
iterations
latexify()
# for i in range(10):
for i in range(iterations):
plt.figure()'k')
plt.plot(x,y,= p
prev = p - (alpha*2*p)
p **2,p-prev,p**2-prev**2,head_width=0.5)
plt.arrow(prev,prev**2],s=40, color='grey')
plt.scatter([prev],[prev"x")
plt.xlabel("y")
plt.ylabel("Iteration "+str(i+1)+" (lr: "+str(alpha)+")")
plt.title(
format_axes(plt.gca())"../gradient-descent/undershooting-"+str(i+1)+".eps", format='eps',transparent=True, bbox_inches="tight") plt.savefig(
= ""
s for i in range(iterations):
+="\\begin{frame}{Slow Convergence}\n"
s+=" \\begin{center}\n"
s+=" \includegraphics[totalheight=6cm]{gradient-descent/undershooting-"+str(i+1)+".eps}\n"
s+=" \end{center}\n"
s+="\end{frame}\n\n" s
print (s)
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-1.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-2.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-3.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-4.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-5.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-6.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-7.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-8.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-9.eps}
\end{center}
\end{frame}
\begin{frame}{Slow Convergence}
\begin{center}
\includegraphics[totalheight=6cm]{gradient-descent/undershooting-10.eps}
\end{center}
\end{frame}
= np.linspace(-5,5,1000)
x = x**2
y
= 4.1
p = .01
alpha = 10
iterations
latexify()
# for i in range(10):
for i in range(iterations):
plt.figure()'k')
plt.plot(x,y,= p
prev = p - (alpha*2*p)
p **2,p-prev,p**2-prev**2,head_width=0.5)
plt.arrow(prev,prev**2],s=40, color='grey')
plt.scatter([prev],[prev"x")
plt.xlabel("y")
plt.ylabel("Iteration "+str(i+1)+" (lr: "+str(alpha)+")")
plt.title(
format_axes(plt.gca())"../gradient-descent/undershooting-"+str(i+1)+".eps", format='eps',transparent=True, bbox_inches="tight") plt.savefig(
= np.linspace(1,10,100)
x = 1/x
y ="GD")
plt.plot(y,label= np.random.random((len(x)))
noise 0] = 0
noise[1] = 0
noise[2] = 0
noise[+0.2*(noise-0.5),label="SGD")
plt.plot(y
plt.legend()"Iterations vs Cost")
plt.title("Iteration")
plt.xlabel("Cost")
plt.ylabel("gd-sgd.eps", format='eps',transparent=True) plt.savefig(
The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.
The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.
= 4.1
val = 0.05
alpha
for i in range(10):
= val - alpha * 2* val
val print (val)
3.6899999999999995
3.3209999999999997
2.9888999999999997
2.6900099999999996
2.4210089999999997
2.1789080999999997
1.9610172899999996
1.7649155609999996
1.5884240048999996
1.4295816044099996