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 notebook
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# Axes3D import has side effects, it enables using projection='3d' in add_subplot
import matplotlib.pyplot as plt
import random
def fun(x, y):
= np.array(x)
x = np.array(y)
y return 14+3*(x**2) + 14*(y**2) - 12*x- 28*y + 12*x*y
= []
lst_x = []
lst_y = init_x
x_ = init_y
y_ = 0.005
alpha
lst_x.append(x_)
lst_y.append(y_)
for i in range(10):
= plt.figure(figsize=(10,10))
fig = fig.add_subplot(111, projection='3d')
ax = y = np.arange(-4.0, 4.0, 0.05)
x = np.meshgrid(x, y)
X, Y = np.array(fun(np.ravel(X), np.ravel(Y)))
zs = zs.reshape(X.shape)
Z = lst_x[-1]
x_ = lst_y[-1]
y_ # ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')
# print (lst_x,lst_y,fun(lst_x,lst_y))
=10,alpha=1,cmap='hsv')
ax.scatter3D(lst_x,lst_y,fun(lst_x,lst_y),lw='orange',cmap='hsv')
ax.plot_surface(X, Y, Z,color
'X Label')
ax.set_xlabel('Y Label')
ax.set_ylabel('Z Label')
ax.set_zlabel("Iteration "+str(i+1))
plt.title(- alpha * (3*x_ - 12 + 12*y_))
lst_x.append(x_ - alpha *(14*y_ -28 + 12*x_))
lst_y.append(y_
plt.show()
x = np.linspace(-5,5,1000) y = x**2 plt.plot(x,y) plt.title(“Cost Function”)
'axes.facecolor'] = '#fafafa'
plt.rcParams[
= 4.1
p = 0.05
alpha = 20
iterations
# for i in range(10):
for i in range(iterations):
plt.figure()
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=100)
plt.scatter([prev],[prev"x")
plt.xlabel("Cost")
plt.ylabel("Iteration "+str(i+1)+" (lr: "+str(alpha)+")")
plt.title("iteration-"+str(i+1)+".eps", format='eps',transparent=True)
plt.savefig(
plt.show()
= ""
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}
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
= -7.2
val
plt.scatter([val],func(np.array([val])))'local minima', xy=(val, func(val)), xytext=(val, 1),
ax.annotate(=dict(facecolor='black', shrink=0.05))
arrowprops
plt.xticks([])
plt.yticks([])
plt.plot(x,y)"local-minima.eps", format='eps',transparent=True) plt.savefig(
= np.linspace(-5,5,1000)
x = x**2
y
= 4.1
p = .95
alpha = 10
iterations
# for i in range(10):
for i in range(iterations):
plt.figure()
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=100)
plt.scatter([prev],[prev"x")
plt.xlabel("Cost")
plt.ylabel("Iteration "+str(i+1)+" (lr: "+str(alpha)+")")
plt.title("overshooting-"+str(i+1)+".eps", format='eps',transparent=True)
plt.savefig( plt.show()
= ""
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
# for i in range(10):
for i in range(iterations):
plt.figure()
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=100)
plt.scatter([prev],[prev"x")
plt.xlabel("Cost")
plt.ylabel("Iteration "+str(i+1)+" (lr: "+str(alpha)+")")
plt.title("undershooting-"+str(i+1)+".eps", format='eps',transparent=True)
plt.savefig( plt.show()
= ""
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(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