6

J'essaie d'implémenter "Stochastic gradient descent" dans MATLAB. J'ai suivi l'algorithme exactement, mais je reçois un TRÈS TRÈS grand w (coffients) pour la fonction de prédiction/ajustement. Est-ce que j'ai une erreur dans l'algorithme?Dégradé stochastique Implémentation de descente - MATLAB

L'algorithme: enter image description here

x = 0:0.1:2*pi  // X-axis 
    n = size(x,2);  
    r = -0.2+(0.4).*rand(n,1); //generating random noise to be added to the sin(x) function 

    t=zeros(1,n); 
    y=zeros(1,n); 



    for i=1:n 
     t(i)=sin(x(i))+r(i);   // adding the noise 
     y(i)=sin(x(i));    // the function without noise 
    end 

    f = round(1+rand(20,1)*n);  //generating random indexes 

    h = x(f);       //choosing random x points 
    k = t(f);       //chossing random y points 

    m=size(h,2);      // length of the h vector 

    scatter(h,k,'Red');    // drawing the training points (with noise) 
    %scatter(x,t,2); 
    hold on; 
    plot(x,sin(x));     // plotting the Sin function 


    w = [0.3 1 0.5];     // starting point of w 
    a=0.05;       // learning rate "alpha" 

// ---------------- ALGORITHM ---------------------// 
    for i=1:20 
     v = [1 h(i) h(i).^2];      // X vector 
     e = ((w*v') - k(i)).*v;   // prediction - observation 
     w = w - a*e;      // updating w 
    end 

    hold on; 

    l = 0:1:6; 
    g = w(1)+w(2)*l+w(3)*(l.^2); 
    plot(l,g,'Yellow');      // drawing the prediction function 

Répondre

7

Si vous utilisez un taux d'apprentissage trop élevé, SGD risque de diverger.
Le taux d'apprentissage devrait converger vers zéro.

3

généralement, si w fini avec des valeurs trop grandes, on surapprentissage. Je n'ai pas vraiment regardé votre code attentivement. Mais je pense, ce qui manque à votre code est un terme de régularisation approprié, qui empêche le surapprentissage d'entraînement. En outre, ici:

e = ((w*v') - k(i)).*v; 

Le v est pas ici le gradient de la valeur prédite, non? Selon l'algorithme, vous devriez le remplacer. Voyons voir comment ça va être après avoir fait ça.

Questions connexes