Skip to content

Commit b873028

Browse files
Added plotting function to nntrain. Removed normalizing code from nntrain.
1 parent b09f3eb commit b873028

File tree

11 files changed

+80
-105
lines changed

11 files changed

+80
-105
lines changed

NN/nneval.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
nn = nnff(nn, train_x, train_y);
88
loss.train.e(end + 1) = nn.L;
99

10+
% validation performance
1011
if nargin == 6
11-
12-
% validation performance
1312
nn = nnff(nn, val_x, val_y);
1413
loss.val.e(end + 1) = nn.L;
15-
1614
end
1715

1816
%calc misclassification rate if softmax

NN/nnpredict.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
function labels = nnpredict(nn, x)
2-
if nn.normalize_input==1;
3-
x = (x-repmat(nn.normalizeMean,size(x,1),1))./repmat(nn.normalizeStd,size(x,1),1);
4-
end
5-
62
nn.testing = 1;
73
nn = nnff(nn, x, zeros(size(x,1), nn.size(end)));
84
nn.testing = 0;

NN/nnsetup.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
nn.size = architecture;
77
nn.n = numel(nn.size);
88

9-
nn.normalize_input = 1; % normalize input elements to be between [-1 1]. Note: use a linear output function if training auto-encoders with normalized inputs
109
nn.activation_function = 'tanh_opt'; % Activation functions of hidden layers: 'sigm' (sigmoid) or 'tanh_opt' (optimal tanh).
1110
nn.learningRate = 2; % learning rate Note: typically needs to be lower when using 'sigm' activation function and non-normalized inputs.
1211
nn.momentum = 0.5; % Momentum

NN/nntrain.m

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,20 @@
1111

1212
loss.train.e = [];
1313
loss.train.e_frac = [];
14-
if nargin == 4 % training data
15-
opts.validation = 0;
16-
17-
else %training data and validation data
14+
loss.val.e = [];
15+
loss.val.e_frac = [];
16+
opts.validation = 0;
17+
if nargin == 6
1818
opts.validation = 1;
19-
loss.val.e = [];
20-
loss.val.e_frac = [];
2119
end
2220

23-
if ~isfield(opts,'plot')
24-
fhandle = [];
25-
elseif opts.plot == 1
21+
fhandle = [];
22+
if isfield(opts,'plot') && opts.plot == 1
2623
fhandle = figure();
27-
else
28-
fhandle = [];
2924
end
3025

31-
3226
m = size(train_x, 1);
3327

34-
if nn.normalize_input==1
35-
[train_x, mu, sigma] = zscore(train_x);
36-
nn.normalizeMean = mu;
37-
sigma(sigma==0) = 0.0001;%this should be very small value.
38-
nn.normalizeStd = sigma;
39-
end
40-
41-
4228
batchsize = opts.batchsize;
4329
numepochs = opts.numepochs;
4430

@@ -74,14 +60,12 @@
7460
t = toc;
7561

7662
if ishandle(fhandle)
77-
7863
if opts.validation == 1
79-
loss = nneval(nn,loss,train_x,train_y,val_x,val_y);
64+
loss = nneval(nn, loss, train_x, train_y, val_x, val_y);
8065
else
81-
loss = nneval(nn,loss,train_x,train_y);
66+
loss = nneval(nn, loss, train_x, train_y);
8267
end
83-
84-
nnupdatefigures(nn,fhandle,loss,opts,i);
68+
nnupdatefigures(nn, fhandle, loss, opts, i);
8569
end
8670

8771
disp(['epoch ' num2str(i) '/' num2str(opts.numepochs) '. Took ' num2str(t) ' seconds' '. Mean squared error on training set is ' num2str(mean(L((n-numbatches):(n-1))))]);

NN/nnupdatefigures.m

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,25 @@ function nnupdatefigures(nn,fhandle,L,opts,i)
1111

1212
%create data for plots
1313
if strcmp(nn.output,'softmax')
14-
1514
plot_x = x_ax';
1615
plot_ye = L.train.e';
1716
plot_yfrac = L.train.e_frac';
1817

1918
else
20-
2119
plot_x = x_ax';
2220
plot_ye = L.train.e';
23-
2421
end
2522

2623
%add error on validation data if present
2724
if opts.validation == 1
28-
2925
plot_x = [plot_x, x_ax'];
3026
plot_ye = [plot_ye,L.val.e'];
31-
3227
end
3328

3429

3530
%add classification error on validation data if present
3631
if opts.validation == 1 && strcmp(nn.output,'softmax')
37-
38-
plot_yfrac = [plot_yfrac, L.val.e_frac'];
39-
32+
plot_yfrac = [plot_yfrac, L.val.e_frac'];
4033
end
4134

4235
% plotting
@@ -54,7 +47,6 @@ function nnupdatefigures(nn,fhandle,L,opts,i)
5447
set(gca,'LegendColorbarListeners',[]);
5548
setappdata(gca,'LegendColorbarManualSpace',1);
5649
setappdata(gca,'LegendColorbarReclaimSpace',1);
57-
5850
end
5951

6052
p2 = subplot(1,2,2);
@@ -68,7 +60,6 @@ function nnupdatefigures(nn,fhandle,L,opts,i)
6860
set(gca,'LegendColorbarListeners',[]);
6961
setappdata(gca,'LegendColorbarManualSpace',1);
7062
setappdata(gca,'LegendColorbarReclaimSpace',1);
71-
7263
end
7364

7465
else

README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ dbn = dbntrain(dbn, train_x, opts);
9191
9292
%unfold dbn to nn
9393
nn = dbnunfoldtonn(dbn, 10);
94-
nn.normalize_input = 0;
9594
nn.activation_function = 'sigm';
9695
9796
%train nn
@@ -121,7 +120,6 @@ test_y = double(test_y);
121120
% Setup and train a stacked denoising autoencoder (SDAE)
122121
rng(0);
123122
sae = saesetup([784 100]);
124-
sae.ae{1}.normalize_input = 0;
125123
sae.ae{1}.activation_function = 'sigm';
126124
sae.ae{1}.learningRate = 1;
127125
sae.ae{1}.inputZeroMaskedFraction = 0.5;
@@ -132,7 +130,6 @@ visualize(sae.ae{1}.W{1}(:,2:end)')
132130
133131
% Use the SDAE to initialize a FFNN
134132
nn = nnsetup([784 100 10]);
135-
nn.normalize_input = 0;
136133
nn.activation_function = 'sigm';
137134
nn.learningRate = 1;
138135
nn.W{1} = sae.ae{1}.W{1};
@@ -148,12 +145,10 @@ assert(er < 0.16, 'Too big error');
148145
% Setup and train a stacked denoising autoencoder (SDAE)
149146
rng(0);
150147
sae = saesetup([784 100 100]);
151-
sae.ae{1}.normalize_input = 0;
152148
sae.ae{1}.activation_function = 'sigm';
153149
sae.ae{1}.learningRate = 1;
154150
sae.ae{1}.inputZeroMaskedFraction = 0.5;
155151
156-
sae.ae{2}.normalize_input = 0;
157152
sae.ae{2}.activation_function = 'sigm';
158153
sae.ae{2}.learningRate = 1;
159154
sae.ae{2}.inputZeroMaskedFraction = 0.5;
@@ -165,7 +160,6 @@ visualize(sae.ae{1}.W{1}(:,2:end)')
165160
166161
% Use the SDAE to initialize a FFNN
167162
nn = nnsetup([784 100 100 10]);
168-
nn.normalize_input = 0;
169163
nn.activation_function = 'sigm';
170164
nn.learningRate = 1;
171165
@@ -236,6 +230,10 @@ test_x = double(test_x) / 255;
236230
train_y = double(train_y);
237231
test_y = double(test_y);
238232
233+
% normalize
234+
[train_x, mu, sigma] = zscore(train_x);
235+
test_x = normalize(test_x, mu, sigma);
236+
239237
%% ex1 vanilla neural net
240238
rng(0);
241239
nn = nnsetup([784 100 10]);
@@ -282,18 +280,48 @@ nn = nntrain(nn, train_x, train_y, opts);
282280
[er, bad] = nntest(nn, test_x, test_y);
283281
assert(er < 0.1, 'Too big error');
284282
285-
%% ex4 neural net with sigmoid activation function, and without normalizing inputs
283+
%% ex4 neural net with sigmoid activation function
286284
rng(0);
287285
nn = nnsetup([784 100 10]);
288286
289287
nn.activation_function = 'sigm'; % Sigmoid activation function
290-
nn.normalize_input = 0; % Don't normalize inputs
291-
nn.learningRate = 1; % Sigm and non-normalized inputs require a lower learning rate
288+
nn.learningRate = 1; % Sigm require a lower learning rate
292289
opts.numepochs = 1; % Number of full sweeps through data
293290
opts.batchsize = 100; % Take a mean gradient step over this many samples
294291
295292
nn = nntrain(nn, train_x, train_y, opts);
296293
294+
[er, bad] = nntest(nn, test_x, test_y);
295+
assert(er < 0.1, 'Too big error');
296+
297+
%% ex5 plotting functionality
298+
rng(0);
299+
nn = nnsetup([784 20 10]);
300+
opts.numepochs = 5; % Number of full sweeps through data
301+
nn.output = 'softmax'; % use softmax output
302+
opts.batchsize = 1000; % Take a mean gradient step over this many samples
303+
opts.plot = 1; % enable plotting
304+
305+
nn = nntrain(nn, train_x, train_y, opts);
306+
307+
[er, bad] = nntest(nn, test_x, test_y);
308+
assert(er < 0.1, 'Too big error');
309+
310+
%% ex6 neural net with sigmoid activation and plotting of validation and training error
311+
% split training data into training and validation data
312+
vx = train_x(1:10000,:);
313+
tx = train_x(10001:end,:);
314+
vy = train_y(1:10000,:);
315+
ty = train_y(10001:end,:);
316+
317+
rng(0);
318+
nn = nnsetup([784 20 10]);
319+
nn.output = 'softmax'; % use softmax output
320+
opts.numepochs = 5; % Number of full sweeps through data
321+
opts.batchsize = 1000; % Take a mean gradient step over this many samples
322+
opts.plot = 1; % enable plotting
323+
nn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)
324+
297325
[er, bad] = nntest(nn, test_x, test_y);
298326
assert(er < 0.1, 'Too big error');
299327
```

tests/test_example_DBN.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
%unfold dbn to nn
3232
nn = dbnunfoldtonn(dbn, 10);
33-
nn.normalize_input = 0;
3433
nn.activation_function = 'sigm';
3534

3635
%train nn

tests/test_example_NN.m

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
train_y = double(train_y);
77
test_y = double(test_y);
88

9+
% normalize
10+
[train_x, mu, sigma] = zscore(train_x);
11+
test_x = normalize(test_x, mu, sigma);
12+
913
%% ex1 vanilla neural net
1014
rng(0);
1115
nn = nnsetup([784 100 10]);
@@ -52,13 +56,12 @@
5256
[er, bad] = nntest(nn, test_x, test_y);
5357
assert(er < 0.1, 'Too big error');
5458

55-
%% ex4 neural net with sigmoid activation function, and without normalizing inputs
59+
%% ex4 neural net with sigmoid activation function
5660
rng(0);
5761
nn = nnsetup([784 100 10]);
5862

5963
nn.activation_function = 'sigm'; % Sigmoid activation function
60-
nn.normalize_input = 0; % Don't normalize inputs
61-
nn.learningRate = 1; % Sigm and non-normalized inputs require a lower learning rate
64+
nn.learningRate = 1; % Sigm require a lower learning rate
6265
opts.numepochs = 1; % Number of full sweeps through data
6366
opts.batchsize = 100; % Take a mean gradient step over this many samples
6467

@@ -70,44 +73,30 @@
7073
%% ex5 plotting functionality
7174
rng(0);
7275
nn = nnsetup([784 20 10]);
73-
nn.normalize_input = 1;
74-
nn.activation_function = 'tanh_opt'; % tanh activation function
75-
nn.normalize_input = 1; % Normalize inputs to range [-1,1]
76-
nn.learningRate = 0.1; % Decrearse learning rate to get smooth decrease in errors
77-
opts.numepochs = 20; % Number of full sweeps through data
78-
opts.batchsize = 100; % Take a mean gradient step over this many samples
79-
opts.plot = 1; % enable plotting
76+
opts.numepochs = 5; % Number of full sweeps through data
77+
nn.output = 'softmax'; % use softmax output
78+
opts.batchsize = 1000; % Take a mean gradient step over this many samples
79+
opts.plot = 1; % enable plotting
8080

8181
nn = nntrain(nn, train_x, train_y, opts);
8282

8383
[er, bad] = nntest(nn, test_x, test_y);
8484
assert(er < 0.1, 'Too big error');
8585

8686
%% ex6 neural net with sigmoid activation and plotting of validation and training error
87-
88-
% create train,val and test set
89-
% splits: 10000-10000-50000
90-
load mnist_uint8;
91-
92-
val_x = double(train_x(1:10000,:)) / 255;
93-
train_x = double(train_x(10001:end,:)) / 255;
94-
test_x = double(test_x)/255;
95-
96-
val_y = double(train_y(1:10000,:));
97-
train_y = double(train_y(10001:end,:));
98-
test_y = double(test_y);
99-
87+
% split training data into training and validation data
88+
vx = train_x(1:10000,:);
89+
tx = train_x(10001:end,:);
90+
vy = train_y(1:10000,:);
91+
ty = train_y(10001:end,:);
10092

10193
rng(0);
10294
nn = nnsetup([784 20 10]);
103-
nn.normalize_input = 0; % dont normalize because we use sigmoid activation
104-
nn.activation_function = 'sigm'; % use sigmoid activation
105-
nn.output = 'softmax'; % use softmax output
106-
nn.learningRate = 0.1; % Decrease learning rate, otherwise the errors does not decrease nicely
107-
opts.numepochs = 20; % Number of full sweeps through data
108-
opts.batchsize = 100; % Take a mean gradient step over this many samples
109-
opts.plot = 1; % enable plotting
110-
nn = nntrain(nn, train_x, train_y, opts,val_x,val_y); % nntrain takes validation set as last two arguments (optionally)
95+
nn.output = 'softmax'; % use softmax output
96+
opts.numepochs = 5; % Number of full sweeps through data
97+
opts.batchsize = 1000; % Take a mean gradient step over this many samples
98+
opts.plot = 1; % enable plotting
99+
nn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)
111100

112101
[er, bad] = nntest(nn, test_x, test_y);
113-
assert(er < 0.06, 'Too big error');
102+
assert(er < 0.1, 'Too big error');

tests/test_example_SAE.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
% Setup and train a stacked denoising autoencoder (SDAE)
1111
rng(0);
1212
sae = saesetup([784 100]);
13-
sae.ae{1}.normalize_input = 0;
1413
sae.ae{1}.activation_function = 'sigm';
1514
sae.ae{1}.learningRate = 1;
1615
sae.ae{1}.inputZeroMaskedFraction = 0.5;
@@ -21,7 +20,6 @@
2120

2221
% Use the SDAE to initialize a FFNN
2322
nn = nnsetup([784 100 10]);
24-
nn.normalize_input = 0;
2523
nn.activation_function = 'sigm';
2624
nn.learningRate = 1;
2725
nn.W{1} = sae.ae{1}.W{1};
@@ -37,12 +35,10 @@
3735
% Setup and train a stacked denoising autoencoder (SDAE)
3836
rng(0);
3937
sae = saesetup([784 100 100]);
40-
sae.ae{1}.normalize_input = 0;
4138
sae.ae{1}.activation_function = 'sigm';
4239
sae.ae{1}.learningRate = 1;
4340
sae.ae{1}.inputZeroMaskedFraction = 0.5;
4441

45-
sae.ae{2}.normalize_input = 0;
4642
sae.ae{2}.activation_function = 'sigm';
4743
sae.ae{2}.learningRate = 1;
4844
sae.ae{2}.inputZeroMaskedFraction = 0.5;
@@ -54,7 +50,6 @@
5450

5551
% Use the SDAE to initialize a FFNN
5652
nn = nnsetup([784 100 100 10]);
57-
nn.normalize_input = 0;
5853
nn.activation_function = 'sigm';
5954
nn.learningRate = 1;
6055

0 commit comments

Comments
 (0)