-
Notifications
You must be signed in to change notification settings - Fork 10
/
score.py
73 lines (55 loc) · 2.76 KB
/
score.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
from typing import List
import torch
from pydgn.training.callback.metric import Metric
class CGMMCompleteLikelihoodScore(Metric):
@property
def name(self) -> str:
return 'Complete Log Likelihood'
def __init__(self, use_as_loss=False, reduction='mean', use_nodes_batch_size=True):
super().__init__(use_as_loss=use_as_loss, reduction=reduction, use_nodes_batch_size=use_nodes_batch_size)
def on_training_batch_end(self, state):
self.batch_metrics.append(state.batch_score[self.name].item())
if state.model.is_graph_classification:
self.num_samples += state.batch_num_targets
else:
# This works for unsupervised CGMM
self.num_samples += state.batch_num_nodes
def on_eval_epoch_end(self, state):
state.update(epoch_score={self.name: torch.tensor(self.batch_metrics).sum() / self.num_samples})
self.batch_metrics = None
self.num_samples = None
def on_eval_batch_end(self, state):
self.batch_metrics.append(state.batch_score[self.name].item())
if state.model.is_graph_classification:
self.num_samples += state.batch_num_targets
else:
# This works for unsupervised CGMM
self.num_samples += state.batch_num_nodes
def _score_fun(self, targets, *outputs, batch_loss_extra):
return outputs[2]
def forward(self, targets: torch.Tensor, *outputs: List[torch.Tensor], batch_loss_extra: dict = None) -> dict:
return outputs[2]
class CGMMTrueLikelihoodScore(Metric):
@property
def name(self) -> str:
return 'True Log Likelihood'
def __init__(self, use_as_loss=False, reduction='mean', use_nodes_batch_size=True):
super().__init__(use_as_loss=use_as_loss, reduction=reduction, use_nodes_batch_size=use_nodes_batch_size)
def on_training_batch_end(self, state):
self.batch_metrics.append(state.batch_score[self.name].item())
if state.model.is_graph_classification:
self.num_samples += state.batch_num_targets
else:
# This works for unsupervised CGMM
self.num_samples += state.batch_num_nodes
def on_eval_batch_end(self, state):
self.batch_metrics.append(state.batch_score[self.name].item())
if state.model.is_graph_classification:
self.num_samples += state.batch_num_targets
else:
# This works for unsupervised CGMM
self.num_samples += state.batch_num_nodes
def _score_fun(self, targets, *outputs, batch_loss_extra):
return outputs[3]
def forward(self, targets: torch.Tensor, *outputs: List[torch.Tensor], batch_loss_extra: dict = None) -> dict:
return outputs[3]