Skip to content

Commit becb20d

Browse files
Merge pull request rasmusbergpalm#66 from albertoandreottiATgmail/convnfix
convnfix
2 parents 1e4e950 + 3dee641 commit becb20d

File tree

8 files changed

+32
-15
lines changed

8 files changed

+32
-15
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
before_script:
2-
- sudo apt-get install octave
3-
2+
- sudo apt-add-repository ppa:octave/stable --yes
3+
- sudo apt-get update -y
4+
- sudo apt-get install octave -y
5+
- sudo apt-get install liboctave-dev -y
46
script:
57
- sh -c "octave tests/runalltests.m"
68

CNN/cnnbp.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function net = cnnbp(net, y)
22
n = numel(net.layers);
33

4-
% error
4+
% error
55
net.e = net.o - y;
66
% loss function
77
net.L = 1/2* sum(net.e(:) .^ 2) / size(net.e, 2);
@@ -41,7 +41,12 @@
4141
if strcmp(net.layers{l}.type, 'c')
4242
for j = 1 : numel(net.layers{l}.a)
4343
for i = 1 : numel(net.layers{l - 1}.a)
44-
net.layers{l}.dk{i}{j} = convn(flipall(net.layers{l - 1}.a{i}), net.layers{l}.d{j}, 'valid') / size(net.layers{l}.d{j}, 3);
44+
%ugly if, until convn() is fixed in Octave.
45+
if(isOctave())
46+
net.layers{l}.dk{i}{j} = convn_valid(flipall(net.layers{l - 1}.a{i}), net.layers{l}.d{j}) / size(net.layers{l}.d{j}, 3);
47+
else
48+
net.layers{l}.dk{i}{j} = convn(flipall(net.layers{l - 1}.a{i}), net.layers{l}.d{j}, 'valid') / size(net.layers{l}.d{j}, 3);
49+
end
4550
end
4651
net.layers{l}.db{j} = sum(net.layers{l}.d{j}(:)) / size(net.layers{l}.d{j}, 3);
4752
end

CNN/cnnnumgradcheck.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ function cnnnumgradcheck(net, x, y)
1111
d = (net_p.L - net_m.L) / (2 * epsilon);
1212
e = abs(d - net.dffb(j));
1313
if e > er
14-
e
15-
d / net.dffb(j)
1614
error('numerical gradient checking failed');
1715
end
1816
end
@@ -27,8 +25,6 @@ function cnnnumgradcheck(net, x, y)
2725
d = (net_p.L - net_m.L) / (2 * epsilon);
2826
e = abs(d - net.dffW(i, u));
2927
if e > er
30-
e
31-
d / net.ffW(i, u)
3228
error('numerical gradient checking failed');
3329
end
3430
end
@@ -45,8 +41,6 @@ function cnnnumgradcheck(net, x, y)
4541
d = (net_p.L - net_m.L) / (2 * epsilon);
4642
e = abs(d - net.layers{l}.db{j});
4743
if e > er
48-
e
49-
d / net.layers{l}.db{j}
5044
error('numerical gradient checking failed');
5145
end
5246
for i = 1 : numel(net.layers{l - 1}.a)

CNN/cnnsetup.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
function net = cnnsetup(net, x, y)
2-
assert(exist('OCTAVE_VERSION')==0, 'CNNs does not work with Octave as there is a bug in the implementation of convolution in octave. See: http://savannah.gnu.org/bugs/?39314');
32
inputmaps = 1;
43
mapsize = size(squeeze(x(:, :, 1)));
54

CNN/convn_valid.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%Convolution for 3 dimensional vectors using conv2
2+
%equivalent to convn(A,B, 'valid')
3+
4+
function result = convn_valid(A, B)
5+
m = size(A, 1) - size(B, 1) + 1;
6+
result = zeros(m, m);
7+
B = flipdim(B,3);
8+
for j=1:size(A, 3)
9+
result += conv2(A(:,:,j),B(:,:,j), 'valid');
10+
end
11+
end

tests/octave-core

116 Bytes
Binary file not shown.

tests/test_example_CNN.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@
99
%% ex1 Train a 6c-2s-12c-2s Convolutional neural network
1010
%will run 1 epoch in about 200 second and get around 11% error.
1111
%With 100 epochs you'll get around 1.2% error
12+
1213
rand('state',0)
14+
1315
cnn.layers = {
1416
struct('type', 'i') %input layer
1517
struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer
1618
struct('type', 's', 'scale', 2) %sub sampling layer
1719
struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer
1820
struct('type', 's', 'scale', 2) %subsampling layer
1921
};
20-
cnn = cnnsetup(cnn, train_x, train_y);
22+
2123

2224
opts.alpha = 1;
23-
opts.batchsize = 50;
24-
opts.numepochs = 1;
25+
opts.batchsize = 200;
26+
opts.numepochs = 7;
2527

28+
cnn = cnnsetup(cnn, train_x, train_y);
2629
cnn = cnntrain(cnn, train_x, train_y, opts);
2730

2831
[er, bad] = cnntest(cnn, test_x, test_y);
2932

3033
%plot mean squared error
3134
figure; plot(cnn.rL);
32-
3335
assert(er<0.12, 'Too big error');

util/isOctave.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
%detects if we're running Octave
2+
function result = isOctave()
3+
result = exist('OCTAVE_VERSION') ~= 0;
4+
end

0 commit comments

Comments
 (0)