forked from sdemyanov/ConvNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnnexamples.m
68 lines (57 loc) · 2.01 KB
/
cnnexamples.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
close all; clear mex;
addpath('./c++/build');
addpath('./matlab');
addpath('./data');
load mnist;
kSampleDim = ndims(TrainX);
kXSize = size(TrainX);
kXSize(kSampleDim) = [];
kWorkspaceFolder = './workspace';
if (~exist(kWorkspaceFolder, 'dir'))
mkdir(kWorkspaceFolder);
end;
kTrainNum = 10000;
kOutputs = size(TrainY, 2);
train_x = TrainX(:, :, 1:kTrainNum);
train_y = TrainY(1:kTrainNum, :);
kTestNum = 10000;
test_x = TestX(:, :, 1:kTestNum);
test_y = TestY(1:kTestNum, :);
params.seed = 1;
params.batchsize = 50;
params.numepochs = 1;
params.alpha = 1;
params.momentum = 0.9;
params.shuffle = 0;
params.verbose = 0;
dropout = 0;
norm_x = squeeze(mean(sqrt(sum(sum(train_x.^2))), kSampleDim));
% This structure is just supposed to demonstrate the implemented options
layers = {
struct('type', 'i', 'mapsize', kXSize, 'outputmaps', 1, ...
'norm', norm_x, 'mean', 0', 'maxdev', 1);
struct('type', 'c', 'kernelsize', [5 5], 'outputmaps', 6) %convolution layer
struct('type', 's', 'scale', [3 3], 'function', 'mean', 'stride', [2 2]) % subsampling layer
struct('type', 'c', 'kernelsize', [5 5], 'outputmaps', 12, 'padding', [1 1]) %convolution layer
struct('type', 's', 'scale', [3 3], 'function', 'max', 'stride', [2 2]) % subsampling layer
struct('type', 'f', 'length', 64) % fully connected layer
struct('type', 'f', 'length', kOutputs, 'function', 'soft', ...
'dropout', dropout) % fully connected layer
};
funtype = 'mexfun';
%funtype = 'matlab';
rng(params.seed);
weights_in = genweights(layers, params.seed, 'matlab');
EpochNum = 1;
errors = zeros(EpochNum, 1);
weights = weights_in;
for i = 1 : EpochNum
disp(['Epoch: ' num2str(i)])
[weights, trainerr] = cnntrain(layers, weights, train_x, train_y, params, funtype);
plot(trainerr);
disp([num2str(mean(trainerr)) ' loss']);
[err, bad, pred] = cnntest(layers, weights, test_x, test_y, funtype);
disp([num2str(err*100) '% error']);
errors(i) = err;
end;
%save(fullfile(kWorkspaceFolder, 'weights.mat'), 'weights');