-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhelper.py
More file actions
84 lines (70 loc) · 2.51 KB
/
helper.py
File metadata and controls
84 lines (70 loc) · 2.51 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
import numpy as np
class AverageMeter:
def __init__(self, epoch=0, iteration=0):
'''
Record the metrics while the neural network is training, e.g. loss, accuracy
@Params:
epoch (int): Number of epoch
iteration (int): Number of batch, should be ceil(total/batch_num)
@Return:
None
'''
assert isinstance(epoch, int), 'Epoch must be integer'
assert isinstance(iteration, int), 'Iteration must be integer'
self.epoch = epoch
self.iteration = iteration
if epoch > 0 and iteration > 0:
self.__history = np.zeros((epoch, iteration))
else:
print('Appending mode activated')
self.__history = [[]]
self.e_counter = 0
self.i_counter = 0
@property
def history(self):
if isinstance(self.__history, list):
return np.array(self.__history[:self.e_counter])
return self.__history
def append(self, value):
'''
Append the metrics each iteration
@Params:
value: value of metric
@Return:
None
'''
if self.epoch > 0 and self.iteration > 0:
assert self.e_counter < self.epoch, \
'Too many epoch, index out of bound'
assert self.i_counter < self.iteration, \
'Too many iteration, index out of bound'
self.__history[self.e_counter, self.i_counter] = value
else:
self.__history[self.e_counter].append(value)
self.i_counter += 1
def step(self):
'''
Append the iterations each epoch
'''
if self.epoch == 0 and self.iteration == 0:
self.__history.append([])
self.e_counter += 1
self.i_counter = 0
def extend(self, am):
'''
Concatenate the history of AverageMeter
am (AverageMeter): Another AverageMeter object
'''
self.__history = np.concatenate((self.history, am.history), axis=0)
def get_average(self, indices=None):
'''
@Return:
the mean of epochs in history
'''
if indices is None:
return np.mean(self.__history[:self.e_counter], axis=1)
if indices == -1:
return np.mean(self.__history[self.e_counter, :self.i_counter])
if isinstance(indices, (tuple, list, int)):
return np.mean(self.__history[indices])
raise IndexError('Unknown indices, expected tuple, list or int')