Ciao Sto cercando di implementare l'algoritmo Gradient Descent per una funzione:Cosa c'è di sbagliato con il mio algoritmo di discesa del gradiente
mio punto di partenza per l'algoritmo è w = (u, v) = (2,2). Il tasso di apprendimento è eta = 0,01 e legato = 10^-14. Ecco il mio codice MATLAB:
function [resultTable, boundIter] = gradientDescent(w, iters, bound, eta)
% FUNCTION [resultTable, boundIter] = gradientDescent(w, its, bound, eta)
%
% DESCRIPTION:
% - This function will do gradient descent error minimization for the
% function E(u,v) = (u*exp(v) - 2*v*exp(-u))^2.
%
% INPUTS:
% 'w' a 1-by-2 vector indicating initial weights w = [u,v]
% 'its' a positive integer indicating the number of gradient descent
% iterations
% 'bound' a real number indicating an error lower bound
% 'eta' a positive real number indicating the learning rate of GD algorithm
%
% OUTPUTS:
% 'resultTable' a iters+1-by-6 table indicating the error, partial
% derivatives and weights for each GD iteration
% 'boundIter' a positive integer specifying the GD iteration when the error
% function got below the given error bound 'bound'
%
% The error function
E = @(u,v) (u*exp(v) - 2*v*exp(-u))^2;
% Partial derivative of E with respect to u
pEpu = @(u,v) 2*(u*exp(v) - 2*v*exp(-u))*(exp(v) + 2*v*exp(-u));
% Partial derivative of E with respect to v
pEpv = @(u,v) 2*(u*exp(v) - 2*v*exp(-u))*(u*exp(v) - 2*exp(-u));
% Initialize boundIter
boundIter = 0;
% Create a table for holding the results
resultTable = zeros(iters+1, 6);
% Iteration number
resultTable(1, 1) = 0;
% Error at iteration i
resultTable(1, 2) = E(w(1), w(2));
% The value of pEpu at initial w = (u,v)
resultTable(1, 3) = pEpu(w(1), w(2));
% The value of pEpv at initial w = (u,v)
resultTable(1, 4) = pEpv(w(1), w(2));
% Initial u
resultTable(1, 5) = w(1);
% Initial v
resultTable(1, 6) = w(2);
% Loop all the iterations
for i = 2:iters+1
% Save the iteration number
resultTable(i, 1) = i-1;
% Update the weights
temp1 = w(1) - eta*(pEpu(w(1), w(2)));
temp2 = w(2) - eta*(pEpv(w(1), w(2)));
w(1) = temp1;
w(2) = temp2;
% Evaluate the error function at new weights
resultTable(i, 2) = E(w(1), w(2));
% Evaluate pEpu at the new point
resultTable(i, 3) = pEpu(w(1), w(2));
% Evaluate pEpv at the new point
resultTable(i, 4) = pEpv(w(1), w(2));
% Save the new weights
resultTable(i, 5) = w(1);
resultTable(i, 6) = w(2);
% If the error function is below a specified bound save this iteration
% index
if E(w(1), w(2)) < bound
boundIter = i-1;
end
end
Questo è un esercizio nella mia macchina corso di formazione, ma per qualche ragione i miei risultati sono tutti sbagliati. Ci deve essere qualcosa di sbagliato nel codice. Ho provato il debugging e il debugging e non ho trovato nulla di sbagliato ... qualcuno può identificare qual è il mio problema qui? ... In altre parole, puoi verificare che il codice sia un algoritmo di discesa del gradiente valido per la funzione data?
Si prega di farmi sapere se la mia domanda è troppo chiaro o se avete bisogno di più informazioni :)
Grazie per il vostro sforzo e aiuto! =)
Ecco il mio risultato per cinque iterazioni e quello che gli altri hanno ottenuto:
PARAMETRI: w = [2,2], l'ETA = 0,01, limite = 10^-14, iter = 5
Avete i dati di input e il risultato? –
@ AnderBiguri Ciao, non ci sono dati di input per questo problema. Il punto è solo per minimizzare la funzione data E (u, v) con la discesa del gradiente. Il punto di partenza è w = (u, v) = (2,2), eta = 0,01, legato = 10^-14. Il parametro 'iters' può essere scelto liberamente, ad es. iters = 50. Pubblicherò i miei risultati con cinque iterazioni e quindi i risultati corrispondenti dal forum di discussione dei miei corsi che altre persone hanno ricevuto. – jjepsuomi
Haha c'è dati di input, e appena me lo dai! grazie, controllerò. –