Initial commit
This commit is contained in:
6
Divers/descente_gradient/README.md
Normal file
6
Divers/descente_gradient/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Algorithme d'apprentissage
|
||||
## La descente de gradient
|
||||
|
||||
La vidéo de ce tutoriel est disponible à l'adresse suivante: https://www.youtube.com/watch?v=0MEyDJa2GTc
|
||||
|
||||
|
||||
52
Divers/descente_gradient/comparaison.py
Normal file
52
Divers/descente_gradient/comparaison.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
from matplotlib import cm
|
||||
from matplotlib.colors import LogNorm
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
def fonction(X, Y):
|
||||
return X*np.exp(-X**2-Y**2)+(X**2+Y**2)/20
|
||||
|
||||
def gradient_fonction(X, Y):
|
||||
g_x=np.exp(-X**2-Y**2)+X*-2*X*np.exp(-X**2-Y**2)+X/10
|
||||
g_y=-2*Y*X*np.exp(-X**2-Y**2)+Y/10
|
||||
return g_x, g_y
|
||||
|
||||
fig=plt.figure()
|
||||
fig.set_size_inches(9, 7, forward=True)
|
||||
ax=Axes3D(fig, azim=-29, elev=49)
|
||||
X=np.arange(-3, 3, 0.2)
|
||||
Y=np.arange(-3, 3, 0.2)
|
||||
X, Y=np.meshgrid(X, Y)
|
||||
Z=fonction(X, Y)
|
||||
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
|
||||
plt.xlabel("Paramètre 1 (x)")
|
||||
plt.ylabel("Paramètre 2 (y)")
|
||||
|
||||
x1=x2=np.random.random_integers(-2, 2)+np.random.rand(1)[0]
|
||||
y1=y2=np.random.random_integers(-2, 2)+np.random.rand(1)[0]
|
||||
|
||||
lr=0.2
|
||||
lr2=0.9
|
||||
correction_x1=0
|
||||
correction_y1=0
|
||||
i=0
|
||||
while True:
|
||||
g_x1, g_y1=gradient_fonction(x1, y1)
|
||||
g_x2, g_y2=gradient_fonction(x2, y2)
|
||||
|
||||
correction_x1=lr2*correction_x1-lr*g_x1
|
||||
x1=x1+correction_x1
|
||||
correction_y1=lr2*correction_y1-lr*g_y1
|
||||
y1=y1+correction_y1
|
||||
|
||||
x2=x2-lr*g_x2
|
||||
y2=y2-lr*g_y2
|
||||
|
||||
ax.scatter(x1, y1, fonction(x1, y1), marker='o', s=10, color='#FF0000')
|
||||
ax.scatter(x2, y2, fonction(x2, y2), marker='o', s=10, color='#00FF00')
|
||||
plt.draw()
|
||||
print("iteration= {} x1={:+7.5f} y1={:+7.5f} x2={:+7.5f} y2={:+7.5f}".format(i, x1, y1, x2, y2))
|
||||
plt.pause(0.05)
|
||||
i+=1
|
||||
23
Divers/descente_gradient/exemple_2d_1.py
Normal file
23
Divers/descente_gradient/exemple_2d_1.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def fonction(x):
|
||||
return x**2+3*x-2
|
||||
|
||||
def gradient_fonction(x):
|
||||
return 2*x+3
|
||||
|
||||
xvals=np.arange(-5, 3, 0.1)
|
||||
yvals=fonction(xvals)
|
||||
plt.plot(xvals, yvals)
|
||||
|
||||
x=np.random.random_integers(-4, 3)+np.random.rand(1)[0]
|
||||
lr=0.2
|
||||
i=0
|
||||
while True:
|
||||
plt.scatter(x, fonction(x), color='#FF0000')
|
||||
plt.draw()
|
||||
plt.pause(0.5)
|
||||
x=x-lr*gradient_fonction(x)
|
||||
print("itération {:3d} -> x={:+7.5f}".format(i, x))
|
||||
i+=1
|
||||
24
Divers/descente_gradient/exemple_2d_2.py
Normal file
24
Divers/descente_gradient/exemple_2d_2.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def fonction(x):
|
||||
return 3*x**4-4*x**3-12*x**2-0*x-3
|
||||
|
||||
def gradient_fonction(x):
|
||||
return 12*x**3-12*x**2-24*x
|
||||
|
||||
xvals=np.arange(-3, 4, 0.1)
|
||||
yvals=fonction(xvals)
|
||||
plt.plot(xvals, yvals)
|
||||
|
||||
x=np.random.random_integers(-3, 3)+np.random.rand(1)[0]
|
||||
i=0
|
||||
print("itération: {} x={}".format(i, x))
|
||||
lr=0.015
|
||||
while True:
|
||||
plt.scatter(x, fonction(x), color='#FF0000')
|
||||
plt.draw()
|
||||
plt.pause(0.5)
|
||||
x=x-lr*gradient_fonction(x)
|
||||
i+=1
|
||||
print("itération {:3d} -> x={}".format(i, x))
|
||||
27
Divers/descente_gradient/exemple_2d_2_inertie.py
Normal file
27
Divers/descente_gradient/exemple_2d_2_inertie.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def fonction(x):
|
||||
return 3*x**4-4*x**3-12*x**2-0*x-3
|
||||
|
||||
def gradient_fonction(x):
|
||||
return 12*x**3-12*x**2-24*x
|
||||
|
||||
xvals=np.arange(-3, 4, 0.1)
|
||||
yvals=fonction(xvals)
|
||||
plt.plot(xvals, yvals)
|
||||
|
||||
x=np.random.random_integers(-3, 3)+np.random.rand(1)[0]
|
||||
i=0
|
||||
print("itération: {} x={}".format(i, x))
|
||||
lr=0.015
|
||||
lr2=0.3
|
||||
correction=0
|
||||
while True:
|
||||
plt.scatter(x, fonction(x), color='#FF0000')
|
||||
plt.draw()
|
||||
plt.pause(0.5)
|
||||
correction=lr2*correction-lr*gradient_fonction(x)
|
||||
x=x+correction
|
||||
i+=1
|
||||
print("itération {:3d} -> x={}".format(i, x))
|
||||
42
Divers/descente_gradient/exemple_3d.py
Normal file
42
Divers/descente_gradient/exemple_3d.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
from matplotlib import cm
|
||||
from matplotlib.colors import LogNorm
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
def fonction(X, Y):
|
||||
return X*np.exp(-X**2-Y**2)+(X**2+Y**2)/20
|
||||
|
||||
def gradient_fonction(X, Y):
|
||||
g_x=np.exp(-X**2-Y**2)+X*-2*X*np.exp(-X**2-Y**2)+X/10
|
||||
g_y=-2*Y*X*np.exp(-X**2-Y**2)+Y/10
|
||||
return g_x, g_y
|
||||
|
||||
fig=plt.figure()
|
||||
fig.set_size_inches(9, 7, forward=True)
|
||||
ax=Axes3D(fig, azim=-29, elev=49)
|
||||
X=np.arange(-3, 3, 0.2)
|
||||
Y=np.arange(-3, 3, 0.2)
|
||||
X, Y=np.meshgrid(X, Y)
|
||||
Z=fonction(X, Y)
|
||||
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
|
||||
plt.xlabel("Paramètre 1 (x)")
|
||||
plt.ylabel("Paramètre 2 (y)")
|
||||
|
||||
x=np.random.random_integers(-2, 2)+np.random.rand(1)[0]
|
||||
y=np.random.random_integers(-2, 2)+np.random.rand(1)[0]
|
||||
|
||||
lr=0.2
|
||||
correction_x=0
|
||||
correction_y=0
|
||||
i=0
|
||||
while True:
|
||||
g_x, g_y=gradient_fonction(x, y)
|
||||
x=x-lr*g_x
|
||||
y=y-lr*g_y
|
||||
ax.scatter(x, y, fonction(x, y), marker='o', s=10, color='#00FF00')
|
||||
plt.draw()
|
||||
print("itération {:3d} -> x={:+7.5f} y={:+7.5f}".format(i, x, y))
|
||||
plt.pause(0.05)
|
||||
i+=1
|
||||
47
Divers/descente_gradient/exemple_3d_inertie.py
Normal file
47
Divers/descente_gradient/exemple_3d_inertie.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
from matplotlib import cm
|
||||
from matplotlib.colors import LogNorm
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
def fonction(X, Y):
|
||||
return X*np.exp(-X**2-Y**2)+(X**2+Y**2)/20
|
||||
|
||||
def gradient_fonction(X, Y):
|
||||
g_x=np.exp(-X**2-Y**2)+X*-2*X*np.exp(-X**2-Y**2)+X/10
|
||||
g_y=-2*Y*X*np.exp(-X**2-Y**2)+Y/10
|
||||
return g_x, g_y
|
||||
|
||||
fig=plt.figure()
|
||||
fig.set_size_inches(9, 7, forward=True)
|
||||
ax=Axes3D(fig, azim=-29, elev=49)
|
||||
X=np.arange(-3, 3, 0.2)
|
||||
Y=np.arange(-3, 3, 0.2)
|
||||
X, Y=np.meshgrid(X, Y)
|
||||
Z=fonction(X, Y)
|
||||
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
|
||||
#ax.contour(X, Y, Z, 70, rstride=1, cstride=1, cmap='plasma')
|
||||
|
||||
plt.xlabel("Paramètre 1 (x)")
|
||||
plt.ylabel("Paramètre 2 (y)")
|
||||
|
||||
x=np.random.random_integers(-2, 2)+np.random.rand(1)[0]
|
||||
y=np.random.random_integers(-2, 2)+np.random.rand(1)[0]
|
||||
|
||||
lr=0.2
|
||||
lr2=0.9
|
||||
correction_x=0
|
||||
correction_y=0
|
||||
i=0
|
||||
while True:
|
||||
g_x, g_y=gradient_fonction(x, y)
|
||||
correction_x=lr2*correction_x-lr*g_x
|
||||
x=x+correction_x
|
||||
correction_y=lr2*correction_y-lr*g_y
|
||||
y=y+correction_y
|
||||
ax.scatter(x, y, fonction(x, y), marker='o', s=10, color='#FF0000')
|
||||
plt.draw()
|
||||
print("itération {:3d} -> x={:+7.5f} y={:+7.5f}".format(i, x, y))
|
||||
plt.pause(0.05)
|
||||
i+=1
|
||||
35
Divers/descente_gradient/gradient.py
Normal file
35
Divers/descente_gradient/gradient.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from mpl_toolkits.mplot3d import Axes3D
|
||||
from matplotlib import cm
|
||||
from matplotlib.colors import LogNorm
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import math
|
||||
|
||||
def fonction(X, Y):
|
||||
return X*np.exp(-X**2-Y**2)+(X**2+Y**2)/20
|
||||
|
||||
def gradient_fonction(X, Y):
|
||||
g_x=np.exp(-X**2-Y**2)+X*-2*X*np.exp(-X**2-Y**2)+X/10
|
||||
g_y=-2*Y*X*np.exp(-X**2-Y**2)+Y/10
|
||||
return g_x, g_y
|
||||
|
||||
fig=plt.figure()
|
||||
fig.set_size_inches(9, 7, forward=True)
|
||||
ax=Axes3D(fig, azim=-29, elev=49)
|
||||
X=np.arange(-3, 3, 0.2)
|
||||
Y=np.arange(-3, 3, 0.2)
|
||||
X, Y=np.meshgrid(X, Y)
|
||||
Z=fonction(X, Y)
|
||||
ax.plot_surface(X, Y, Z, rstride=1, cstride=1)
|
||||
plt.xlabel("Paramètre 1 (x)")
|
||||
plt.ylabel("Paramètre 2 (y)")
|
||||
|
||||
x, y=np.meshgrid(np.arange(-3, 3, 0.2),
|
||||
np.arange(-3, 3, 0.2))
|
||||
z=-1
|
||||
|
||||
u, v=gradient_fonction(x, y)
|
||||
w=0
|
||||
ax.quiver(x, y, z, u, v, w, length=0.15, normalize=True, color='#333333')
|
||||
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user