0

I implemented stochastic gradient descent algorithm matlab but unable to get proper result. First, I shuffled the training set and update each weight using one sample at a time. Is there any mistake ?

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 Answers0