-
Notifications
You must be signed in to change notification settings - Fork 11
/
cnn_mnist_init.m
102 lines (87 loc) · 3.9 KB
/
cnn_mnist_init.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
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
function net = cnn_mnist_init(varargin)
% CNN_MNIST_LENET Initialize a CNN similar for MNIST
opts.useBatchNorm = true ;
opts.networkType = 'simplenn' ;
opts = vl_argparse(opts, varargin) ;
rng('default');
rng(0) ;
f=1/100 ;
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(5,1,1,50, 'single'), zeros(1, 50, 'single')}}, ...
'stride',1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [4 1], ...
'stride', [2 1], ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'normalize', ...
'param', [5 1 0.0001/5 0.75]) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(5,1,50,40, 'single'), zeros(1,40,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [4 1], ...
'stride', [2 1], ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'normalize', ...
'param', [5 1 0.0001/5 0.75]) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(3,1,40,20, 'single'), zeros(1,20,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'normalize', ...
'param', [5 1 0.0001/5 0.75]) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,19,20,400, 'single'), zeros(1,400,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'normalize', ...
'param', [5 1 0.0001/5 0.75]) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,400,20, 'single'), zeros(1,20,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'softmaxloss') ;
% optionally switch to batch normalization
if opts.useBatchNorm
net = insertBnorm(net, 1) ;
net = insertBnorm(net, 4) ;
net = insertBnorm(net, 7) ;
end
% Meta parameters
net.meta.inputSize = [30 19 1] ;
net.meta.trainOpts.learningRate = [0.01*ones(1, 3) 0.001*ones(1, 25)] ;
net.meta.trainOpts.numEpochs = 20 ;
net.meta.trainOpts.batchSize = 12 ;
% Fill in defaul values
net = vl_simplenn_tidy(net) ;
% Switch to DagNN if requested
switch lower(opts.networkType)
case 'simplenn'
% done
case 'dagnn'
net = dagnn.DagNN.fromSimpleNN(net, 'canonicalNames', true) ;
net.addLayer('error', dagnn.Loss('loss', 'classerror'), ...
{'prediction','label'}, 'error') ;
otherwise
assert(false) ;
end
% --------------------------------------------------------------------
function net = insertBnorm(net, l)
% --------------------------------------------------------------------
assert(isfield(net.layers{l}, 'weights'));
ndim = size(net.layers{l}.weights{1}, 4);
layer = struct('type', 'bnorm', ...
'weights', {{ones(ndim, 1, 'single'), zeros(ndim, 1, 'single')}}, ...
'learningRate', [1 1 0.05], ...
'weightDecay', [0 0]) ;
net.layers{l}.biases = [] ;
net.layers = horzcat(net.layers(1:l), layer, net.layers(l+1:end)) ;