-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn_classifier.m
More file actions
75 lines (66 loc) · 1.92 KB
/
Copy pathknn_classifier.m
File metadata and controls
75 lines (66 loc) · 1.92 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
% Change the filenames if you've saved the files under different names
% On some platforms, the files might be saved as
% train-images.idx3-ubyte / train-labels.idx1-ubyte
images = loadMNISTImages('train-resize');
labels = loadMNISTLabels('train-labels-idx1-ubyte');
test_images = loadMNISTImages('t10k-resize');
test_labels = loadMNISTLabels('t10k-labels-idx1-ubyte');
% We are using display_network from the autoencoder code
% display_network(images(:,1:100)); % Show the first 100 images
% disp(labels(1:10));
n = 60000;
% test_n = ceil(n*15/85);
test_n = 10000;
scale = 2;
ori_size = 28;
size = ori_size/scale;
% p = zeros(size*size, n);
% for i = 1:n
% for j = 0:size-1
% for k = 1:size
% p(j*size+k,i) = images(j*ori_size*scale+k*scale,i);
% end
% end
% end
% p = p.';
p = images(:,1:n).';
t = labels(1:n,1);
% test_p = zeros(size*size, test_n);
% for i = 1:test_n
% for j = 0:size-1
% for k = 1:size
% test_p(j*size+k,i) = test_images(j*ori_size*scale+k*scale,i);
% end
% end
% end
% test_p = test_p.';
test_p = test_images(:,1:test_n).';
test_t = zeros(10,test_n);
for i = 1:test_n
test_t(test_labels(i)+1, i) = 1;
end
net = ClassificationKNN.fit(p, t);
[label,score,cost] = predict(net,test_p);
% net = newff(p, t, [100], {'tansig' 'logsig'}, 'trainrp', ...
% '', 'mse', {}, {}, 'divideblock');
% net = init(net);
% % net.trainParam.lr = 2;
% net.trainParam.delta0 = 0.07;
% net.trainParam.deltamax = 100;
% net.trainParam.epochs = 400;
% % net.trainParam.max_fail = 1000;
% % net.trainParam.min_grad = 0;
% % net.trainParam.epochs = 100;
% % net.divideParam.Q = n+test_n;
% net.divideParam.trainRatio = 0.75;
% net.divideParam.valRatio = 0.15;
% net.divideParam.testRatio = 0;
%
%
% [trained_net, stats] = train(net, p, t);
test_o = zeros(10, test_n);
for i = 1:test_n
test_o(label(i)+1, i) = 1;
end
plotconfusion(test_t, test_o);
% rloss = resubLoss(net);