forked from YunzhuLi/CompositionalKoopmanOperators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
137 lines (96 loc) · 3.08 KB
/
utils.py
File metadata and controls
137 lines (96 loc) · 3.08 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import sys
import h5py
import numpy as np
import torch
from torch.autograd import Variable
def print_args(args):
print("===== Experiment Configuration =====")
options = vars(args)
for key, value in options.items():
print(f'{key}: {value}')
print("====================================")
def rand_float(lo, hi):
return np.random.rand() * (hi - lo) + lo
def rand_int(lo, hi):
return np.random.randint(lo, hi)
def calc_dis(a, b):
return np.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
def norm(x, p=2):
return np.power(np.sum(x ** p), 1. / p)
def store_data(data_names, data, path):
hf = h5py.File(path, 'w')
for i in range(len(data_names)):
hf.create_dataset(data_names[i], data=data[i])
hf.close()
def load_data(data_names, path):
hf = h5py.File(path, 'r')
data = []
for i in range(len(data_names)):
d = np.array(hf.get(data_names[i]))
data.append(d)
hf.close()
return data
def combine_stat(stat_0, stat_1):
mean_0, std_0, n_0 = stat_0[:, 0], stat_0[:, 1], stat_0[:, 2]
mean_1, std_1, n_1 = stat_1[:, 0], stat_1[:, 1], stat_1[:, 2]
mean = (mean_0 * n_0 + mean_1 * n_1) / (n_0 + n_1)
std = np.sqrt(
(std_0 ** 2 * n_0 + std_1 ** 2 * n_1 + (mean_0 - mean) ** 2 * n_0 + (mean_1 - mean) ** 2 * n_1) / (n_0 + n_1))
n = n_0 + n_1
return np.stack([mean, std, n], axis=-1)
def init_stat(dim):
# mean, std, count
return np.zeros((dim, 3))
def var_norm(x):
return torch.sqrt((x ** 2).sum()).item()
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def get_flat(x, keep_dim=False):
if keep_dim:
return x.reshape(torch.Size([1, x.size(0) * x.size(1)]) + x.size()[2:])
return x.reshape(torch.Size([x.size(0) * x.size(1)]) + x.size()[2:])
def to_var(tensor, use_gpu, requires_grad=False):
if use_gpu:
return Variable(torch.FloatTensor(tensor).cuda(), requires_grad=requires_grad)
else:
return Variable(torch.FloatTensor(tensor), requires_grad=requires_grad)
def to_np(x):
return x.detach().cpu().numpy()
def mix_iters(iters):
table = []
for i, iter in enumerate(iters):
table += [i] * len(iter)
np.random.shuffle(table)
for i in table:
yield iters[i].next()
class Tee(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
self.file.close()
def write(self, data):
self.file.write(data)
self.stdout.write(data)
def flush(self):
self.file.flush()
def close(self):
self.__del__()
class AverageMeter(object):
def __init__(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
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