Skip to content

Instantly share code, notes, and snippets.

@Mizzlr
Created June 29, 2016 11:24
Show Gist options
  • Save Mizzlr/45262d5935c466296887c735ac812dcc to your computer and use it in GitHub Desktop.
Save Mizzlr/45262d5935c466296887c735ac812dcc to your computer and use it in GitHub Desktop.
clear ; close all; clc
% Specify the architecture
input_layer_size = 900; % 30x30 Input Images of Digits
hidden_layer_size = 300; % 300 hidden units
num_labels = 92;
% set maximum number of training iteration as you please
% larger MaxIter results in better accuracy
options = optimset('MaxIter', 10);
lambda = 1; % learning rate for regularization
% load the dataset
X = dlmread('X.csv');
y = dlmread('Y.csv');
m = size(X, 1); % number of samples
% randomly initialize the network
fprintf('\nInitializing Neural Network Parameters ...\n')
initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);
initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];
% start learning from the dataset
fprintf('\nTraining Neural Network... \n')
costFunction = @(p) nnCostFunction(p, ...
input_layer_size, ...
hidden_layer_size, ...
num_labels, X, y, lambda);
% call the optimizer
[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);
% save the learnt parameters
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
hidden_layer_size, (input_layer_size + 1));
Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
num_labels, (hidden_layer_size + 1));
dlmwrite('Theta2.csv',Theta2);
dlmwrite('Theta1.csv',Theta1);
% compute and print training accuracy
pred = predict(Theta1, Theta2, X);
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment