-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutil.py
More file actions
47 lines (37 loc) · 1.38 KB
/
util.py
File metadata and controls
47 lines (37 loc) · 1.38 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
import io
import torch
import numpy as np
class MultiDimAverageMeter(object):
def __init__(self, dims):
self.dims = dims
self.cum = torch.zeros(np.prod(dims))
self.cnt = torch.zeros(np.prod(dims))
self.idx_helper = torch.arange(np.prod(dims), dtype=torch.long).reshape(
*dims
)
def add(self, vals, idxs):
flattened_idx = torch.stack(
[self.idx_helper[tuple(idxs[i])] for i in range(idxs.size(0))],
dim=0,
)
self.cum.index_add_(0, flattened_idx, vals.view(-1).float())
self.cnt.index_add_(
0, flattened_idx, torch.ones_like(vals.view(-1), dtype=torch.float)
)
def get_mean(self):
return (self.cum / self.cnt).reshape(*self.dims)
def reset(self):
self.cum.zero_()
self.cnt.zero_()
class EMA:
def __init__(self, label, alpha=0.9):
self.label = label
self.alpha = alpha
self.parameter = torch.zeros(label.size(0))
self.updated = torch.zeros(label.size(0))
def update(self, data, index):
self.parameter[index] = self.alpha * self.parameter[index] + (1-self.alpha*self.updated[index]) * data
self.updated[index] = 1
def max_loss(self, label):
label_index = np.where(self.label == label)[0]
return self.parameter[label_index].max()