Implementing "Gradient Descent Algorithm" in Matlab -



Implementing "Gradient Descent Algorithm" in Matlab -

i'm solving programming assignment in machine learning course. in i've implement gradient descent algorithm below

i'm using next code in matlab

data = load('ex1data1.txt'); % text file conatins 2 values in each row separated commas x = [ones(m, 1), data(:,1)]; theta = zeros(2, 1); iterations = 1500; alpha = 0.01; function [theta, j_history] = gradientdescent(x, y, theta, alpha, num_iters) m = length(y); % number of training examples j_history = zeros(num_iters, 1); iter = 1:num_iters k=1:m; j1=(1/m)*sum((theta(1)+theta(2).*x(k,2))-y(k)) j2=((1/m)*sum((theta(1)+theta(2).*x(k,2))-y(k)))*(x(k,2)) theta(1)=theta(1)-alpha*(j1); theta(2)=theta(2)-alpha*(j2); j_history(iter) = computecost(x, y, theta); end end theta = gradientdescent(x, y, theta, alpha, iterations);

on running above code i'm getting error message

its clear error message that, result of below expression

((1/m)*sum((theta(1)+theta(2).*x(k,2))-y(k)))*(x(k,2))

is vector, trying save in scalar variable j2. think x(k,2) creating problem, i've used index vector x fetch value kth row , 2nd column. on other hand whole vector getting multiplied, please suggest me how can prepare it.

you should larn read error messages, , follow leads there:

as stated in error message, number of elements on left-hand-side , right-hand-side of line theta(2)=theta(2)-alpha*(j2); not same, seek spot 1 is. standard trick disp(size(...)) various terms of look on line before check if things have size expect them be.

some farther reasoning: theta(2) , alpha appear scalars, j2 non-scalar.

looking @ definition of j2, seems sum(...) scalar, while final (x(k,2)) vector of size (m,1), j2 of size (m,1), while should scalar. error need include x(k,2) part sum, end result scalar.

some other observations:

you create first column of x ones, while later utilize sec column. easier utilize x = data(:,1) , utilize that.

you m=length(y); k=1:m; , utilize y(k) several times. easier utilize y ...

you need like

theta(1)=theta(1) - alpha / m * sum(whatever - y); theta(2)=theta(2) - alpha / m * sum((whatever - y) .* x);

you need figure out rest ...

algorithm matlab

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -