-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
311 lines (235 loc) · 9.15 KB
/
utilities.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
# @Author: charles
# @Date: 2021-09-08 14:09:79
# @Email: charles.berube@polymtl.ca
# @Last modified by: charles
# @Last modified time: 2022-08-05 15:08:61
import os
import math
import warnings
from timeit import default_timer as timer
import torch
from torch.utils.data.sampler import SubsetRandomSampler
import torch.nn.functional as F
import numpy as np
from tqdm import tqdm
warnings.filterwarnings(
"ignore", message="Initializing zero-element tensors is a no-op")
def softclip(x, min):
return min + F.softplus(x - min)
def min_max_scale(x):
return (x - np.min(x))/(np.max(x) - np.min(x))
def str_with_err(value, error):
if error > 0:
digits = -int(math.floor(math.log10(error)))
else:
digits = 0
if digits < 0:
digits = 0
err10digits = math.floor(error*10**digits)
return "${0:.{2}f}({1:.0f})$".format(value, err10digits, digits)
def append_suffix(fpath, suffix):
name, ext = os.path.splitext(fpath)
return f"{name}-{suffix}{ext}"
def compute_MARP0(X):
MARP0 = 0
for i in range(len(X)):
for j in range(i):
MARP0 += np.abs(X[i] - X[j])
MARP0 *= 2/(len(X)**2)
return MARP0
def weights_init(m):
if isinstance(m, torch.nn.Linear):
# torch.nn.init.xavier_uniform_(m.weight)
torch.nn.init.kaiming_uniform_(m.weight)
def normalize(X, axis=1):
X_max = X.max(axis=axis, keepdims=True)
X_min = X.min(axis=axis, keepdims=True)
return (X - X_min) / (X_max - X_min)
def standardize(X, axis=1):
X_avg = X.mean(axis=axis, keepdims=True)
X_std = X.std(axis=axis, keepdims=True)
return (X - X_avg) / X_std
def max_abs_scale(X, axis=1):
max_abs = np.abs(X).max(axis=axis, keepdims=True)
return X / max_abs
def normalize_complex(X, axis=None):
norm = np.linalg.norm(X, axis=-1, keepdims=True)
if axis is not None:
norm = norm.max(axis=axis)
return X / norm
def log_complex(X):
complex_X = X[:, :, 0] + 1j*X[:, :, 1]
LC = np.empty(X.shape)
LC[:, :, 0] = np.log(np.abs(complex_X))
LC[:, :, 1] = np.angle(complex_X)
return LC
def polarize(X):
complex_X = X[:, :, 0] + 1j*X[:, :, 1]
AP = np.empty(X.shape)
AP[:, :, 0] = np.abs(complex_X)
AP[:, :, 1] = np.angle(complex_X)
return AP
def wide_normalize(X, X_min=None, X_max=None):
if X_max is None:
X_max = X.max(0, keepdims=True).max(1, keepdims=True)
print("X_max:", X_max)
if (X_min is None):
X_min = X.min(0, keepdims=True).min(1, keepdims=True)
print("X_min:", X_min)
return (X - X_min) / (X_max - X_min)
def split_train_test(dataset, test_split=0.2, random_seed=None):
shuffle_dataset = True
# Creating data indices for training and validation splits:
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(test_split * dataset_size))
if shuffle_dataset:
np.random.seed(random_seed)
np.random.shuffle(indices)
train_indices, test_indices = indices[split:], indices[:split]
# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
test_sampler = SubsetRandomSampler(test_indices)
return train_sampler, test_sampler
def frange_cycle_sigmoid(start, stop, n_epoch, n_cycle=4, ratio=0.5):
L = np.ones(n_epoch)
period = n_epoch/n_cycle
step = (stop-start)/(period*ratio) # step is in [0,1]
for c in range(n_cycle):
v, i = start, 0
while v <= stop:
L[int(i+c*period)] = 1.0/(1.0 + np.exp(- (v*12.-6.)))
v += step
i += 1
return L
def frange_cycle_cosine(start, stop, n_epoch, n_cycle=4, ratio=0.5):
L = np.ones(n_epoch)
period = n_epoch/n_cycle
step = (stop-start)/(period*ratio) # step is in [0,1]
for c in range(n_cycle):
v, i = start, 0
while v <= stop:
L[int(i+c*period)] = 0.5-.5*math.cos(v*math.pi)
v += step
i += 1
return L
def log_normal_pdf(sample, mu, logvar, raxis=1):
log2pi = np.log(2. * np.pi)
return torch.sum(
-.5 * ((sample - mu) ** 2. * torch.exp(-logvar) + logvar + log2pi),
axis=raxis)
def train(model, train_loader, verbose, lr, n_epoch, device=None, beta=None,
valid_loader=None):
if device is None:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
train_losses = ['log_sigma', 'NLL', 'KLD', 'AUX', 'train']
valid_losses = ['valid']
grads = ['input_grad_total']
history = {k: np.zeros(n_epoch) for k in train_losses}
history.update({k: torch.zeros(model.cond_dim) for k in grads})
history.update({k: np.zeros(n_epoch) for k in valid_losses})
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
start_time = timer()
model.to(device)
for e in range(n_epoch):
running_loss = {k: 0 for k in train_losses} # reset running losses
model.train()
for X, c, y in train_loader:
X = X.to(device)
c = c.to(device)
y = y.to(device)
if c.shape[-1] > 0:
c.requires_grad = True
optimizer.zero_grad()
# Forward pass
Xp, mu, logvar, p = model(X, c)
if y.shape[-1] == 0:
AUX = torch.tensor(0)
elif y.shape[-1] > 1:
AUX = model.reconstruction_loss(p, y)
NLL, KLD = model.vae_loss(Xp, X, mu, logvar)
total_loss = NLL + KLD + AUX
running_loss['log_sigma'] += model.log_sigma.item()*X.size(0)
running_loss['NLL'] += NLL.item()
running_loss['KLD'] += KLD.item()
running_loss['AUX'] += AUX.item()*X.size(0)
running_loss['train'] += total_loss.item()
if c.shape[-1] > 0:
y_grad_total = torch.autograd.grad(
NLL, c, retain_graph=True)[0]
history['input_grad_total'] += y_grad_total.square().sum(0)
# Backward pass
total_loss.backward()
optimizer.step()
for k in train_losses:
history[k][e] = running_loss[k]/len(train_loader.sampler)
verbose_str = (f"Epoch: {(e+1):.0f}, "
f"log sigma: {history['log_sigma'][e]:.2f}, "
f"NLL: {history['NLL'][e]:.0f}, "
f"KLD: {history['KLD'][e]:.0f}, "
f"AUX: {history['AUX'][e]:.3f}, "
f"Train: {history['train'][e]:.0f}"
)
if valid_loader:
model.eval()
running_loss = {k: 0 for k in valid_losses} # reset running losses
for X, c, y in valid_loader:
X = X.to(device)
c = c.to(device)
y = y.to(device)
# Forward pass
Xp, mu, logvar, p = model(X, c)
if y.shape[-1] == 0:
AUX = torch.tensor(0)
elif y.shape[-1] > 1:
AUX = model.reconstruction_loss(p, y)
NLL, KLD = model.vae_loss(Xp, X, mu, logvar)
total_loss = NLL + KLD + AUX
running_loss['valid'] += total_loss.item()
for k in valid_losses:
history[k][e] = running_loss[k]/len(valid_loader.sampler)
verbose_str += f", Valid: {history['valid'][e]:.0f}"
if verbose:
if (e + 1) % verbose == 0:
print(verbose_str)
end_time = timer()
print(f'Training time: {(end_time - start_time)/60:.2f} m')
return history
def predict(model, dataloader, n_real=20, device=None, disable_prog_bar=False):
"""Infers reconstructions and labels from a test DataLoader.
"""
if device is None:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
N = len(dataloader.sampler)
inputs = torch.empty(N, model.input_dim)
outputs = torch.empty(N, n_real, model.input_dim)
conds = torch.empty(N, model.cond_dim)
labels = torch.empty(N, model.label_dim)
mus = torch.empty(N, model.latent_dim)
logvars = torch.empty(N, model.latent_dim)
preds = torch.empty(N, n_real, model.label_dim)
i = 0
model.to(device)
model.eval()
with torch.no_grad():
for (X, c, y) in tqdm(dataloader, disable=disable_prog_bar):
X = X.to(device)
c = c.to(device)
y = y.to(device)
B = X.shape[0]
inputs[i:i+B] = X
conds[i:i+B] = c
labels[i:i+B] = y
X = X.unsqueeze(1).expand(-1, n_real,
- 1).reshape(B*n_real, model.input_dim)
c = c.unsqueeze(1).expand(-1, n_real,
- 1).reshape(B*n_real, model.cond_dim)
Xp, mu, logvar, p = model(X, c)
outputs[i:i+B] = Xp.reshape(B, n_real, model.input_dim)
mus[i:i+B] = mu.reshape(B, n_real, model.latent_dim)[:, 0, :]
logvars[i:i+B] = logvar.reshape(B, n_real, model.latent_dim)[:, 0, :]
preds[i:i+B] = p.reshape(B, n_real, model.label_dim)
i += B
keys = ['inputs', 'labels', 'outputs', 'mus', 'logvars', 'preds']
scope = locals()
return {k: scope[k] for k in keys}