-
Notifications
You must be signed in to change notification settings - Fork 20
/
train_and_eval.py
executable file
·742 lines (656 loc) · 25 KB
/
train_and_eval.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
import numpy as np
import copy
import torch
import dgl
from utils import set_seed
"""
1. Train and eval
"""
def train(model, data, feats, labels, criterion, optimizer, idx_train, lamb=1):
"""
GNN full-batch training. Input the entire graph `g` as data.
lamb: weight parameter lambda
"""
model.train()
# Compute loss and prediction
logits = model(data, feats)
out = logits.log_softmax(dim=1)
loss = criterion(out[idx_train], labels[idx_train])
loss_val = loss.item()
loss *= lamb
optimizer.zero_grad()
loss.backward()
optimizer.step()
return loss_val
def train_sage(model, dataloader, feats, labels, criterion, optimizer, lamb=1):
"""
Train for GraphSAGE. Process the graph in mini-batches using `dataloader` instead the entire graph `g`.
lamb: weight parameter lambda
"""
device = feats.device
model.train()
total_loss = 0
for step, (input_nodes, output_nodes, blocks) in enumerate(dataloader):
blocks = [blk.int().to(device) for blk in blocks]
batch_feats = feats[input_nodes]
batch_labels = labels[output_nodes]
# Compute loss and prediction
logits = model(blocks, batch_feats)
out = logits.log_softmax(dim=1)
loss = criterion(out, batch_labels)
total_loss += loss.item()
loss *= lamb
optimizer.zero_grad()
loss.backward()
optimizer.step()
return total_loss / len(dataloader)
def train_mini_batch(model, feats, labels, batch_size, criterion, optimizer, lamb=1):
"""
Train MLP for large datasets. Process the data in mini-batches. The graph is ignored, node features only.
lamb: weight parameter lambda
"""
model.train()
num_batches = max(1, feats.shape[0] // batch_size)
idx_batch = torch.randperm(feats.shape[0])[: num_batches * batch_size]
if num_batches == 1:
idx_batch = idx_batch.view(1, -1)
else:
idx_batch = idx_batch.view(num_batches, batch_size)
total_loss = 0
for i in range(num_batches):
# No graph needed for the forward function
logits = model(None, feats[idx_batch[i]])
out = logits.log_softmax(dim=1)
loss = criterion(out, labels[idx_batch[i]])
total_loss += loss.item()
loss *= lamb
optimizer.zero_grad()
loss.backward()
optimizer.step()
return total_loss / num_batches
def evaluate(model, data, feats, labels, criterion, evaluator, idx_eval=None):
"""
Returns:
out: log probability of all input data
loss & score (float): evaluated loss & score, if idx_eval is not None, only loss & score on those idx.
"""
model.eval()
with torch.no_grad():
logits = model.inference(data, feats)
out = logits.log_softmax(dim=1)
if idx_eval is None:
loss = criterion(out, labels)
score = evaluator(out, labels)
else:
loss = criterion(out[idx_eval], labels[idx_eval])
score = evaluator(out[idx_eval], labels[idx_eval])
return out, loss.item(), score
def evaluate_mini_batch(
model, feats, labels, criterion, batch_size, evaluator, idx_eval=None
):
"""
Evaluate MLP for large datasets. Process the data in mini-batches. The graph is ignored, node features only.
Return:
out: log probability of all input data
loss & score (float): evaluated loss & score, if idx_eval is not None, only loss & score on those idx.
"""
model.eval()
with torch.no_grad():
num_batches = int(np.ceil(len(feats) / batch_size))
out_list = []
for i in range(num_batches):
logits = model.inference(None, feats[batch_size * i : batch_size * (i + 1)])
out = logits.log_softmax(dim=1)
out_list += [out.detach()]
out_all = torch.cat(out_list)
if idx_eval is None:
loss = criterion(out_all, labels)
score = evaluator(out_all, labels)
else:
loss = criterion(out_all[idx_eval], labels[idx_eval])
score = evaluator(out_all[idx_eval], labels[idx_eval])
return out_all, loss.item(), score
"""
2. Run teacher
"""
def run_transductive(
conf,
model,
g,
feats,
labels,
indices,
criterion,
evaluator,
optimizer,
logger,
loss_and_score,
):
"""
Train and eval under the transductive setting.
The train/valid/test split is specified by `indices`.
The input graph is assumed to be large. Thus, SAGE is used for GNNs, mini-batch is used for MLPs.
loss_and_score: Stores losses and scores.
"""
set_seed(conf["seed"])
device = conf["device"]
batch_size = conf["batch_size"]
idx_train, idx_val, idx_test = indices
feats = feats.to(device)
labels = labels.to(device)
if "SAGE" in model.model_name:
# Create dataloader for SAGE
# Create csr/coo/csc formats before launching sampling processes
# This avoids creating certain formats in each data loader process, which saves momory and CPU.
g.create_formats_()
sampler = dgl.dataloading.MultiLayerNeighborSampler(
[eval(fanout) for fanout in conf["fan_out"].split(",")]
)
dataloader = dgl.dataloading.NodeDataLoader(
g,
idx_train,
sampler,
batch_size=batch_size,
shuffle=True,
drop_last=False,
num_workers=conf["num_workers"],
)
# SAGE inference is implemented as layer by layer, so the full-neighbor sampler only collects one-hop neighors
sampler_eval = dgl.dataloading.MultiLayerFullNeighborSampler(1)
dataloader_eval = dgl.dataloading.NodeDataLoader(
g,
torch.arange(g.num_nodes()),
sampler_eval,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=conf["num_workers"],
)
data = dataloader
data_eval = dataloader_eval
elif "MLP" in model.model_name:
feats_train, labels_train = feats[idx_train], labels[idx_train]
feats_val, labels_val = feats[idx_val], labels[idx_val]
feats_test, labels_test = feats[idx_test], labels[idx_test]
else:
g = g.to(device)
data = g
data_eval = g
best_epoch, best_score_val, count = 0, 0, 0
for epoch in range(1, conf["max_epoch"] + 1):
if "SAGE" in model.model_name:
loss = train_sage(model, data, feats, labels, criterion, optimizer)
elif "MLP" in model.model_name:
loss = train_mini_batch(
model, feats_train, labels_train, batch_size, criterion, optimizer
)
else:
loss = train(model, data, feats, labels, criterion, optimizer, idx_train)
if epoch % conf["eval_interval"] == 0:
if "MLP" in model.model_name:
_, loss_train, score_train = evaluate_mini_batch(
model, feats_train, labels_train, criterion, batch_size, evaluator
)
_, loss_val, score_val = evaluate_mini_batch(
model, feats_val, labels_val, criterion, batch_size, evaluator
)
_, loss_test, score_test = evaluate_mini_batch(
model, feats_test, labels_test, criterion, batch_size, evaluator
)
else:
out, loss_train, score_train = evaluate(
model, data_eval, feats, labels, criterion, evaluator, idx_train
)
# Use criterion & evaluator instead of evaluate to avoid redundant forward pass
loss_val = criterion(out[idx_val], labels[idx_val]).item()
score_val = evaluator(out[idx_val], labels[idx_val])
loss_test = criterion(out[idx_test], labels[idx_test]).item()
score_test = evaluator(out[idx_test], labels[idx_test])
logger.debug(
f"Ep {epoch:3d} | loss: {loss:.4f} | s_train: {score_train:.4f} | s_val: {score_val:.4f} | s_test: {score_test:.4f}"
)
loss_and_score += [
[
epoch,
loss_train,
loss_val,
loss_test,
score_train,
score_val,
score_test,
]
]
if score_val >= best_score_val:
best_epoch = epoch
best_score_val = score_val
state = copy.deepcopy(model.state_dict())
count = 0
else:
count += 1
if count == conf["patience"] or epoch == conf["max_epoch"]:
break
model.load_state_dict(state)
if "MLP" in model.model_name:
out, _, score_val = evaluate_mini_batch(
model, feats, labels, criterion, batch_size, evaluator, idx_val
)
else:
out, _, score_val = evaluate(
model, data_eval, feats, labels, criterion, evaluator, idx_val
)
score_test = evaluator(out[idx_test], labels[idx_test])
logger.info(
f"Best valid model at epoch: {best_epoch: 3d}, score_val: {score_val :.4f}, score_test: {score_test :.4f}"
)
return out, score_val, score_test
def run_inductive(
conf,
model,
g,
feats,
labels,
indices,
criterion,
evaluator,
optimizer,
logger,
loss_and_score,
):
"""
Train and eval under the inductive setting.
The train/valid/test split is specified by `indices`.
idx starting with `obs_idx_` contains the node idx in the observed graph `obs_g`.
idx starting with `idx_` contains the node idx in the original graph `g`.
The model is trained on the observed graph `obs_g`, and evaluated on both the observed test nodes (`obs_idx_test`) and inductive test nodes (`idx_test_ind`).
The input graph is assumed to be large. Thus, SAGE is used for GNNs, mini-batch is used for MLPs.
idx_obs: Idx of nodes in the original graph `g`, which form the observed graph 'obs_g'.
loss_and_score: Stores losses and scores.
"""
set_seed(conf["seed"])
device = conf["device"]
batch_size = conf["batch_size"]
obs_idx_train, obs_idx_val, obs_idx_test, idx_obs, idx_test_ind = indices
feats = feats.to(device)
labels = labels.to(device)
obs_feats = feats[idx_obs]
obs_labels = labels[idx_obs]
obs_g = g.subgraph(idx_obs)
if "SAGE" in model.model_name:
# Create dataloader for SAGE
# Create csr/coo/csc formats before launching sampling processes
# This avoids creating certain formats in each data loader process, which saves momory and CPU.
obs_g.create_formats_()
g.create_formats_()
sampler = dgl.dataloading.MultiLayerNeighborSampler(
[eval(fanout) for fanout in conf["fan_out"].split(",")]
)
obs_dataloader = dgl.dataloading.NodeDataLoader(
obs_g,
obs_idx_train,
sampler,
batch_size=batch_size,
shuffle=True,
drop_last=False,
num_workers=conf["num_workers"],
)
sampler_eval = dgl.dataloading.MultiLayerFullNeighborSampler(1)
obs_dataloader_eval = dgl.dataloading.NodeDataLoader(
obs_g,
torch.arange(obs_g.num_nodes()),
sampler_eval,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=conf["num_workers"],
)
dataloader_eval = dgl.dataloading.NodeDataLoader(
g,
torch.arange(g.num_nodes()),
sampler_eval,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=conf["num_workers"],
)
obs_data = obs_dataloader
obs_data_eval = obs_dataloader_eval
data_eval = dataloader_eval
elif "MLP" in model.model_name:
feats_train, labels_train = obs_feats[obs_idx_train], obs_labels[obs_idx_train]
feats_val, labels_val = obs_feats[obs_idx_val], obs_labels[obs_idx_val]
feats_test_tran, labels_test_tran = (
obs_feats[obs_idx_test],
obs_labels[obs_idx_test],
)
feats_test_ind, labels_test_ind = feats[idx_test_ind], labels[idx_test_ind]
else:
obs_g = obs_g.to(device)
g = g.to(device)
obs_data = obs_g
obs_data_eval = obs_g
data_eval = g
best_epoch, best_score_val, count = 0, 0, 0
for epoch in range(1, conf["max_epoch"] + 1):
if "SAGE" in model.model_name:
loss = train_sage(
model, obs_data, obs_feats, obs_labels, criterion, optimizer
)
elif "MLP" in model.model_name:
loss = train_mini_batch(
model, feats_train, labels_train, batch_size, criterion, optimizer
)
else:
loss = train(
model,
obs_data,
obs_feats,
obs_labels,
criterion,
optimizer,
obs_idx_train,
)
if epoch % conf["eval_interval"] == 0:
if "MLP" in model.model_name:
_, loss_train, score_train = evaluate_mini_batch(
model, feats_train, labels_train, criterion, batch_size, evaluator
)
_, loss_val, score_val = evaluate_mini_batch(
model, feats_val, labels_val, criterion, batch_size, evaluator
)
_, loss_test_tran, score_test_tran = evaluate_mini_batch(
model,
feats_test_tran,
labels_test_tran,
criterion,
batch_size,
evaluator,
)
_, loss_test_ind, score_test_ind = evaluate_mini_batch(
model,
feats_test_ind,
labels_test_ind,
criterion,
batch_size,
evaluator,
)
else:
obs_out, loss_train, score_train = evaluate(
model,
obs_data_eval,
obs_feats,
obs_labels,
criterion,
evaluator,
obs_idx_train,
)
# Use criterion & evaluator instead of evaluate to avoid redundant forward pass
loss_val = criterion(
obs_out[obs_idx_val], obs_labels[obs_idx_val]
).item()
score_val = evaluator(obs_out[obs_idx_val], obs_labels[obs_idx_val])
loss_test_tran = criterion(
obs_out[obs_idx_test], obs_labels[obs_idx_test]
).item()
score_test_tran = evaluator(
obs_out[obs_idx_test], obs_labels[obs_idx_test]
)
# Evaluate the inductive part with the full graph
out, loss_test_ind, score_test_ind = evaluate(
model, data_eval, feats, labels, criterion, evaluator, idx_test_ind
)
logger.debug(
f"Ep {epoch:3d} | loss: {loss:.4f} | s_train: {score_train:.4f} | s_val: {score_val:.4f} | s_tt: {score_test_tran:.4f} | s_ti: {score_test_ind:.4f}"
)
loss_and_score += [
[
epoch,
loss_train,
loss_val,
loss_test_tran,
loss_test_ind,
score_train,
score_val,
score_test_tran,
score_test_ind,
]
]
if score_val >= best_score_val:
best_epoch = epoch
best_score_val = score_val
state = copy.deepcopy(model.state_dict())
count = 0
else:
count += 1
if count == conf["patience"] or epoch == conf["max_epoch"]:
break
model.load_state_dict(state)
if "MLP" in model.model_name:
obs_out, _, score_val = evaluate_mini_batch(
model, obs_feats, obs_labels, criterion, batch_size, evaluator, obs_idx_val
)
out, _, score_test_ind = evaluate_mini_batch(
model, feats, labels, criterion, batch_size, evaluator, idx_test_ind
)
else:
obs_out, _, score_val = evaluate(
model,
obs_data_eval,
obs_feats,
obs_labels,
criterion,
evaluator,
obs_idx_val,
)
out, _, score_test_ind = evaluate(
model, data_eval, feats, labels, criterion, evaluator, idx_test_ind
)
score_test_tran = evaluator(obs_out[obs_idx_test], obs_labels[obs_idx_test])
out[idx_obs] = obs_out
logger.info(
f"Best valid model at epoch: {best_epoch :3d}, score_val: {score_val :.4f}, score_test_tran: {score_test_tran :.4f}, score_test_ind: {score_test_ind :.4f}"
)
return out, score_val, score_test_tran, score_test_ind
"""
3. Distill
"""
def distill_run_transductive(
conf,
model,
feats,
labels,
out_t_all,
distill_indices,
criterion_l,
criterion_t,
evaluator,
optimizer,
logger,
loss_and_score,
):
"""
Distill training and eval under the transductive setting.
The hard_label_train/soft_label_train/valid/test split is specified by `distill_indices`.
The input graph is assumed to be large, and MLP is assumed to be the student model. Thus, node feature only and mini-batch is used.
out_t: Soft labels produced by the teacher model.
criterion_l & criterion_t: Loss used for hard labels (`labels`) and soft labels (`out_t`) respectively
loss_and_score: Stores losses and scores.
"""
set_seed(conf["seed"])
device = conf["device"]
batch_size = conf["batch_size"]
lamb = conf["lamb"]
idx_l, idx_t, idx_val, idx_test = distill_indices
feats = feats.to(device)
labels = labels.to(device)
out_t_all = out_t_all.to(device)
feats_l, labels_l = feats[idx_l], labels[idx_l]
feats_t, out_t = feats[idx_t], out_t_all[idx_t]
feats_val, labels_val = feats[idx_val], labels[idx_val]
feats_test, labels_test = feats[idx_test], labels[idx_test]
best_epoch, best_score_val, count = 0, 0, 0
for epoch in range(1, conf["max_epoch"] + 1):
loss_l = train_mini_batch(
model, feats_l, labels_l, batch_size, criterion_l, optimizer, lamb
)
loss_t = train_mini_batch(
model, feats_t, out_t, batch_size, criterion_t, optimizer, 1 - lamb
)
loss = loss_l + loss_t
if epoch % conf["eval_interval"] == 0:
_, loss_l, score_l = evaluate_mini_batch(
model, feats_l, labels_l, criterion_l, batch_size, evaluator
)
_, loss_val, score_val = evaluate_mini_batch(
model, feats_val, labels_val, criterion_l, batch_size, evaluator
)
_, loss_test, score_test = evaluate_mini_batch(
model, feats_test, labels_test, criterion_l, batch_size, evaluator
)
logger.debug(
f"Ep {epoch:3d} | loss: {loss:.4f} | s_l: {score_l:.4f} | s_val: {score_val:.4f} | s_test: {score_test:.4f}"
)
loss_and_score += [
[epoch, loss_l, loss_val, loss_test, score_l, score_val, score_test]
]
if score_val >= best_score_val:
best_epoch = epoch
best_score_val = score_val
state = copy.deepcopy(model.state_dict())
count = 0
else:
count += 1
if count == conf["patience"] or epoch == conf["max_epoch"]:
break
model.load_state_dict(state)
out, _, score_val = evaluate_mini_batch(
model, feats, labels, criterion_l, batch_size, evaluator, idx_val
)
# Use evaluator instead of evaluate to avoid redundant forward pass
score_test = evaluator(out[idx_test], labels_test)
logger.info(
f"Best valid model at epoch: {best_epoch: 3d}, score_val: {score_val :.4f}, score_test: {score_test :.4f}"
)
return out, score_val, score_test
def distill_run_inductive(
conf,
model,
feats,
labels,
out_t_all,
distill_indices,
criterion_l,
criterion_t,
evaluator,
optimizer,
logger,
loss_and_score,
):
"""
Distill training and eval under the inductive setting.
The hard_label_train/soft_label_train/valid/test split is specified by `distill_indices`.
idx starting with `obs_idx_` contains the node idx in the observed graph `obs_g`.
idx starting with `idx_` contains the node idx in the original graph `g`.
The model is trained on the observed graph `obs_g`, and evaluated on both the observed test nodes (`obs_idx_test`) and inductive test nodes (`idx_test_ind`).
The input graph is assumed to be large, and MLP is assumed to be the student model. Thus, node feature only and mini-batch is used.
idx_obs: Idx of nodes in the original graph `g`, which form the observed graph 'obs_g'.
out_t: Soft labels produced by the teacher model.
criterion_l & criterion_t: Loss used for hard labels (`labels`) and soft labels (`out_t`) respectively.
loss_and_score: Stores losses and scores.
"""
set_seed(conf["seed"])
device = conf["device"]
batch_size = conf["batch_size"]
lamb = conf["lamb"]
(
obs_idx_l,
obs_idx_t,
obs_idx_val,
obs_idx_test,
idx_obs,
idx_test_ind,
) = distill_indices
feats = feats.to(device)
labels = labels.to(device)
out_t_all = out_t_all.to(device)
obs_feats = feats[idx_obs]
obs_labels = labels[idx_obs]
obs_out_t = out_t_all[idx_obs]
feats_l, labels_l = obs_feats[obs_idx_l], obs_labels[obs_idx_l]
feats_t, out_t = obs_feats[obs_idx_t], obs_out_t[obs_idx_t]
feats_val, labels_val = obs_feats[obs_idx_val], obs_labels[obs_idx_val]
feats_test_tran, labels_test_tran = (
obs_feats[obs_idx_test],
obs_labels[obs_idx_test],
)
feats_test_ind, labels_test_ind = feats[idx_test_ind], labels[idx_test_ind]
best_epoch, best_score_val, count = 0, 0, 0
for epoch in range(1, conf["max_epoch"] + 1):
loss_l = train_mini_batch(
model, feats_l, labels_l, batch_size, criterion_l, optimizer, lamb
)
loss_t = train_mini_batch(
model, feats_t, out_t, batch_size, criterion_t, optimizer, 1 - lamb
)
loss = loss_l + loss_t
if epoch % conf["eval_interval"] == 0:
_, loss_l, score_l = evaluate_mini_batch(
model, feats_l, labels_l, criterion_l, batch_size, evaluator
)
_, loss_val, score_val = evaluate_mini_batch(
model, feats_val, labels_val, criterion_l, batch_size, evaluator
)
_, loss_test_tran, score_test_tran = evaluate_mini_batch(
model,
feats_test_tran,
labels_test_tran,
criterion_l,
batch_size,
evaluator,
)
_, loss_test_ind, score_test_ind = evaluate_mini_batch(
model,
feats_test_ind,
labels_test_ind,
criterion_l,
batch_size,
evaluator,
)
logger.debug(
f"Ep {epoch:3d} | l: {loss:.4f} | s_l: {score_l:.4f} | s_val: {score_val:.4f} | s_tt: {score_test_tran:.4f} | s_ti: {score_test_ind:.4f}"
)
loss_and_score += [
[
epoch,
loss_l,
loss_val,
loss_test_tran,
loss_test_ind,
score_l,
score_val,
score_test_tran,
score_test_ind,
]
]
if score_val >= best_score_val:
best_epoch = epoch
best_score_val = score_val
state = copy.deepcopy(model.state_dict())
count = 0
else:
count += 1
if count == conf["patience"] or epoch == conf["max_epoch"]:
break
model.load_state_dict(state)
obs_out, _, score_val = evaluate_mini_batch(
model, obs_feats, obs_labels, criterion_l, batch_size, evaluator, obs_idx_val
)
out, _, score_test_ind = evaluate_mini_batch(
model, feats, labels, criterion_l, batch_size, evaluator, idx_test_ind
)
# Use evaluator instead of evaluate to avoid redundant forward pass
score_test_tran = evaluator(obs_out[obs_idx_test], labels_test_tran)
out[idx_obs] = obs_out
logger.info(
f"Best valid model at epoch: {best_epoch: 3d} score_val: {score_val :.4f}, score_test_tran: {score_test_tran :.4f}, score_test_ind: {score_test_ind :.4f}"
)
return out, score_val, score_test_tran, score_test_ind