forked from HekpoMaH/Neural-Bipartite-Matching
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bellmanford.py
More file actions
142 lines (121 loc) · 4.16 KB
/
test_bellmanford.py
File metadata and controls
142 lines (121 loc) · 4.16 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
"""
Usage:
test.py [options] [--algorithms=ALGO]...
Options:
-h --help Show this screen.
--algorithms ALGO Which algorithms to add. One of {AugmentingPath, BFS, BellmanFord}. [default: BellmanFord]
--processor-type PROC Type of processor. One of {MPNN, PNA, GAT}. [default: MPNN]
--scale UP Test on larger graph size. Remember to add underscore (e.g. _30) [default: ]
"""
import sys
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch_geometric
from torch_geometric.data import DataLoader
from tqdm import tqdm
from docopt import docopt
from flow_datasets import SingleIterationDataset
from hyperparameters import get_hyperparameters
from train_bellmanford import iterate_over, load_algorithms, get_print_info
from flow_datasets import BellmanFordDataset
from models import AlgorithmProcessor, AugmentingPathNetwork
# args["--use-ints"] = True # Always uses integers
def get_print_format():
fmt = """
==========================
Mean step acc: {:.4f} Last step acc: {:.4f}
loss-(dist,pred,term,total): {:.4f} {:.4f} {:.4f} {:.4f}
===============
"""
return fmt
if __name__ == "__main__":
args = docopt(__doc__)
hyperparameters = get_hyperparameters()
DEVICE = hyperparameters["device"]
DIM_LATENT = hyperparameters["dim_latent"]
processor = AlgorithmProcessor(
DIM_LATENT, BellmanFordDataset, args["--processor-type"]
).to(DEVICE)
load_algorithms(args["--algorithms"], processor)
NAME = (
'BellmanFord'+args["--processor-type"]+str(hyperparameters["test_lr"])+str(hyperparameters["test_weight_decay"])
)
processor.load_state_dict(torch.load(f'best_models/best_{NAME}.pt'))
processor.eval()
# upscale = args["--upscale"]
for algorithm in processor.algorithms.values():
algorithm.test_dataset = algorithm.dataset_class(
algorithm.dataset_root, split="test"+args["--scale"], less_wired=True, device="cpu"
)
# print(algorithm.test_dataset[0].x[:, 0, 0])
iterate_over(processor, test=True)
# if "AugmentingPath" not in processor.algorithms:
# print("Mean/Last step acc", processor.algorithms["BFS"].get_validation_accuracies())
# exit(0)
fmt = get_print_format()
(
total_loss_dist,
total_loss_pred,
total_loss_term,
total_loss,
mean_step_acc,
final_step_acc,
) = get_print_info(processor.algorithms["BellmanFord"])
if get_hyperparameters()["calculate_termination_statistics"]:
print(
"Term precision:",
processor.algorithms["BellmanFord"].true_positive
/ (
processor.algorithms["BellmanFord"].true_positive
+ processor.algorithms["BellmanFord"].false_positive
)
if processor.algorithms["BellmanFord"].true_positive
+ processor.algorithms["BellmanFord"].false_positive
else "N/A",
)
print(
"Term recall:",
processor.algorithms["BellmanFord"].true_positive
/ (
processor.algorithms["BellmanFord"].true_positive
+ processor.algorithms["BellmanFord"].false_negative
)
if processor.algorithms["BellmanFord"].true_positive
+ processor.algorithms["BellmanFord"].false_negative
else "N/A",
)
print(
fmt.format(
mean_step_acc,
final_step_acc,
total_loss_dist,
total_loss_pred,
total_loss_term,
total_loss,
)
)
# print(
# fmt.format(
# mean_step_acc,
# final_step_acc,
# tnr,
# subtract_acc,
# total_loss_dist,
# total_loss_pred,
# total_loss_term,
# # find_min,
# total_loss,
# # sum(broken_invariants),
# # len_broken,
# # sum(broken_all),
# # len_broken,
# # sum(broken_reachabilities),
# # len_broken,
# # sum(broken_flows),
# # len_broken,
# "N/A",
# )
# )