-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_val.py
222 lines (181 loc) · 7.14 KB
/
train_val.py
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
try:
from time import time
from Methods.method_gromov_wasserstein import FgwEstimator
from Methods.method_graph_kernel import GraphKernelEstimator
from Methods.method_fingerprint import IOKREstimator
from Utils.metabolites_utils import center_gram_matrix, normalize_gram_matrix
from Utils.load_data import load_dataset_kernel_graph, load_dataset_kernel_finger
from Utils.diffusion import diffuse
except ModuleNotFoundError:
import sys
sys.path.insert(0, '/tsi/clusterhome/lmotte/Implementation/metabolite-identification-with-fused-gromov-wasserstein')
from time import time
from Methods.method_gromov_wasserstein import FgwEstimator
from Methods.method_graph_kernel import GraphKernelEstimator
from Methods.method_fingerprint import IOKREstimator
from Utils.metabolites_utils import center_gram_matrix, normalize_gram_matrix
from Utils.load_data import load_dataset_kernel_graph, load_dataset_kernel_finger
from Utils.diffusion import diffuse
def exp_gw_onehot(n_tr, n_val, L, unused, n_bary, n_c_max):
# Load data
t0 = time()
D_tr, D_te = load_dataset_kernel_graph(n_tr - n_val)
K, Y = D_tr
K_tr_te, K_te_te, Y_te = D_te
n = K_tr_te.shape[0]
K_tr_te, K_te_te = K_tr_te[:, :n_val], K_te_te[:n_val, :n_val]
Y_te = [Y_te[0][: n_val], Y_te[1][: n_val], Y_te[2][: n_val], Y_te[3][: n_val]]
print(f'Load time: {time() - t0}', flush=True)
# Input pre-processing
t0 = time()
center, normalize = True, True
if center:
K_tr_te = center_gram_matrix(K_tr_te, K, K_tr_te, K)
K = center_gram_matrix(K)
if normalize:
K_tr_te = normalize_gram_matrix(K_tr_te, K, K_te_te)
K = normalize_gram_matrix(K)
print(f'Pre-processing time: {time() - t0}', flush=True)
# Train
t0 = time()
clf = FgwEstimator()
clf.ground_metric = 'onehot'
clf.train(K, Y, L)
print(f'Train time: {time() - t0}', flush=True)
# Predict
t0 = time()
fgw, topk, n_pred = clf.predict(K_tr_te, n_bary=n_bary, Y_te=Y_te, n_c_max=n_c_max)
print(f'Test time: {time() - t0}', flush=True)
print(f'{(n_tr, n_val, L, None, n_bary, n_c_max)}, mean fgw : {fgw}, topk = {topk}', flush=True)
return fgw[0], topk, n, n_pred
def exp_gw_fine(n_tr, n_val, L, w, n_bary, n_c_max):
# Load data
t0 = time()
D_tr, D_te = load_dataset_kernel_graph(n_tr - n_val)
K, Y = D_tr
K_tr_te, K_te_te, Y_te = D_te
n = K_tr_te.shape[0]
K_tr_te, K_te_te = K_tr_te[:, :n_val], K_te_te[:n_val, :n_val]
Y_te = [Y_te[0][: n_val], Y_te[1][: n_val], Y_te[2][: n_val], Y_te[3][: n_val]]
print(f'Load time: {time() - t0}', flush=True)
# Input pre-processing
t0 = time()
center, normalize = True, True
if center:
K_tr_te = center_gram_matrix(K_tr_te, K, K_tr_te, K)
K = center_gram_matrix(K)
if normalize:
K_tr_te = normalize_gram_matrix(K_tr_te, K, K_te_te)
K = normalize_gram_matrix(K)
print(f'Pre-processing time: {time() - t0}', flush=True)
# Train
t0 = time()
clf = FgwEstimator()
clf.ground_metric = 'fine'
clf.w = w
clf.train(K, Y, L)
print(f'Train time: {time() - t0}', flush=True)
# Predict
t0 = time()
fgw, topk, n_pred = clf.predict(K_tr_te, n_bary=n_bary, Y_te=Y_te, n_c_max=n_c_max)
print(f'Test time: {time() - t0}', flush=True)
print(f'{(n_tr, n_val, L, w, n_bary, n_c_max)}, mean fgw : {fgw}, topk = {topk}', flush=True)
return fgw[0], topk, n, n_pred
def exp_gw_diffuse(n_tr, n_val, L, tau, n_bary, n_c_max):
# Load data
t0 = time()
D_tr, D_te = load_dataset_kernel_graph(n_tr - n_val)
K, Y = D_tr
K_tr_te, K_te_te, Y_te = D_te
n = K_tr_te.shape[0]
K_tr_te, K_te_te = K_tr_te[:, :n_val], K_te_te[:n_val, :n_val]
Y_te = [Y_te[0][: n_val], Y_te[1][: n_val], Y_te[2][: n_val], Y_te[3][: n_val]]
print(f'Load time: {time() - t0}', flush=True)
# Input pre-processing
t0 = time()
center, normalize = True, True
if center:
K_tr_te = center_gram_matrix(K_tr_te, K, K_tr_te, K)
K = center_gram_matrix(K)
if normalize:
K_tr_te = normalize_gram_matrix(K_tr_te, K, K_te_te)
K = normalize_gram_matrix(K)
print(f'Pre-processing time: {time() - t0}', flush=True)
# Train
t0 = time()
clf = FgwEstimator()
clf.ground_metric = 'diffuse'
clf.tau = tau
Y = diffuse(Y, clf.tau)
clf.train(K, Y, L)
print(f'Train time: {time() - t0}', flush=True)
# Predict
t0 = time()
fgw, topk, n_pred = clf.predict(K_tr_te, n_bary=n_bary, Y_te=Y_te, n_c_max=n_c_max)
print(f'Test time: {time() - t0}', flush=True)
print(f'{(n_tr, n_val, L, tau, n_bary, n_c_max)}, mean fgw : {fgw}, topk = {topk}', flush=True)
return fgw[0], topk, n, n_pred
def exp_gk(n_tr, n_val, L, h, n_bary, n_c_max):
# Load data
t0 = time()
D_tr, D_te = load_dataset_kernel_graph(n_tr - n_val)
K, Y = D_tr
K_tr_te, K_te_te, Y_te = D_te
K_tr_te, K_te_te = K_tr_te[:, :n_val], K_te_te[:n_val, :n_val]
Y_te = [Y_te[0][: n_val], Y_te[1][: n_val], Y_te[2][: n_val], Y_te[3][: n_val]]
n = K_tr_te.shape[0]
print(f'Load time: {time() - t0}', flush=True)
# Input pre-processing
t0 = time()
center, normalize = True, True
if center:
K_tr_te = center_gram_matrix(K_tr_te, K, K_tr_te, K)
K = center_gram_matrix(K)
if normalize:
K_tr_te = normalize_gram_matrix(K_tr_te, K, K_te_te)
K = normalize_gram_matrix(K)
print(f'Pre-processing time: {time() - t0}', flush=True)
# Train
t0 = time()
clf = GraphKernelEstimator()
clf.train(K, Y, L)
print(f'Train time: {time() - t0}', flush=True)
# Predict
t0 = time()
clf.h = h
fgw, topk, n_pred = clf.predict(K_tr_te, n_bary=n_bary, Y_te=Y_te, n_c_max=n_c_max)
print(f'Test time: {time() - t0}', flush=True)
print(f'{(n_tr, n_val, L, h, n_bary, n_c_max)}, mean fgw : {fgw}, topk = {topk}', flush=True)
return fgw[0], topk, n, n_pred
def exp_finger(n_tr, n_val, L, g, unused, n_c_max):
# Load data
t0 = time()
D_tr, D_te = load_dataset_kernel_finger(n_tr-n_val)
K, Y = D_tr
K_tr_te, K_te_te, Y_te = D_te
K_tr_te, K_te_te = K_tr_te[:, :n_val], K_te_te[:n_val, :n_val]
Y_te = [Y_te[0][: n_val], Y_te[1][: n_val], Y_te[2][: n_val], Y_te[3][: n_val]]
n = K_tr_te.shape[0]
print(f'Load time: {time() - t0}', flush=True)
# Input pre-processing
t0 = time()
center, normalize = True, True
if center:
K_tr_te = center_gram_matrix(K_tr_te, K, K_tr_te, K)
K = center_gram_matrix(K)
if normalize:
K_tr_te = normalize_gram_matrix(K_tr_te, K, K_te_te)
K = normalize_gram_matrix(K)
print(f'Pre-processing time: {time() - t0}', flush=True)
# Train
t0 = time()
clf = IOKREstimator()
clf.train(K, Y, L, g)
print(f'Train time: {time() - t0}', flush=True)
# Predict
t0 = time()
n_bary = n_tr
fgw, topk, n_pred = clf.predict(K_tr_te, Y_te=Y_te, n_c_max=n_c_max)
print(f'Test time: {time() - t0}', flush=True)
print(f'{(n_tr, n_val, L, g, n_bary, n_c_max)}, mean fgw : {fgw}, topk = {topk}', flush=True)
return fgw[0], topk, n, n_pred