-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevaluate_model_utils.py
More file actions
144 lines (114 loc) · 8.85 KB
/
Copy pathevaluate_model_utils.py
File metadata and controls
144 lines (114 loc) · 8.85 KB
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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from tqdm import tqdm
import numpy as np
import logging
import time
import argparse
import os
import json
from models.EdgeBank import edge_bank_link_prediction
from utils.metrics import get_link_prediction_metrics, get_node_classification_metrics
from utils.utils import set_random_seed
from utils.utils import NegativeEdgeSampler, NeighborSampler
from utils.DataLoader import Data
def evaluate_model_link_prediction(model_name: str, model: nn.Module, final_trained_positional_encoding: torch.Tensor,
neighbor_sampler: NeighborSampler, evaluate_idx_data_loader: DataLoader,
evaluate_neg_edge_sampler: NegativeEdgeSampler, evaluate_data: Data, loss_func: nn.Module, num_fft_batches: int = 100,
num_neighbors: int = 20, time_gap: int = 2000, ablation = 'none', testing = False):
# Ensures the random sampler uses a fixed seed for evaluation (i.e. we always sample the same negatives for validation / test set)
assert evaluate_neg_edge_sampler.seed is not None
evaluate_neg_edge_sampler.reset_random_state()
model[0].set_neighbor_sampler(neighbor_sampler)
model.eval()
with torch.no_grad():
# store evaluate losses and metrics
evaluate_losses, evaluate_metrics = [], []
evaluate_idx_data_loader_tqdm = tqdm(evaluate_idx_data_loader, ncols=120)
positional_encoding = final_trained_positional_encoding
for batch_idx, evaluate_data_indices in enumerate(evaluate_idx_data_loader_tqdm):
evaluate_data_indices = evaluate_data_indices.numpy()
batch_src_node_ids, batch_dst_node_ids, batch_node_interact_times, batch_edge_ids = \
evaluate_data.src_node_ids[evaluate_data_indices], evaluate_data.dst_node_ids[evaluate_data_indices], \
evaluate_data.node_interact_times[evaluate_data_indices], evaluate_data.edge_ids[evaluate_data_indices]
if evaluate_neg_edge_sampler.negative_sample_strategy != 'random':
batch_neg_src_node_ids, batch_neg_dst_node_ids = evaluate_neg_edge_sampler.sample(size=len(batch_src_node_ids),
batch_src_node_ids=batch_src_node_ids,
batch_dst_node_ids=batch_dst_node_ids,
current_batch_start_time=batch_node_interact_times[0],
current_batch_end_time=batch_node_interact_times[-1])
else:
_, batch_neg_dst_node_ids = evaluate_neg_edge_sampler.sample(size=len(batch_src_node_ids))
batch_neg_src_node_ids = batch_src_node_ids
batch_node_ids = batch_src_node_ids.tolist() + batch_dst_node_ids.tolist()
batch_node_ids = torch.from_numpy(np.array(batch_node_ids)).unique().numpy()
if positional_encoding.shape[1] > num_fft_batches:
positional_encoding = torch.clone(positional_encoding[:,-num_fft_batches:, :])
# (batch_size, pe_dim)
fft_current_positional_encoding = model[0].fourier_transform_pe(batch_node_ids, positional_encoding, batch_idx)
current_positional_encoding = torch.clone(positional_encoding[:, -1, :])
current_positional_encoding[torch.from_numpy(batch_node_ids)] = fft_current_positional_encoding
if ablation == 'no_pe':
pos_src = model[0].aggregated_node_embeddings(node_ids = batch_src_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
pos_dst = model[0].aggregated_node_embeddings(node_ids = batch_dst_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
neg_src = model[0].aggregated_node_embeddings(node_ids = batch_neg_src_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
neg_dst = model[0].aggregated_node_embeddings(node_ids = batch_neg_dst_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
else:
pos_src = model[0].combining_pe_raw_feat(pe = current_positional_encoding,
node_ids = batch_src_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap,)
pos_dst = model[0].combining_pe_raw_feat(pe = current_positional_encoding,
node_ids = batch_dst_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
neg_src = model[0].combining_pe_raw_feat(pe = current_positional_encoding,
node_ids = batch_neg_src_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
neg_dst = model[0].combining_pe_raw_feat(pe = current_positional_encoding,
node_ids = batch_neg_dst_node_ids,
node_interact_times = batch_node_interact_times,
num_neighbors = num_neighbors,
time_gap = time_gap)
positive_probabilities = model[1](input_1=pos_src, input_2=pos_dst).squeeze(dim=-1).sigmoid().clamp(0, 1)
negative_probabilities = model[1](input_1=neg_src, input_2=neg_dst).squeeze(dim=-1).sigmoid().clamp(0, 1)
predicts = torch.cat([positive_probabilities, negative_probabilities], dim=0)
labels = torch.cat([torch.ones_like(positive_probabilities), torch.zeros_like(negative_probabilities)], dim=0)
batch_node_ids = batch_src_node_ids.tolist() + batch_dst_node_ids.tolist()
batch_node_ids = torch.from_numpy(np.array(batch_node_ids)).unique().numpy()
new_node_pe = model[0].update_pe(
pe = current_positional_encoding,
node_ids = batch_node_ids,
edge_ids = batch_edge_ids,
batch_src_node_ids = batch_src_node_ids,
batch_dst_node_ids = batch_dst_node_ids,
node_interact_times = batch_node_interact_times,
current_time = batch_node_interact_times.max(),
num_neighbors = num_neighbors,
time_gap = time_gap)
current_positional_encoding = new_node_pe
# current_positional_encoding[torch.from_numpy(batch_node_ids)] = new_node_pe
# spatial_node_embeddings[torch.from_numpy(batch_node_ids)] = new_spatial_node_embeddings
positional_encoding = torch.cat([positional_encoding, current_positional_encoding.unsqueeze(1)], dim = 1)
loss = loss_func(input=predicts, target=labels)
evaluate_losses.append(loss.item())
evaluate_metrics.append(get_link_prediction_metrics(predicts=predicts, labels=labels))
evaluate_idx_data_loader_tqdm.set_description(f'evaluate for the {batch_idx + 1}-th batch, evaluate loss: {loss.item()}')
return evaluate_losses, evaluate_metrics