-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
86 lines (63 loc) · 2.59 KB
/
utils.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
78
79
80
81
82
83
84
85
86
import torch
import scipy
import random
import numpy as np
from torch import nn
def set_randomness(random_seed: int = 2024):
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)
def combined_shape(length: int, shape: int = None):
if shape is None:
return (length,)
return (length, shape) if np.isscalar(shape) else (length, *shape)
def count_vars(m: nn.Module):
return sum([np.prod(p.shape) for p in m.parameters()])
def discount_cumsum(x, gamma: float):
return scipy.signal.lfilter([1], [1, float(-gamma)], x[::-1], axis=0)[::-1]
def soft_update(trg: nn.Module, src: nn.Module, tau: float) -> None:
for tp, sp in zip(trg.parameters(), src.parameters()):
tp.data.copy_((1 - tau) * tp.data + tau * sp.data)
class AverageMeter(object):
"""Computes and stores the average and current value"""
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 RunningMeanStd:
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
def __init__(self, epsilon=1e-4, shape=(), device: torch.device = torch.device("cpu")):
self.mean = torch.zeros(shape, dtype=torch.float32).to(device)
self.var = torch.ones(shape, dtype=torch.float32).to(device)
self.count = epsilon
def update(self, x):
batch_mean = torch.mean(x, dim=0)
batch_var = torch.var(x, dim=0)
batch_count = x.shape[0]
self.update_from_moments(batch_mean, batch_var, batch_count)
def update_from_moments(self, batch_mean, batch_var, batch_count):
self.mean, self.var, self.count = update_mean_var_count_from_moments(
self.mean, self.var, self.count, batch_mean, batch_var, batch_count
)
def update_mean_var_count_from_moments(mean, var, count, batch_mean, batch_var, batch_count):
delta = batch_mean - mean
tot_count = count + batch_count
new_mean = mean + delta * batch_count / tot_count
m_a = var * count
m_b = batch_var * batch_count
M2 = m_a + m_b + torch.square(delta) * count * batch_count / tot_count
new_var = M2 / tot_count
new_count = tot_count
return new_mean, new_var, new_count