Answered! Computer exercises Several of the exercises use the data in the following table….

Computer exercises
Several of the exercises use the data in the following table.

W3 sample 0.1 1.1 7.1 4.2 3.0 2.9 6.8 7.1 1.4 4.3 0.5 8.7 8.9 0.2 -3.5 4.1 4.5 0.0 2.9 2.1 4.2 7.7 2.0 2.7 6.3 1.6 0.1 5.2 4.1 2.8 4.2 1.9 4.0 2.2 -6.7 -4.0 3.1 5.0 1.4 3.2 1.3 3.7 0.5 9.2 -0.8 1.3 2.4 4.0 3.4 6.2 0.9 1.2 2.5 6.1 4.1 3.4 5.0 6.4 8.4 3.7 5.1 1.6 7.1 9.7 10 3.9 4.0 4.1 2.2 1.9 5.1 -8.0 -6.3

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Computer exercises Several of the exercises use the data in the following table….
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

Section 5.4

1. Consider basic gradient descent (Algorithm 1) and Newton’s algorithm (Algorithm 2) applied to the data in the tables.

(a) Apply both to the three-dimensional data in categories ω1 and ω3. For the gradient descent use η(k) = 0.1. Plot the criterion function as function of the iteration number.

(b) Estimate the total number of mathematical operations in the two algorithms.

(c) Plot the convergence time versus learning rate. What is the minimum learning rate that fails to lead to convergence?

Expert Answer

 MAIN.M

% Loading the dataset

dataSet = load(‘DataSet.txt’);

% Storing the values in seperate matrices

W1 = dataSet(:, 1);

w3 = dataSet(:, 2);

% Do you want feature normalization?
 
normalization = true;
 
% Applying mean normalization to our dataset
 
if (normalization)
 
    maxX = max(W1);
    minX = min(W1);
   W1= (W1 - maxX) / (maxX - minX);
 
End
 
% Running gradient descent on the data
 
% 'w1' is our input matrix
 
% 'w3' is our output matrix
 
% 'parameters' is a matrix containing our initial theta and slope
 
parameters = [0; 0];
 
learningRate = 0.1;
 
iteration = 1500;
 
[parameters, criterionHistory] = gradient(w1, w3, parameters, learningRate, iteration);
% Plotting our final hypothesis
 
figure;
 
plot(min(w1 (:, 2)):max(w1 (:, 2)), parameters(1) + parameters(2) * (min(w1 (:, 2)):max(w1 (:, 2))));
 
hold on;
% Plotting our criterion function on a different figure to see how we did
 
figure;
 
plot(criterionHistory, 1:iteration);
 
% Finally predicting the output of the provided input
 
input = 120;
 
if (normalization)
    input = (input - maxX) / (maxX - minX);
end
 
output = parameters(1) + parameters(2) * input;
 
disp(output);

gradient.m

function [ parameters, criterionHistory ] = gradient(w1, w3, parameters, learningRate, iteration )

% Getting the length of our dataset
 
m = length(w3);
 
% Creating a matrix of zeros for storing our criterion function history
 
criterionHistory = zeros(iteration, 1); 

% Running gradient descent

for i = 1:iteration

% Calculating the transpose of our hypothesis

h = (w1* parameters – w3′;

% Updating the parameters

parameters(1) = parameters(1) – learningRate * (1/m) * h * w1(:, 1);

parameters(2) = parameters(2) – learningRate * (1/m) * h * w1 (:, 2);

% Keeping track of the criterion function

criterionHistory(i) = criterion(w1, w3, parameters);

end

criterion.m

parameters(1) = parameters(1) - learningRate * (1/m) * h * w1(:, 1);
parameters(2) = parameters(2) - learningRate * (1/m) * h * w1(:, 2);
function [ criterion ] = criterion( w1, w3, parameters )
 
    %   Calculates the criterion function
 
    criterion = (w1 * parameters – w3)' * (w1 * parameters – w3) / (2 * length(w3));    
 
end

Still stressed from student homework?
Get quality assistance from academic writers!