-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_net_main.m
More file actions
59 lines (52 loc) · 2.04 KB
/
train_net_main.m
File metadata and controls
59 lines (52 loc) · 2.04 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
%% train_net_main.m
%MAT 128b Project 2 - Part 4, 5, 6
%Driver file for train_net.m
clear; clc; close all;
load mnistdata;
%% Initialize neural net parameters
layers = 1; %number of hidden layers [1,inf)
neurons_hidden = 100; %number of neurons per hidden layer
trainingRate = .05; %within the interval [0.01, 0.1]
inputSize = 50000; %number of digit image samples in the input
%Things you can't change
neurons_input = 784; %number of neurons in the input layer
neurons_output = 10; %number of neurons in the output layer
%% Initialize weight matrices
W{1} = (rand(neurons_input, neurons_hidden)*2-1)/10; %INPUT -> HIDDEN
for iter = 1:layers-1
W{iter+1} = (rand(neurons_hidden, neurons_hidden)*2-1)/10; %HIDDEN -> HIDDEN
end
W{end+1} = (rand(neurons_hidden, neurons_output)*2-1)/10; %HIDDEN -> OUTPUT
%% Load training data
train{1} = train0;
train{2} = train1;
train{3} = train2;
train{4} = train3;
train{5} = train4;
train{6} = train5;
train{7} = train6;
train{8} = train7;
train{9} = train8;
train{10} = train9;
%% Initialize the input and train the neural net
disp('Initializing...');
INPUT = zeros(inputSize, 784);
digits = [0 1 2 3 4 5 6 7 8 9];
digits = repmat(digits,1,inputSize/10);
for i = 1:inputSize
INPUT(i,:) = train{digits(i)+1}(ceil(rand()*5400),:);
end
W = train_net(INPUT, digits, W, layers, trainingRate);
clc;
%% Save weight matrices in .mat files
filename = 'W_master.mat';
save(filename, 'W', 'inputSize', 'trainingRate')
disp('--------------- TRAINING COMPLETE ---------------')
disp('Neural Net Parameters:')
disp([' - Number of HIDDEN layers = ' num2str(layers)])
disp([' - Number of INPUT neurons = ' num2str(neurons_input)])
disp([' - Number of HIDDEN neurons/layer = ' num2str(neurons_hidden)])
disp([' - Number of OUTPUT neurons = ' num2str(neurons_output)])
disp([' - Training rate = ' num2str(trainingRate)])
disp([' - Number of training images used = ' num2str(inputSize)])
disp([' - Weight data written to: ' filename])