-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_custom_small256.m
More file actions
442 lines (347 loc) · 13.7 KB
/
Copy pathencoder_custom_small256.m
File metadata and controls
442 lines (347 loc) · 13.7 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
%% Parameters
noi_0 = [3]; %Digits in Class 1 (+1)
noi_1 = [8]; %Digits in Class 2 (-1)
img_size = [16, 16]; %Image dimensions
l = 20; %Length of label vector
a = 1; %Label vector scalar
epochs = 10; %Autoencoder epochs
display_num = 200; %Test point to display
step_size = 10;
%% Setting data up
% load mnist into workspace
mnist = load('mnist256.mat').mnist;
label = mnist(:,1);
X = mnist(:,(2:end));
%Setting up training and testing data and labels
num_idx0 = zeros(length(label), 1);
for i = noi_0
num_idx0 = num_idx0 + (label == i);
end
num_idx0 = logical(num_idx0);
num_idx1 = zeros(length(label), 1);
for i = noi_1
num_idx1 = num_idx1 + (label == i);
end
num_idx1 = logical(num_idx1);
X0 = X(num_idx0,:);
X1 = X(num_idx1,:);
n0_temp = floor(3*length(X0) / 4);
n1_temp = floor(3*length(X1) / 4);
X_train = [X0(1:n0_temp, :); X1(1:n1_temp, :)]';
X_test = [X0(n0_temp+1:end, :); X1(n1_temp+1:end, :)]';
[d,n_train] = size(X_train);
[~,n_test] = size(X_test);
temp0 = label(num_idx0);
temp1 = label(num_idx1);
Y_train = [zeros(n0_temp, 1) + 1; zeros(n1_temp, 1) - 1];
Y_train_ext = Y_train; %[Y_train; Y_train];
Y_test = [zeros(length(temp0) - n0_temp, 1) + 1; zeros(length(temp1) - n1_temp, 1) - 1];
X_train_custom = [X_train; repmat(Y_train', l, 1)]; %X_train_custom = [X_train, X_train; repmat(Y_train', l, 1), zeros(l, n_train)];
X_test_custom = [X_test; zeros(l, n_test)];
[d_custom,n_train_custom] = size(X_train_custom);
[~,n_test_custom] = size(X_test_custom);
%% Creating CCR and MSE vecs for plotting
CCR_train_lda_vec = [];
CCR_test_lda_vec = [];
CCR_train_svmLin_vec = [];
CCR_test_svmLin_vec = [];
CCR_train_svmKern_vec = [];
CCR_test_svmKern_vec = [];
MSE_train_vec = [];
MSE_test_vec = [];
CCR_train_lda_vec_reconstructed = [];
CCR_test_lda_vec_reconstructed = [];
CCR_train_svmLin_vec_reconstructed = [];
CCR_test_svmLin_vec_reconstructed = [];
CCR_train_svmKern_vec_reconstructed = [];
CCR_test_svmKern_vec_reconstructed = [];
CCR_train_lda_vec_custom = [];
CCR_test_lda_vec_custom = [];
CCR_train_svmLin_vec_custom = [];
CCR_test_svmLin_vec_custom = [];
CCR_train_svmKern_vec_custom = [];
CCR_test_svmKern_vec_custom = [];
MSE_train_vec_custom = [];
MSE_test_vec_custom = [];
CCR_train_lda_vec_custom_reconstructed = [];
CCR_test_lda_vec_custom_reconstructed = [];
CCR_train_svmLin_vec_custom_reconstructed = [];
CCR_test_svmLin_vec_custom_reconstructed = [];
CCR_train_svmKern_vec_custom_reconstructed = [];
CCR_test_svmKern_vec_custom_reconstructed = [];
CCR_test_bottomrows_custom_reconstructed = [];
CCR_train_svmKern_vec_custom_0s = [];
CCR_test_svmKern_vec_custom_0s = [];
MSE_train_vec_custom_0s = [];
MSE_test_vec_custom_0s = [];
%% Starting the loop
positionCount = 1;
k_array = 1:step_size:d;
for k = k_array
%% Unmodified autoencoder with regular inputs
%autoencoding
autoenc = trainAutoencoder(X_train, k, 'MaxEpochs',epochs);
X_train_small = encode(autoenc, X_train);
X_test_small = encode(autoenc, X_test);
k = k
%lda
lda = fitcdiscr(X_train_small', Y_train);
Y_hat_train = predict(lda, X_train_small');
Y_hat_test = predict(lda, X_test_small');
CCR_train = sum(Y_hat_train == Y_train)/n_train;
CCR_test = sum(Y_hat_test == Y_test)/n_test;
CCR_train_lda_vec = [CCR_train_lda_vec, CCR_train];
CCR_test_lda_vec = [CCR_test_lda_vec, CCR_test];
%svm linear
svm_lin = fitclinear(X_train_small',Y_train);
Y_hat_train = predict(svm_lin, X_train_small');
Y_hat_test = predict(svm_lin, X_test_small');
CCR_train = sum(Y_hat_train == Y_train)/n_train;
CCR_test = sum(Y_hat_test == Y_test)/n_test;
CCR_train_svmLin_vec = [CCR_train_svmLin_vec, CCR_train];
CCR_test_svmLin_vec = [CCR_test_svmLin_vec, CCR_test];
% svm kernel
svm_kernel = fitckernel(X_train_small',Y_train);
Y_hat_train = predict(svm_kernel, X_train_small');
Y_hat_test = predict(svm_kernel, X_test_small');
CCR_train = sum(Y_hat_train == Y_train)/n_train;
CCR_test = sum(Y_hat_test == Y_test)/n_test;
CCR_train_svmKern_vec = [CCR_train_svmKern_vec, CCR_train];
CCR_test_svmKern_vec = [CCR_test_svmKern_vec, CCR_test];
% MSE
X_train_Reconstructed = predict(autoenc,X_train);
X_test_Reconstructed = predict(autoenc,X_test);
mseError_train = mse(X_train - X_train_Reconstructed);
MSE_train_vec = [MSE_train_vec, mseError_train];
mseError_test = mse(X_test - X_test_Reconstructed);
MSE_test_vec = [MSE_test_vec, mseError_test];
%% Modified autoencoder with custom inputs
%autoencoding
autoenc_custom = trainAutoencoder(X_train_custom, k, 'MaxEpochs',epochs);
X_train_small_custom = encode(autoenc_custom, X_train_custom);
X_test_small_custom = encode(autoenc_custom, X_test_custom);
%lda
lda_custom = fitcdiscr(X_train_small_custom', Y_train_ext);
Y_hat_train = predict(lda_custom, X_train_small_custom');
Y_hat_test = predict(lda_custom, X_test_small_custom');
CCR_train = sum(Y_hat_train == Y_train_ext)/n_train_custom;
CCR_test = sum(Y_hat_test == Y_test)/n_test_custom;
CCR_train_lda_vec_custom = [CCR_train_lda_vec_custom, CCR_train];
CCR_test_lda_vec_custom = [CCR_test_lda_vec_custom, CCR_test];
%svm linear
svm_lin_custom = fitclinear(X_train_small_custom',Y_train_ext);
Y_hat_train_custom = predict(svm_lin_custom, X_train_small_custom');
Y_hat_test_custom = predict(svm_lin_custom, X_test_small_custom');
CCR_train = sum(Y_hat_train_custom == Y_train_ext)/n_train;
CCR_test = sum(Y_hat_test_custom == Y_test)/n_test;
CCR_train_svmLin_vec_custom = [CCR_train_svmLin_vec_custom, CCR_train];
CCR_test_svmLin_vec_custom = [CCR_test_svmLin_vec_custom, CCR_test];
%svm kernel
svm_kernel_custom = fitckernel(X_train_small_custom',Y_train);
Y_hat_train_custom = predict(svm_kernel_custom, X_train_small_custom');
Y_hat_test_custom = predict(svm_kernel_custom, X_test_small_custom');
CCR_train = sum(Y_hat_train_custom == Y_train_ext)/n_train;
CCR_test = sum(Y_hat_test_custom == Y_test)/n_test;
CCR_train_svmKern_vec_custom = [CCR_train_svmKern_vec_custom, CCR_train];
CCR_test_svmKern_vec_custom = [CCR_test_svmKern_vec_custom, CCR_test];
%MSE
X_train_Reconstructed_custom = predict(autoenc_custom,X_train_custom);
X_test_Reconstructed_custom = predict(autoenc_custom,X_test_custom);
mseError_train_custom = mse(X_train_custom(1:(end - l), :) - X_train_Reconstructed_custom(1:(end - l), :));
MSE_train_vec_custom = [MSE_train_vec_custom, mseError_train_custom];
mseError_test_custom = mse(X_test_custom(1:(end - l), :) - X_test_Reconstructed_custom(1:(end - l), :));
MSE_test_vec_custom = [MSE_test_vec_custom, mseError_test_custom];
%% Images
if(k == 1 || k == 11 || k == 31 || k == 51 || k == 101 || k == 151 || k == 201 || k == 251)
figure(1)
subplot(4,4,positionCount)
positionCount = positionCount + 1;
img = X_test_Reconstructed(1:end, display_num);
imshow(reshape(img, img_size))
titlevar = {"Original Reconstruction", " k = " + k};
title(titlevar);
subplot(4,4,positionCount)
positionCount = positionCount + 1;
img = X_test_Reconstructed_custom(1:(end - l), display_num);
imshow(reshape(img, img_size))
titlevar = {"Modified Reconstruction", " k = " + k};
title(titlevar);
end
end
%% Original Image
figure
img = X_test(1:end, display_num);
imshow(reshape(img, img_size))
title("Original");
%% Plots
figure()
hold on
plot(k_array(1:end), CCR_train_lda_vec(1:end))
plot(k_array(1:end), CCR_train_lda_vec_custom(1:end))
title_var = {"CCR LDA Train", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR")
xlabel("k")
legend("Original", "Modified")
%legend("Train", "Test")
ylim([0.3 1.05])
hold off
figure()
hold on
plot(k_array(1:end), CCR_test_lda_vec(1:end))
plot(k_array(1:end), CCR_test_lda_vec_custom(1:end))
title_var = {"CCR LDA Test:", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR")
xlabel("k")
legend("Original", "Modified")
ylim([0.3 1.05])
hold off
figure()
hold on
plot(k_array(1:end), CCR_train_svmLin_vec(1:end))
plot(k_array(1:end), CCR_train_svmLin_vec_custom(1:end))
title_var = {"CCR SVM Linear Train", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR")
xlabel("k")
legend("Original", "Modified")
ylim([0.3 1.05])
hold off
figure()
hold on
plot(k_array(1:end), CCR_test_svmLin_vec(1:end))
plot(k_array(1:end), CCR_test_svmLin_vec_custom(1:end))
title_var = {"CCR SVM Linear Test", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR")
xlabel("k")
legend("Original", "Modified")
ylim([0.3 1.05])
hold off
figure()
hold on
plot(k_array(1:end), CCR_train_svmKern_vec(1:end))
plot(k_array(1:end), CCR_train_svmKern_vec_custom(1:end))
title_var = {"CCR SVM Kernal Train", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR")
xlabel("k")
legend("Original", "Modified")
legend("Train", "Test")
ylim([0.3 1.05])
hold off
figure()
hold on
plot(k_array(1:end), CCR_test_svmKern_vec(1:end))
plot(k_array(1:end), CCR_test_svmKern_vec_custom(1:end))
title_var = {"CCR SVM Kernal Test", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR")
xlabel("k")
legend("Original", "Modified")
ylim([0.3 1.05])
hold off
figure()
hold on
plot(k_array(1:end), MSE_train_vec(1:end))
plot(k_array(1:end), MSE_train_vec_custom(1:end))
title_var = {"Train MSE", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("MSE")
xlabel("k")
legend("Original", "Modified")
hold off
figure()
hold on
plot(k_array(1:end), MSE_test_vec(1:end))
plot(k_array(1:end), MSE_test_vec_custom(1:end))
title_var = {"Test MSE", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("MSE")
xlabel("k")
legend("Original", "Modified")
hold off
figure()
hold on
plot(k_array(1:end), CCR_train_svmKern_vec_custom(1:end) - CCR_train_svmKern_vec(1:end))
%title_var = {"CCR SVM Kernal Train Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
title_var = {"CCR SVM Kernal Train Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR Difference")
xlabel("k")
ylim([-0.2 0.2])
hold off
figure()
hold on
plot(k_array(1:end), CCR_test_svmKern_vec_custom(1:end) - CCR_test_svmKern_vec(1:end))
%title_var = {"CCR SVM Kernal Test Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
title_var = {"CCR SVM Kernal Test Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("CCR Difference")
xlabel("k")
ylim([-0.2 0.2])
hold off
figure()
hold on
plot(k_array(1:end), MSE_train_vec_custom(1:end) - MSE_train_vec(1:end))
%title_var = {"Train MSE Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
title_var = {"Train MSE Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("MSE")
xlabel("k")
hold off
figure()
hold on
plot(k_array(1:end), MSE_test_vec_custom(1:end) - MSE_test_vec(1:end))
%title_var = {"Test MSE difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
title_var = {"Test MSE difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
title(title_var)
ylabel("MSE")
xlabel("k")
hold off
%% Extra plots for LDA and SVM Linear. Commentated out for convienence
% figure()
% hold on
% plot(k_array(1:end), CCR_train_lda_vec_custom(1:end) - CCR_train_lda_vec(1:end))
% %title_var = {"CCR LDA Train Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title_var = {"CCR LDA Train Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title(title_var)
% ylabel("CCR difference")
% xlabel("k")
% %legend("Train", "Test")
% ylim([-0.2 0.2])
% hold off
%
% figure()
% hold on
% plot(k_array(1:end), CCR_test_lda_vec_custom(1:end) - CCR_test_lda_vec(1:end))
% %title_var = {"CCR LDA Test Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title_var = {"CCR LDA Train Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title(title_var)
% ylabel("CCR difference")
% xlabel("k")
% ylim([-0.5 0.5])
% hold off
%
% figure()
% hold on
% plot(k_array(1:end), CCR_train_svmLin_vec_custom(1:end) - CCR_train_svmLin_vec(1:end))
% %title_var = {"CCR SVM Linear Train Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title_var = {"CCR LDA Train Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title(title_var)
% ylabel("CCR Difference")
% xlabel("k")
% ylim([-0.2 0.2])
% hold off
%
% figure()
% hold on
% plot(k_array(1:end), CCR_test_svmLin_vec_custom(1:end) - CCR_test_svmLin_vec(1:end))
% %title_var = {"CCR SVM Linear Test Difference: " + noi_0 + ", " + noi_1, "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title_var = {"CCR LDA Train Difference: 0-4 vs 5-9", "l = " + l + ", a = " + a + ", epochs = " + epochs};
% title(title_var)
% ylabel("CCR")
% ylabel("CCR Difference")
% ylim([-0.2 0.2])
% hold off