-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstat.py
77 lines (61 loc) · 2.09 KB
/
stat.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
# Copyright (c) 2020
# Licensed under The MIT License
# Written by Zhiheng Li
# Email: [email protected]
import numpy as np
class AverageMeter(object):
"""Computes and stores the average and current value
Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class StdAverageMeter(object):
"""Computes and stores the average and current value
Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
self.val_lst = []
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
self.val_lst.append(val)
def std(self):
return np.std(np.array(self.val_lst))
class FoldEpochMat:
def __init__(self, num_fold, num_epoch, best_result_keys, *keys):
self.best_result_keys = best_result_keys
self.key_to_mat_dict = {}
for k in keys:
self.key_to_mat_dict[k] = np.zeros((num_fold, num_epoch))
def update(self, fold, epoch, key_val_dict: dict):
for key, val in key_val_dict.items():
self.key_to_mat_dict[key][fold, epoch] = val
def result(self, fold):
fold += 1
criterion = sum([self.key_to_mat_dict[key][:fold].mean(axis=0) for key in self.best_result_keys])
best_epoch = criterion.argmax()
mean_ret = {}
std_ret = {}
for key, mat in self.key_to_mat_dict.items():
colomn = mat[:fold, best_epoch]
mean_ret[key] = colomn.mean()
std_ret[key] = colomn.std()
return mean_ret, std_ret, best_epoch + 1