-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
55 lines (45 loc) · 1.48 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
import torch
import torch.nn.init as init
import numpy as np
import torch.nn as nn
import random
def fix_random_seeds(seed: int) -> None:
"""
Use in the beginning of the program only.
"""
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
np.random.seed(seed)
random.seed(seed)
def normc_initializer(std, m):
with torch.no_grad():
if type(m) == nn.Linear:
init.normal_(m.weight)
m.weight *= std / torch.sqrt(torch.sum(m.weight**2))
def count_parameters(policy) -> int:
return sum(p.numel() for p in policy.parameters())
def unroll_parameters(parameters) -> torch.tensor:
parameters = [parameter.data.flatten() for parameter in parameters]
parameters = torch.cat(parameters, dim=0)
return parameters
def fill_policy_parameters(policy, parameters) -> None:
cur_ind = 0
for param in policy.parameters():
size = len(param.data)
param.data = parameters[cur_ind:cur_ind+size].view(param.data.size())
cur_ind += size
def compute_ranks(x):
"""
Returns ranks in [0, len(x))
Note: This is different from scipy.stats.rankdata, which returns ranks in [1, len(x)].
"""
assert x.ndim == 1
ranks = np.empty(len(x), dtype=int)
ranks[x.argsort()] = np.arange(len(x))
return ranks
def compute_centered_ranks(x):
y = compute_ranks(x.ravel()).reshape(x.shape).astype(np.float32)
y /= (x.size - 1)
y -= .5
return y