-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMonksRun_HoldOut.m
73 lines (58 loc) · 2.14 KB
/
MonksRun_HoldOut.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
69
70
71
72
73
% monks attribute (discrete attributes)
% a1 in [1,3] -> 3 bit
% a2 in [1,3] -> 3 bit
% a3 in [1,2] -> 2 bit
% a4 in [1,3] -> 3 bit
% a5 in [1,4] -> 4 bit
% a6 in [1,2] -> 2 bit
% Tot = 17 bit
% class in [0,1] -> 2 bit
addpath(genpath(pwd))
oneOfkConversion = [3 3 2 3 4 2];
% prepare training data
data = csvread('Data/Classification/monks-3.csv'); %Change number to 1-2-3 to increase difficulty
x = data(:,[2 3 4 5 6 7]);
y = data(:,1);
X = zeros(size(x,1),sum(oneOfkConversion));
for i=1:size(x,1)
X(i,:) = oneOfk(x(i,:), oneOfkConversion);
end
input_dim = 17;
output_dim = 1;
iterations = 100;
bias = 1;
threshold_grad = 1e-8;
shuffle = 1;
validation = 1;
use = 0; % 1 = regression, 0 = classification
assessment = 0;
if ~validation
mb_size = 32; %used only with no validation. Otherwise we initialize it in kfold-holdout
tr_perc = 0.6;
test_perc = 0.2;
[X_train,y_train,X_val,y_val,X_test,y_test] = train_validation_test_split(X,y,tr_perc,test_perc,shuffle);
% define hyperparameters
hidden_dim = 10;
eta = 0.9; % learning rate
lambda = 1e-3; % Tykhonov
alpha = 0.8; % momentum
if mb_size > size(X_train,1)
error('mb_size larger than number of patterns: %d', size(X,1));
end
% train neural network
nn = NeuralNetwork(use,input_dim,output_dim,hidden_dim,iterations,eta,lambda,alpha,bias,threshold_grad,mb_size);
fprintf('Starting fitting...');
[train_acc,test_acc,train_err,test_err,iter] = nn.fit(X_train,y_train,X_val,y_val);
printf;
% test with trained neural network
[accuracy, outputs, errors_test] = nn.test(X_test,y_test);
fprintf('Test Accuracy: %f\n', accuracy);
else
tr_perc = 0.6;
test_perc = 0.2;
[nn,train_acc, test_acc, train_err, test_err, iter] = holdOut(use,X,y,input_dim,output_dim,iterations,bias,threshold_grad,tr_perc,test_perc,shuffle);
fprintf('Test accuracy: %f\n', test_acc(end));
fprintf('Test error: %f\n', test_err(end));
fprintf("Eta: %f\nLambda: %f\nAlpha: %f\n Hidden dimensions: %f %f\n",nn.eta,nn.lambda,nn.alpha, nn.hidden_dim);
end
plot_curve(iter, train_err, test_err, train_acc, test_acc);