簡體   English   中英

MATLAB中的隨機梯度下降算法

[英]Stochastic gradient descent algorithm in MATLAB

我正在嘗試在MATLAB中實現隨機梯度下降,但是我在某個地方出錯了。 我認為也許我檢查收斂性的方式是不正確的(我不太確定如何在每次迭代時更新估算器),但是我不確定。 我一直在嘗試僅適合基本線性數據,但是得到的結果相差很遠,希望能得到一些幫助。 有人可以指出我要去哪里,為什么這不能正常工作?

謝謝!

這是數據設置和通用代碼:

clear all;
close all;
clc

N_features = 2;
d = 100;
m = 100;

X_train = 10*rand(d,1);
X_test = 10*rand(d,1);
X_train = [ones(d,1) X_train];
X_test = [ones(d,1) X_test];

y_train = 5 + X_train(:,2) + 0.5*randn(d,1);
y_test = 5 + X_test(:,2) + 0.5*randn(d,1);

gamma = 0.01; %learning rate

[sgd_est_train,sgd_est_test,SSE_train,SSE_test,w] = stoch_grad(d,m,N_features,X_train,y_train,X_test,y_test,gamma);

figure(1)
plot(X_train(:,2),sgd_est_train,'ro',X_train(:,2),y_train,'go')

figure(2)
plot(X_test(:,2),sgd_est_test,'bo',X_test(:,2),y_test,'go')

實際實現SGD的功能是:

% stochastic gradient descent

function [sgd_est_train,sgd_est_test,SSE_train,SSE_test,w] = stoch_grad(d,m,N_features,X_train,y_train,X_test,y_test,gamma)

    epsilon = 0.01; %convergence criterion
    max_iter = 10000;

    w0 = zeros(N_features,1); %initial guess
    w = zeros(N_features,1); %for convenience

    x = zeros(d,1);
    z = zeros(d,1);

    for jj=1:max_iter;
        for kk=1:d;
            x = X_train(kk,:)';
            z = gamma*((w0'*x-y_train(kk))*x);
            w = w0 - z;
        end

        if norm(w0-w,2)<epsilon
            break;
        else
            w0 = w;
        end
    end

    sgd_est_test = zeros(m,1);
    sgd_est_train = zeros(d,1);

    for ll=1:m;
        sgd_est_test(ll,1) = w'*X_test(ll,:)';
    end

    for ii=1:d;
        sgd_est_train(ii,1) = w'*X_train(ii,:)';
    end

    SSE_test = sum((sgd_est_test - y_test).^2);
    SSE_train = sum((sgd_est_train - y_train).^2);

end

我試圖將學習率降低到0.001,結果是: 伊姆古爾
這告訴我您的算法產生的估計形式為y = a x而不是y = a x + b(出於某種原因忽略了常數項),並且您還需要降低學習率才能收斂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM