in Education by
I'm trying to implement stochastic gradient descent in MATLAB however I am not seeing any convergence. Mini-batch gradient descent worked as expected so I think that the cost function and gradient steps are correct. The two main issues I am having are: Randomly shuffling the data in the training set before the for-loop Selecting one example at a time Here is my MATLAB code: Generating Data alpha = 0.001; num_iters = 10; xrange =(-10:0.1:10); % data lenght ydata = 5*(xrange)+30; % data with gradient 2, intercept 5 % plot(xrange,ydata); grid on; noise = (2*randn(1,length(xrange))); % generating noise target = ydata + noise; % adding noise to data f1 = figure subplot(2,2,1); scatter(xrange,target); grid on; hold on; % plot a scttaer title('Linear Regression') xlabel('xrange') ylabel('ydata') tita0 = randn(1,1); %intercept (randomised) tita1 = randn(1,1); %gradient (randomised) % Initialize Objective Function History J_history = zeros(num_iters, 1); % Number of training examples m = (length(xrange)); Shuffling data, Gradient Descent and Cost Function % STEP1 : we shuffle the data data = [ xrange, ydata]; data = data(randperm(size(data,1)),:); y = data(:,1); X = data(:,2:end); for iter = 1:num_iters for i = 1:m x = X(:,i); % STEP2 Select one example h = tita0 + tita1.*x; % building the estimated %Changed to xrange in BGD %c = (1/(2*length(xrange)))*sum((h-target).^2) temp0 = tita0 - alpha*((1/m)*sum((h-target))); temp1 = tita1 - alpha*((1/m)*sum((h-target).*x)); %Changed to xrange in BGD tita0 = temp0; tita1 = temp1; fprintf("here\n %d; %d", i, x) end J_history(iter) = (1/(2*m))*sum((h-target).^2); % Calculating cost from data to estimate fprintf('Iteration #%d - Cost = %d... \r\n',iter, J_history(iter)); end On plotting the cost vs iterations and linear regression graphs, the MSE settles (local minimum?) at around 420 which is wrong. On the other hand if I re-run the exact same code however using batch gradient descent I get acceptable results. In batch gradient descent I am changing x to xrange: Any suggestions on what I am doing wrong? EDIT: I also tried selecting random indexes using: f = round(1+rand(1,1)*201); %generating random indexes and then selecting one example: x = xrange(f); % STEP2 Select one example Proceeding to use x in the hypothesis and GD steps also yield a cost of 420. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Firstly we have to shuffle the data as it has two main advantages: Improve the ML model quality Improve predictive performance This is how you will shuffle your data: data = [ xrange', target']; data = data(randperm(size(data,1)),:); Now we have to index X and y correctly: y = data(:,2); X = data(:,1); Then during gradient descent, you need to update based on a single value not on target: tita0 = tita0 - alpha*((1/m)*((h-y(i)))); tita1 = tita1 - alpha*((1/m)*((h-y(i)).*x)); Theta converges to [5, 30] with the changes above.

Related questions

0 votes
    I'm working through my AI textbook I got and I've come to the last homework problem for my section: "Implement the ... in C# or Java? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am currently working on an AI Agent that will be able to identify both the start state and the goal ... be greatly appreciated. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I try to learn and implement a simple genetic algorithm library for my project. At this time, evolution, ... Gaussian distribution?.) Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    The problem is that I cannot make a script that makes the enemy rotate so that "up" is pointing against the ... something simple... Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    From what I've read so far they seem very similar. Differential evolution uses floating point numbers instead, and ... of both. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I write programs to play a board game variants sometimes. The basic strategy is standard alpha-beta pruning or ... human players. Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    In the MNIST beginner tutorial, there is the statement accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) tf ... (x,1)? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am looking for an open source neural network library. So far, I have looked at FANN, WEKA, and OpenNN. Are the ... , and ease of use. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    What is the difference between informed and uninformed searches? Can you explain this with some examples? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Like lots of you guys on SO, I often write in several languages. And when it comes to planning stuff, (or ... to this being possible? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm looking for some examples of robot/AI programming using Lisp. Are there any good online examples available ... in nature)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm teaching a kid programming, and am introducing some basic artificial intelligence concepts at the moment. To begin ... and boxes)? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am searching for information on algorithms to process text sentences or to follow a structure when creating sentences ... be great. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm looking to try and write a chess AI. Is there something I can use on the .NET framework (or maybe ... making a chess game? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm writing a game that's a variant of Gomoku. Basically a tic tac toe on a huge board. Wondering if anyone ... [self put randomly]; } Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
...