matlab中的随机梯度下降算法实现

时间:2017-03-25 12:18:11

标签: matlab machine-learning gradient-descent

我实现了随机梯度下降算法matlab但无法得到正确的结果。首先,我将训练集改组,并一次使用一个样本更新每个重量。有什么错吗?

function [theta, J] = gradientdescent(x, y,theta,alpha,epochs)

m = length(y); 
J = zeros(epochs, 1);

for e = 1:epochs
n = randperm(size(x,1));
input=x(n,:);
target=y(n);
htheta = input * theta;
for i=1:m
for j = 1:size(theta, 1)

    theta(j) = theta(j) - alpha * (htheta(i) - target(i)) * input(i,j);
end  
end

 J(e) = costfunction(x, y, theta);

 end

 end

0 个答案:

没有答案