-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMNIST_ResNet.m
More file actions
161 lines (118 loc) · 3.82 KB
/
Copy pathMNIST_ResNet.m
File metadata and controls
161 lines (118 loc) · 3.82 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
% see more detail in CIFAR_main.m
clear
clc
rand( 'state' , 0 );
randn( 'state' , 0 );
addpath 'CNN_frame'
addpath 'CNN_frame/util'
addpath 'CNN_frame/layer_constructor'
addpath 'CNN_frame/layer_calculator'
addpath 'datasets/MNIST_dataset'
%% -------------------prepare data set----------------------
[ train , test ] = load_MNIST_2d(3);
train_input = {train.X};
train_target = train.T;
test_input = {test.X};
test_target = test.T;
validation_size = 1000;
[validation_input, validation_target] = get_mini_batch(test_input, test_target, 1 : validation_size);
clear train
clear test
input_size = size(train_input{1}(:, :, 1));
input_channel = length(train_input);
test_batch_size = 1000;
%% -------------configure the training strategy-------------
% warming up
config_step1.learning_rate = 0.02;
config_step1.half_life = inf;
config_step1.momentum = 0;
config_step1.weight_decay = 0.0001;
config_step1.batch_size = 30;
config_step1.validate_interval = 200;
config_step1.max_epochs = 1;
config_step1.threshold = 0.002;
% training
config_step2 = config_step1;
config_step2.learning_rate = 0.07;
config_step2.half_life = 8;
config_step2.max_epochs = 4;
% training
config_step3 = config_step2;
config_step3.learning_rate = 0.03;
config_step3.max_epochs = 10;
%% -------------define the structure of CNN----------------
l = 1;
CNN{l}.map_size = input_size;
CNN{l}.output = input_channel;
l = l + 1;
CNN{l}.type = 'residual_block';
CNN{l}.weight.filler.type = 'xavier';
CNN{l}.weight.learning_rate = 1;
CNN{l}.BN_decay = 0.99;
CNN{l}.sampling.option = true;
CNN{l}.sampling.type = 'max';
CNN{l}.sampling.shape = [2, 2];
CNN{l}.sampling.stride = [2, 2];
CNN{l}.output = 10;
l = l + 1;
CNN{l}.type = 'residual_block';
CNN{l}.weight.filler.type = 'xavier';
CNN{l}.weight.learning_rate = 1;
CNN{l}.BN_decay = 0.99;
CNN{l}.sampling.option = true;
CNN{l}.sampling.type = 'max';
CNN{l}.sampling.shape = [2, 2];
CNN{l}.sampling.stride = [2, 2];
CNN{l}.output = 20;
l = l + 1;
CNN{l}.type = 'residual_block';
CNN{l}.weight.filler.type = 'xavier';
CNN{l}.weight.learning_rate = 1;
CNN{l}.BN_decay = 0.99;
CNN{l}.sampling.option = true;
CNN{l}.sampling.type = 'max';
CNN{l}.sampling.shape = [2, 2];
CNN{l}.sampling.stride = [2, 2];
CNN{l}.output = 40;
l = l + 1;
CNN{l}.type = 'full_connection';
CNN{l}.weight.filler.type = 'xavier';
CNN{l}.weight.learning_rate = 1;
CNN{l}.bias.learning_rate = 2;
CNN{l}.dropout.option = true;
CNN{l}.dropout.rate = 0.5;
CNN{l}.output = 80;
l = l + 1;
CNN{l}.type = 'activation';
CNN{l}.activation = 'relu';
l = l + 1;
CNN{l}.type = 'full_connection';
CNN{l}.weight.filler.type = 'xavier';
CNN{l}.weight.learning_rate = 1;
CNN{l}.bias.learning_rate = 2;
CNN{l}.dropout.option= false;
CNN{l}.output = 10;
l = l + 1;
CNN{l}.type = 'activation';
CNN{l}.activation = 'softmax';
CNN{l}.cost_function = 'cross_entropy';
%% -------------initializaing the CNN-------------------
CNN = CNN_initialization(CNN);
%% --------------check the gradient---------------------
% check_size = 5;
% epsilon = 1e-8;
% tolerance = 1e-7;
% CNN_gradient_check(train_input, train_target, CNN, tr_config.cost_function, check_size, epsilon, tolerance);
%% -----------------train the CNN-----------------------
tic;
% training step 1
[CNN, result] = CNN_train(train_input, train_target, validation_input, validation_target, config_step1, CNN);
% training step 2
[CNN, result] = CNN_train(train_input, train_target, validation_input, validation_target, config_step2, CNN);
% training step 3
[CNN, result] = CNN_train(train_input, train_target, validation_input, validation_target, config_step3, CNN);
result.train_time = toc;
%% ----------------test the CNN------------------------
[result.accuracy, result.confusion_matrix] = CNN_test(test_input, test_target, CNN, test_batch_size);
%% ------------save the running log and figure---------
writing_log(CNN, result, config_step2);