-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhyperparameter_tuning.m
127 lines (108 loc) · 5.62 KB
/
hyperparameter_tuning.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
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
% This function performs hyper-parameter tuning utilizing Bayesian ZSL and
% extract the parameter tuple with the highest Harmonic mean. The model
% version, constrained or unconstrained, must be specified.
% Inputs:
% Data -- Training and test + attributes
% Model versions -- 'constrained', 'unconstrained'| Ex: ['Model', 'constrained']
%
% Optional: (if the model version is specified as unconstrained)
% # components for PCA -- dim | Ex: ['pca', 300]
function [k0, k1, mm, a0, b0, mu_0, s, K] = hyperparameter_tuning(xtrain,ytrain,xtest_unseen,ytest_unseen,xtest_seen,ytest_seen, att, varargin)
% Default # features for PCA id Unconstrained model selected
dim = 500;
if nargin<2
fprintf('The model version is missing!! Please insert version name: constrained or unconstrained \n');
elseif nargin==4
dim = varargin{4};
end
% Tuning range for the parameters
k0_range = [0.1 1]; %[0.01 0.1 1 10];
k1_range = k0_range;
a0_range = [1 10 100];
b0_range = a0_range;
s_range = 1:2:10;
K_range = [1, 2, 3, 4];
% Initialization
s = 0; a0 = 20; b0 = 20; mm = dim + 2;
bestH = 0; best_acc_s = 0; best_acc_us = 0;
if strcmp(varargin{2}, 'unconstrained')
% PCA for dimentionality reduction
fprintf('Applying PCA to reduce the dimension...\n')
C = cov(xtrain);
[vv, ~] = eig(C);
xtrain = xtrain*vv(:,end-dim+1:end);
xtest_seen = xtest_seen*vv(:,end-dim+1:end);
xtest_unseen = xtest_unseen*vv(:,end-dim+1:end);
% Precalculation of class means and scatter matrices for unconstrained model
[mu_0, scatter] = calculate_priors(xtrain, ytrain, 'Model', 'unconstrained');
% m range is determined by the dim of data
m_range = [10*dim 50*dim 500*dim];
fprintf('Tuning is getting started...\n')
for kk=K_range
for k_0=k0_range
for k_1=k1_range
for m=m_range
for ss=s_range
%Psi=(m-dim-1)*scatter/s;
tic
[acc_s,acc_us,H] = Bayesian_ZSL(xtrain,ytrain,xtest_unseen,ytest_unseen,xtest_seen,ytest_seen, att,'Model', 'unconstrained','tuning',true,'num_neighbor', kk,...
'kappa_0', k_0, 'kappa_1', k_1, 'cov_shape', m, 'prior_mean', mu_0,'prior_covscale', ss,'scatter', scatter, 'pca', 0);
% Print out when there is an improvement in
% Harmonic mean
if H>bestH %|| acc_s>best_acc_s || acc_us>best_acc_us
bestH = H;% best_acc_s = acc_s; best_acc_us = acc_us;
k0 = k_0; k1 = k_1; mm = m; s = ss; K = kk;
disp(['GZSL unseen: averaged per-class accuracy=' num2str(acc_us) ]);
disp(['GZSL seen: averaged per-class accuracy=' num2str(acc_s) ]);
disp(['GZSL: H=' num2str(H)]);
disp(['K=' num2str(kk)]);
disp(['k0=' num2str(k_0)]);
disp(['k1=' num2str(k_1)]);
disp(['m=' num2str(m)]);
disp(['s=' num2str(s)]);
end
toc
end
end
end
end
end
else
d = size(xtrain, 2);
%m_range = [10*d 50*d 500*d];
% Precalculation of class means and scatter matrices for unconstrained model
fprintf('Calculating priors for the Constrained model ...');
tic
[mu_0, ~] = calculate_priors(xtrain, ytrain, 'Model', 'constrained');
toc
fprintf('Tuning is getting started...\n')
for kk=K_range
for k_0=k0_range
for k_1=k1_range
for a_0=a0_range
for b_0=b0_range
tic
[acc_s,acc_us,H] = Bayesian_ZSL(xtrain,ytrain,xtest_unseen,ytest_unseen,xtest_seen,ytest_seen, att,...
'Model', 'constrained','tuning', true,'num_neighbor', kk, 'kappa_0', k_0, 'kappa_1', k_1,...
'prior_mean', mu_0,'invg_shape', a_0, 'invg_scale', b_0);
% Print out when there is an improvement in
% Harmonic mean
if H>bestH %|| acc_us>best_acc_us % || acc_s>best_acc_s
bestH = H; % best_acc_us = acc_us; % best_acc_s = acc_s;
k0 = k_0; k1 = k_1; a0 = a_0; b0 = b_0; K = kk;
disp(['GZSL unseen: averaged per-class accuracy=' num2str(acc_us) ]);
disp(['GZSL seen: averaged per-class accuracy=' num2str(acc_s) ]);
disp(['GZSL: H=' num2str(H)]);
disp(['K=' num2str(kk)]);
disp(['k0=' num2str(k_0)]);
disp(['k1=' num2str(k_1)]);
disp(['a0=' num2str(a_0)]);
disp(['b0=' num2str(b_0)]);
end
toc
end
end
end
end
end
end