-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCR.m
46 lines (33 loc) · 1.31 KB
/
OCR.m
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
%% Initialization
clear all; close all; clc;
%% load data and split
% build X and y from data.txt
data = load('data.txt');
X = data(:, 1:100);
y = data(:, 101);
% split to train and test
X_test = X(311:340, :);
y_test = y(311:340, :);
X = removerows(X, 311:340);
y = removerows(y, 311:340);
[m, n] = size(X);
%% training NN
input_layer_size = 100;
hidden_layer_size = 50;
num_labels = 5;
% initialize all_theta
initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);
initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];
% set options
options = optimset('MaxIter', 3000);
% call fmincg
[nn_params, cost] = fmincg(@(p) nnCostFunction(p, input_layer_size, hidden_layer_size, num_labels, X, y), initial_nn_params, options);
% Obtain Theta1 and Theta2 back from nn_params
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));
%% predicting using Theta1 and Theta2
% calculate predictions
pred = predict(Theta1, Theta2, X_test);
% calculate test set accuracy
test_accuracy = mean(double(y_test == pred))