-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·326 lines (263 loc) · 10.5 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import numpy as np
import torch
import random
import json
from hashlib import sha1
from einops import reduce
from collections import defaultdict
import itertools
import time
#loss function with rel/abs Lp loss
class LpLoss(object):
def __init__(self, d=2, p=2, size_average=True, reduction=True):
super().__init__()
#Dimension and Lp-norm type are postive
assert d > 0 and p > 0
self.d = d
self.p = p
self.reduction = reduction
self.size_average = size_average
def abs(self, x, y):
num_examples = x.size()[0]
#Assume uniform mesh
h = 1.0 / (x.size()[1] - 1.0)
all_norms = (h**(self.d/self.p))*torch.norm(x.reshape(num_examples,-1) - y.reshape(num_examples,-1), self.p, 1)
if self.reduction:
if self.size_average:
return torch.mean(all_norms)
else:
return torch.sum(all_norms)
return all_norms
def rel(self, x, y):
num_examples = x.size()[0]
diff_norms = torch.norm(x.reshape(num_examples,-1) - y.reshape(num_examples,-1), self.p, 1)
y_norms = torch.norm(y.reshape(num_examples,-1), self.p, 1)
if self.reduction:
if self.size_average:
return torch.mean(diff_norms/y_norms)
else:
return torch.sum(diff_norms/y_norms)
return diff_norms/y_norms
def __call__(self, x, y):
if type(x) == tuple:
x = x[0]
return self.rel(x, y)
# normalization, pointwise gaussian
class UnitGaussianNormalizer:
def __init__(self, x, eps=0.00001, reduce_dim=[0], verbose=True):
super().__init__()
n_samples, *shape = x.shape
self.sample_shape = shape
self.verbose = verbose
self.reduce_dim = reduce_dim
# x could be in shape of ntrain*n or ntrain*T*n or ntrain*n*T
self.mean = torch.mean(x, reduce_dim, keepdim=True).squeeze(0)
self.std = torch.std(x, reduce_dim, keepdim=True).squeeze(0)
self.eps = eps
if verbose:
print(f'UnitGaussianNormalizer init on {n_samples}, reducing over {reduce_dim}, samples of shape {shape}.')
print(f' Mean and std of shape {self.mean.shape}, eps={eps}')
def encode(self, x):
# x = x.view(-1, *self.sample_shape)
x = x - self.mean
x = x / (self.std + self.eps)
# x = (x.view(-1, *self.sample_shape) - self.mean) / (self.std + self.eps)
return x
def decode(self, x, sample_idx=None):
if sample_idx is None:
std = self.std + self.eps # n
mean = self.mean
else:
if len(self.mean.shape) == len(sample_idx[0].shape):
std = self.std[sample_idx] + self.eps # batch*n
mean = self.mean[sample_idx]
if len(self.mean.shape) > len(sample_idx[0].shape):
std = self.std[:,sample_idx]+ self.eps # T*batch*n
mean = self.mean[:,sample_idx]
# x is in shape of batch*n or T*batch*n
# x = (x.view(self.sample_shape) * std) + mean
# x = x.view(-1, *self.sample_shape)
x = x * std
x = x + mean
return x
def cuda(self):
self.mean = self.mean.cuda()
self.std = self.std.cuda()
return self
def cpu(self):
self.mean = self.mean.cpu()
self.std = self.std.cpu()
return self
def to(self, device):
self.mean = self.mean.to(device)
self.std = self.std.to(device)
return self
def nll_mu_var(out, y):
# out: Tuple of tensors (mu, var, ...)
mu, var = out[0], out[1]
nll = ((mu - y).pow(2)/var + torch.log(var)).sum()
return nll
# Metrics
def compute_mse_by_t(mu, y, reduce="sum"):
# out & y: nf nx nt d
test_mse_by_t = ((mu - y)**2).sum(dim=[0, 1, 3]) / y.shape[1]
if reduce == "mean":
# Mean over n_samples
test_mse_by_t /= y.shape[0]
return test_mse_by_t
def compute_mse_by_example(mu, var, y):
# out & y: nf nx nt d
test_mse_by_example = ((mu - y)**2).mean(dim=[1, 2, 3])
return test_mse_by_example
def compute_nll_by_example(mu, var, y):
nll_by_example = ((mu - y).pow(2)/var + torch.log(2 * np.pi * var)).sum(dim=[1,2,3]) / 2
return nll_by_example
def compute_nMeRCI(mu, var, y, alpha=0.95):
# Compute n-MeRCI (normalized Mean Rescaled Confidence Interval) for correlation between uncertainty and errors.
# Papers: https://arxiv.org/pdf/1908.07253.pdf, https://www.sciencedirect.com/science/article/pii/S0045782522004595#b55
# Smaller values (closer to zero) is better.
mae = torch.abs(mu - y).sum(dim=[1,2,3])
std = torch.sqrt(var.sum(dim=[1,2,3]))
lamda = mae / std
lamda_alpha = torch.quantile(lamda, alpha)
# Should be equal to alpha
# print((mae <= std * lamda_alpha).float().mean())
num = (lamda_alpha * std).mean() - mae.mean()
denom = mae.max() - mae.mean()
return num/denom
def compute_rmsce(mu, var, y, nbins=10):
# Compute root mean squared calibration error.
dist = torch.distributions.Normal(mu, torch.sqrt(var)+1e-10)
ps = torch.linspace(0, 1, nbins+1)
calibration_err = [(p - (y <= dist.icdf(p)).float().mean(dim=0))**2 for p in ps]
calibration_err = torch.stack(calibration_err).mean(dim=0).sqrt()
return calibration_err.mean()
def compute_crps_by_example(mu, var, y, nbins=10):
# Compute Continuous Ranked Probability Score (CRPS)
# (https://www.jstor.org/stable/23243806?seq=4, https://arxiv.org/pdf/2102.00968.pdf)
ps = torch.linspace(0, 1, nbins+1)[1:-1]
dist = torch.distributions.Normal(mu, torch.sqrt(var)+1e-10)
crps = 0.
for p in ps:
y_pred_at_p = dist.icdf(p)
ql_p = ((y_pred_at_p > y).int() - p) * (y_pred_at_p - y)
crps += ql_p
crps *= 2/len(ps)
return crps.mean(dim=[1,2,3])
def compute_piw_by_example(mu, var, y):
# Assumes p=0.95
std = torch.sqrt(var)
piw = 2 * 1.96 * std
return piw.mean(dim=[1,2,3])
def compute_forward_time(model, x, repetitions=100):
warmup = repetitions // 10
times = []
for i in range(warmup + repetitions):
t0 = time.time()
_ = model(x)
torch.cuda.current_stream().synchronize()
time_taken = (time.time() - t0) * 1000 # in ms
if i >= warmup:
times.append(time_taken)
return np.mean(times)
def compute_n_params(model):
n_params = 0
for p in model.parameters():
n_params += np.prod(p.shape)
return int(n_params)
def compute_n_flops(model_name, Np, fno_modes, fno_width, n_layers, n_models):
# Assumes d_i = d_o = 1
lifting_layer = 2 * Np * fno_width
fourier_layer = 10 * fno_width * Np * np.log2(Np) + fno_modes * (2 * fno_width**2 - fno_width) + 2 * Np * fno_width**2
projection_layer = 2 * Np * fno_width
if model_name.lower() == 'EnsembleFNO2d'.lower():
n_flops = n_models * (lifting_layer + n_layers * fourier_layer + projection_layer)
elif model_name.lower() == 'DiverseFNO2d'.lower():
n_flops = lifting_layer + n_layers * fourier_layer + n_models * projection_layer
else:
n_flops = -1
return int(n_flops)
def compute_all_metrics(out, y, results, metrics=None):
if type(out) == tuple:
mu, var = out[0], out[1]
else:
mu = out
var = torch.zeros_like(mu) + 1e-20
if metrics is None:
metrics = ["mse", "nll", "piw", "crps"]
results_ = {}
for metric in metrics:
metric_fn = globals()[f"compute_{metric}_by_example"]
results_[f"{metric}_by_example"] = metric_fn(mu, var, y).detach().cpu()
results_[metric] = results_[f"{metric}_by_example"].sum().item()
for key in results_.keys():
if key not in results:
results[key] = results_[key]
else:
if key.endswith("by_example"):
results[key] = torch.cat([results[key], results_[key]], dim=0)
else:
results[key] += results_[key]
return results
def plot_at_time(index, t_plot, t_sliced, grid, y, mu, std, ax, **kwargs):
ax.plot(grid, y[index, :, t_plot], label=f"True Solution (t={t_sliced[t_plot]:.1f})")
ax.plot(grid, mu[index, :, t_plot], label=f"Predicted (t={t_sliced[t_plot]:.1f})")
if std is not None:
ax.fill_between(grid, mu[index, :, t_plot]-3*std[index, :, t_plot], mu[index, :, t_plot]+3*std[index, :, t_plot], color='b', alpha=0.1)
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$u(x, t)$")
ax.legend()
if "ylim" in kwargs:
ax.set_ylim(*kwargs["ylim"])
if "title" in kwargs:
ax[0].set_title(kwargs["title"])
# ax[0].set_xlim(-0.05, 1.05)
# ax[0].set_ylim(-1, 1)
def set_seed(seed=314_271):
# Set seed for random, numpy and torch
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic=True
torch.backends.cudnn.benchmark=True
def config_to_hash(config):
config_repr = json.dumps(config, sort_keys=True)
return sha1(config_repr.encode()).hexdigest()
def dict_to_file(d, filepath):
with open(filepath, 'w') as f:
json.dump(d, f)
def filter_config(config, keys, mode="remove", new_config=None):
if mode == "remove":
if new_config is None:
new_config = config.copy()
new_config_keys = list(new_config.keys())
for key in new_config_keys:
if key in keys or "." in key:
new_config.pop(key)
elif mode == "add":
if new_config is None:
new_config = {}
for key in keys:
new_config[key] = config[key]
return new_config
def generate_commands(filename, datasets, models, other_config, seed=0):
if type(seed) == int:
seed = [seed]
commands = []
for s in seed:
for dataset_name, dataset_config in datasets.items():
for model_name, model_config in models.items():
dataset_name = dataset_name.split(":")[0]
model_name = model_name.split(":")[0]
command = f"python -u {filename} "
command += f"--model={model_name} "
command += f"--dataset={dataset_name} "
command += f"--seed={s} "
# Dataset & Model parameters
config = dataset_config | model_config | other_config
values = [[f"{k}" if vi=="" else f"{k}={vi}" for vi in v] for k,v in config.items()]
for p in itertools.product(*values):
commands.append(command + " ".join(p))
return commands