-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathdemo.m
More file actions
65 lines (47 loc) 路 2.08 KB
/
Copy pathdemo.m
File metadata and controls
65 lines (47 loc) 路 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
% Initialization.
clear; close all; clc;
% Load training data ----------------------------------------------------------------
fprintf('Loading data...\n');
load('digits.mat');
m = size(X, 1);
% Plotting some training example ----------------------------------------------------
fprintf('Visualizing data...\n');
% Randomly select 100 data points to display
random_digits_indices = randperm(size(X, 1));
random_digits_indices = random_digits_indices(1:100);
display_data(X(random_digits_indices, :));
% Debug neural network --------------------------------------------------------------
% Check gradients with lambda = 0.
% lambda = 0;
% debug_nn_gradients(lambda);
% fprintf('\nProgram paused. Press enter to continue.\n');
% pause;
% Check gradients with lambda = 3.
% lambda = 3;
% debug_nn_gradients(lambda);
% fprintf('\nProgram paused. Press enter to continue.\n');
% pause;
% Initializing neural network parameters --------------------------------------------
fprintf('Initializing neural network parameters...\n');
% Setup neural network parameter.
layers = [
400, % Input layer - 20x20 Input images of digits.
25, % First hidden layer - 25 hidden units.
% 15, % Second hidden layer - 10 hidden units.
10 % Output layer - 10 labels, from 1 to 10 (note that we have mapped "0" to label 10).
];
% Training neural network -----------------------------------------------------------
fprintf('Training neural network...\n');
% Defines the range for initial theta values.
epsilon = 0.12;
% Regularization parameter.
lambda = 0.01;
% Number of iterations to perform for gradient descent.
max_iterations = 30;
% Train neural network.
[nn_params, cost] = neural_network_train(X, y, layers, lambda, epsilon, max_iterations);
% Calculate training set accuracy ---------------------------------------------------
fprintf('Calculate training set accuracy...\n');
% After training the neural network, we would like to use it to predict the labels.
predictions = neural_network_predict(X, nn_params, layers);
fprintf('Training Set Accuracy: %f\n', mean(double(predictions == y)) * 100);