Path: blob/master/Week 5/Programming Assignment - 4/machine-learning-ex4/ex4/ex4.m
864 views
%% Machine Learning Online Class - Exercise 4 Neural Network Learning12% Instructions3% ------------4%5% This file contains code that helps you get started on the6% linear exercise. You will need to complete the following functions7% in this exericse:8%9% sigmoidGradient.m10% randInitializeWeights.m11% nnCostFunction.m12%13% For this exercise, you will not need to change any code in this file,14% or any other files other than those mentioned above.15%1617%% Initialization18clear ; close all; clc1920%% Setup the parameters you will use for this exercise21input_layer_size = 400; % 20x20 Input Images of Digits22hidden_layer_size = 25; % 25 hidden units23num_labels = 10; % 10 labels, from 1 to 1024% (note that we have mapped "0" to label 10)2526%% =========== Part 1: Loading and Visualizing Data =============27% We start the exercise by first loading and visualizing the dataset.28% You will be working with a dataset that contains handwritten digits.29%3031% Load Training Data32fprintf('Loading and Visualizing Data ...\n')3334load('ex4data1.mat');35m = size(X, 1);3637% Randomly select 100 data points to display38sel = randperm(size(X, 1));39sel = sel(1:100);4041displayData(X(sel, :));4243fprintf('Program paused. Press enter to continue.\n');44pause;454647%% ================ Part 2: Loading Parameters ================48% In this part of the exercise, we load some pre-initialized49% neural network parameters.5051fprintf('\nLoading Saved Neural Network Parameters ...\n')5253% Load the weights into variables Theta1 and Theta254load('ex4weights.mat');5556% Unroll parameters57nn_params = [Theta1(:) ; Theta2(:)];5859%% ================ Part 3: Compute Cost (Feedforward) ================60% To the neural network, you should first start by implementing the61% feedforward part of the neural network that returns the cost only. You62% should complete the code in nnCostFunction.m to return cost. After63% implementing the feedforward to compute the cost, you can verify that64% your implementation is correct by verifying that you get the same cost65% as us for the fixed debugging parameters.66%67% We suggest implementing the feedforward cost *without* regularization68% first so that it will be easier for you to debug. Later, in part 4, you69% will get to implement the regularized cost.70%71fprintf('\nFeedforward Using Neural Network ...\n')7273% Weight regularization parameter (we set this to 0 here).74lambda = 0;7576J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...77num_labels, X, y, lambda);7879fprintf(['Cost at parameters (loaded from ex4weights): %f '...80'\n(this value should be about 0.287629)\n'], J);8182fprintf('\nProgram paused. Press enter to continue.\n');83pause;8485%% =============== Part 4: Implement Regularization ===============86% Once your cost function implementation is correct, you should now87% continue to implement the regularization with the cost.88%8990fprintf('\nChecking Cost Function (w/ Regularization) ... \n')9192% Weight regularization parameter (we set this to 1 here).93lambda = 1;9495J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...96num_labels, X, y, lambda);9798fprintf(['Cost at parameters (loaded from ex4weights): %f '...99'\n(this value should be about 0.383770)\n'], J);100101fprintf('Program paused. Press enter to continue.\n');102pause;103104105%% ================ Part 5: Sigmoid Gradient ================106% Before you start implementing the neural network, you will first107% implement the gradient for the sigmoid function. You should complete the108% code in the sigmoidGradient.m file.109%110111fprintf('\nEvaluating sigmoid gradient...\n')112113g = sigmoidGradient([-1 -0.5 0 0.5 1]);114fprintf('Sigmoid gradient evaluated at [-1 -0.5 0 0.5 1]:\n ');115fprintf('%f ', g);116fprintf('\n\n');117118fprintf('Program paused. Press enter to continue.\n');119pause;120121122%% ================ Part 6: Initializing Pameters ================123% In this part of the exercise, you will be starting to implment a two124% layer neural network that classifies digits. You will start by125% implementing a function to initialize the weights of the neural network126% (randInitializeWeights.m)127128fprintf('\nInitializing Neural Network Parameters ...\n')129130initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);131initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);132133% Unroll parameters134initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];135136137%% =============== Part 7: Implement Backpropagation ===============138% Once your cost matches up with ours, you should proceed to implement the139% backpropagation algorithm for the neural network. You should add to the140% code you've written in nnCostFunction.m to return the partial141% derivatives of the parameters.142%143fprintf('\nChecking Backpropagation... \n');144145% Check gradients by running checkNNGradients146checkNNGradients;147148fprintf('\nProgram paused. Press enter to continue.\n');149pause;150151152%% =============== Part 8: Implement Regularization ===============153% Once your backpropagation implementation is correct, you should now154% continue to implement the regularization with the cost and gradient.155%156157fprintf('\nChecking Backpropagation (w/ Regularization) ... \n')158159% Check gradients by running checkNNGradients160lambda = 3;161checkNNGradients(lambda);162163% Also output the costFunction debugging values164debug_J = nnCostFunction(nn_params, input_layer_size, ...165hidden_layer_size, num_labels, X, y, lambda);166167fprintf(['\n\nCost at (fixed) debugging parameters (w/ lambda = %f): %f ' ...168'\n(for lambda = 3, this value should be about 0.576051)\n\n'], lambda, debug_J);169170fprintf('Program paused. Press enter to continue.\n');171pause;172173174%% =================== Part 8: Training NN ===================175% You have now implemented all the code necessary to train a neural176% network. To train your neural network, we will now use "fmincg", which177% is a function which works similarly to "fminunc". Recall that these178% advanced optimizers are able to train our cost functions efficiently as179% long as we provide them with the gradient computations.180%181fprintf('\nTraining Neural Network... \n')182183% After you have completed the assignment, change the MaxIter to a larger184% value to see how more training helps.185options = optimset('MaxIter', 50);186187% You should also try different values of lambda188lambda = 1;189190% Create "short hand" for the cost function to be minimized191costFunction = @(p) nnCostFunction(p, ...192input_layer_size, ...193hidden_layer_size, ...194num_labels, X, y, lambda);195196% Now, costFunction is a function that takes in only one argument (the197% neural network parameters)198[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);199200% Obtain Theta1 and Theta2 back from nn_params201Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...202hidden_layer_size, (input_layer_size + 1));203204Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...205num_labels, (hidden_layer_size + 1));206207fprintf('Program paused. Press enter to continue.\n');208pause;209210211%% ================= Part 9: Visualize Weights =================212% You can now "visualize" what the neural network is learning by213% displaying the hidden units to see what features they are capturing in214% the data.215216fprintf('\nVisualizing Neural Network... \n')217218displayData(Theta1(:, 2:end));219220fprintf('\nProgram paused. Press enter to continue.\n');221pause;222223%% ================= Part 10: Implement Predict =================224% After training the neural network, we would like to use it to predict225% the labels. You will now implement the "predict" function to use the226% neural network to predict the labels of the training set. This lets227% you compute the training set accuracy.228229pred = predict(Theta1, Theta2, X);230231fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);232233234235236