-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
67 lines (57 loc) · 2.44 KB
/
logger.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
import torch
class Logger(object):
def __init__(self, runs, info=None):
self.info = info
self.results = [[] for _ in range(runs)]
def add_result(self, run, result):
assert len(result) == 4
assert run >= 0 and run < len(self.results)
self.results[run].append(result)
def print_statistics(self, run=None, mode='max_acc'):
if run is not None:
result = 100 * torch.tensor(self.results[run])
argmax = result[:, 1].argmax().item()
argmin = result[:, 3].argmin().item()
if mode == 'max_acc':
ind = argmax
else:
ind = argmin
print_str=f'Run {run + 1:02d}:'+\
f'Highest Train: {result[:, 0].max():.2f} '+\
f'Highest Valid: {result[:, 1].max():.2f} '+\
f'Highest Test: {result[:, 2].max():.2f} '+\
f'Chosen epoch: {ind+1}\n'+\
f'Final Train: {result[ind, 0]:.2f} '+\
f'Final Test: {result[ind, 2]:.2f}'
print(print_str)
else:
best_results = []
max_val_epoch=0
for r in self.results:
r=100*torch.tensor(r)
train1 = r[:, 0].max().item()
test1 = r[:, 2].max().item()
valid = r[:, 1].max().item()
if mode == 'max_acc':
train2 = r[r[:, 1].argmax(), 0].item()
test2 = r[r[:, 1].argmax(), 2].item()
max_val_epoch=r[:, 1].argmax()
else:
train2 = r[r[:, 3].argmin(), 0].item()
test2 = r[r[:, 3].argmin(), 2].item()
best_results.append((train1, test1, valid, train2, test2))
best_result = torch.tensor(best_results)
print_str=f'{len(self.results)} runs: '
r = best_result[:, 0]
print_str+=f'Highest Train: {r.mean():.2f} ± {r.std():.2f} '
print_str+=f'Highest val epoch:{max_val_epoch}\n'
r = best_result[:, 1]
print_str+=f'Highest Test: {r.mean():.2f} ± {r.std():.2f} '
r = best_result[:, 4]
print_str+=f'Final Test: {r.mean():.2f} ± {r.std():.2f}'
self.test=r.mean()
return print_str
def output(self,out_path,info):
with open(out_path,'a') as f:
f.write(info)
f.write(f'test acc:{self.test}\n')