| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- %% Initialization
- clear ; close all; clc
- %% Setup the parameters you will use for this exercise
- input_layer_size = 400; % 20x20 Input Images of Digits
- hidden_layer_size = 25; % 25 hidden units
- num_labels = 10; % 10 labels, from 1 to 10
- % (note that we have mapped "0" to label 10)
- %% =========== Part 1: Loading and Visualizing Data =============
- % We start the exercise by first loading and visualizing the dataset.
- % You will be working with a dataset that contains handwritten digits.
- %
- % Load Training Data
- fprintf('Loading and Visualizing Data ...\n')
- load('ex4data1.mat');
- m = size(X, 1);
- % Randomly select 100 data points to display
- sel = randperm(size(X, 1));
- sel = sel(1:100);
- % Load the weights into variables Theta1 and Theta2
- load('ex4weights.mat');
- % Unroll parameters
- nn_params = [Theta1(:) ; Theta2(:)];
- fprintf('\nFeedforward Using Neural Network ...\n')
- % Weight regularization parameter (we set this to 0 here).
- lambda = 0;
- J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...
- num_labels, X, y, lambda);
- fprintf(['Cost at parameters (loaded from ex4weights): %f '...
- '\n(this value should be about 0.287629)\n'], J);
- lambda = 1;
- J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...
- num_labels, X, y, lambda);
- fprintf(['Cost at parameters (loaded from ex4weights)=> lambda = 1: %f '...
- '\n(this value should be about 0.383770)\n'], J);
-
- fprintf('\nProgram paused. Press enter to continue.\n');
- fprintf('\nEvaluating sigmoid gradient...\n')
- g = sigmoidGradient([-1 -0.5 0 0.5 1]);
- fprintf('Sigmoid gradient evaluated at [-1 -0.5 0 0.5 1]:\n ');
- fprintf('%f ', g);
- fprintf('\n\n');
- fprintf('Program paused. Press enter to continue.\n');
- %% ================ Part 6: Initializing Pameters ================
- % In this part of the exercise, you will be starting to implment a two
- % layer neural network that classifies digits. You will start by
- % implementing a function to initialize the weights of the neural network
- % (randInitializeWeights.m)
- fprintf('\nInitializing Neural Network Parameters ...\n')
- initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
- initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);
- % Unroll parameters
- initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];
- %% =============== Part 7: Implement Backpropagation ===============
- % Once your cost matches up with ours, you should proceed to implement the
- % backpropagation algorithm for the neural network. You should add to the
- % code you've written in nnCostFunction.m to return the partial
- % derivatives of the parameters.
- %
- fprintf('\nChecking Backpropagation... \n');
- % Check gradients by running checkNNGradients
- checkNNGradients;
- fprintf('\nProgram paused. Press enter to continue.\n');
- pause;
- %%%%%%https://github.com/rieder91/MachineLearning/blob/master/Exercise%204/ex4/nnCostFunction.m
|