Vous êtes sur la page 1sur 4

TP OPTIMISATION

Encadré par : Mrs Aberqi

Réalisé par :

. GESI 1. N :6°.
RAPPORT DU PROJET

OPTIMISATION D’UNE FONCTION : Méthode de gradient descente


La Descente de Gradient est un algorithme d’optimisation qui permet de trouver
le minimum de n’importe quelle fonction convexe en convergeant progressivement vers celui-
ci.
I. PARTIE THEORIQUE :
Dans la partie théorique on va procédé par les étapes suivantes.
On va travailler sur la fonction :

𝑓(𝑥1 , 𝑥2 ) = 𝑥12 + 𝑥1 𝑥2 + 3𝑥22


Etape 1 : calcul de la fonction de mise à jour
𝛿 𝛿
On a 𝑓(𝑥1 , 𝑥2 ) = 2𝑥1 + 𝑥2 et 𝑓(𝑥1 , 𝑥2 ) = 6𝑥2 + 𝑥1
𝛿𝑥1 𝛿𝑥2

𝑥1 ≔ 𝑥1 − 𝛼(2𝑥1 + 𝑥2 ) et 𝑥2 ≔ 𝑥2 − 𝛼(6𝑥2 + 𝑥1 )

Etape 2 : choix du Learning rate le taux d’apprentissage


Le taux d’apprentissage nous permet de Controller la vitesse de l’algorithme. Généralement
On le choisit suffisamment petit afin de parcourir le max de points dans l’optimisation
d’atteindre le minimum.

On fix 𝛼 = 0.01
𝑥1 ≔ 𝑥1 − 0.02𝑥1 + 0.01𝑥2 = 0.98𝑥1 + 0.01𝑥2
𝑥2 ≔ 𝑥2 − 0.06𝑥2 + 0.01𝑥1 = 0.94𝑥2 + 0.01𝑥1
Etape 3 : itérer jusqu’à avoir le minimum
Nombre 𝑥1 𝑥2 𝑓(𝑥1 , 𝑥2 )
d’itération
0 3 3 45

1 2.97 2.85 41.65

2 2.93 2.7 38.36

3 2.89 2.56 35.41


. . . .

. . . .

. . . .

n 0.53 -0.12 -0.14

I. PARTIE NUMERIQUE :
Le programme :
function [xopt,fopt,niter,gnorm,dx] = grad_descent(varargin)
if nargin==0
% define starting point
x0 = [3 3]';
elseif nargin==1
% if a single input argument is provided, it is a user-defined starting
% point.
x0 = varargin{1};
else
error('Incorrect number of input arguments.')
end
% termination tolerance
tol = 1e-6;
% maximum number of allowed iterations
maxiter = 1000;
% minimum allowed perturbation
dxmin = 1e-6;
% step size ( 0.33 causes instability, 0.2 quite accurate)
alpha = 0.01;
% initialize gradient norm, optimization vector, iteration counter, perturbation
gnorm = inf; x = x0; niter = 0; dx = inf;
% define the objective function:
f = @(x1,x2) x1.^2 + x1.*x2 + 3*x2.^2;
% plot objective function contours for visualization:
figure(1); clf; ezcontour(f,[-5 5 -5 5]); axis equal; hold on
% redefine objective function syntax for use with optimization:
f2 = @(x) f(x(1),x(2));
% gradient descent algorithm:
while and(gnorm>=tol, and(niter <= maxiter, dx >= dxmin))
% calculate gradient:
g = grad(x);
gnorm = norm(g);
% take step:
xnew = x - alpha*g;
% check step
if ~isfinite(xnew)
display(['Number of iterations: ' num2str(niter)])
error('x is inf or NaN')
end
% plot current point
plot([x(1) xnew(1)],[x(2) xnew(2)],'ko-')
refresh
% update termination metrics
niter = niter + 1;
dx = norm(xnew-x);
x = xnew;

end
xopt = x;
fopt = f2(xopt);
niter = niter - 1;
% define the gradient of the objective
function g = grad(x)
g = [2*x(1) + x(2)
x(1) + 6*x(2)];

La visualisation :

Vous aimerez peut-être aussi