-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
217 lines (172 loc) · 6.1 KB
/
utils.py
File metadata and controls
217 lines (172 loc) · 6.1 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
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
"""Utility file for custom funcs/objects
"""
import numpy as np
import torch
import matplotlib.pyplot as plt
import imageio
import seaborn as sns
from torch.distributions import MultivariateNormal
from models import GMM
from matplotlib import cm
MIN, MAX = -10, 10
sns.set_style('white')
class EarlyStopping:
"""Early stops if validation loss doesn't improve after a given patience.
From https://github.com/Bjarten/early-stopping-pytorch
"""
def __init__(self, patience=7, verbose=False, delta=0):
"""
Args:
patience (int): How long to wait after last time
validation loss improved. Default: 7
verbose (bool): If True, prints a message for each
validation loss improvement. Default: False
delta (float): Minimum change in the monitored quantity
to qualify as an improvement. Default: 0
"""
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
def __call__(self, val_loss, model):
score = -val_loss
if self.best_score is None:
self.best_score = score
#self.save_checkpoint(val_loss, model)
elif score < self.best_score + self.delta:
self.counter += 1
if self.verbose:
print(f'EarlyStopping counter: {self.counter}')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
#self.save_checkpoint(val_loss, model)
self.counter = 0
def save_checkpoint(self, val_loss, model):
'''Saves model when validation loss decrease.'''
if self.verbose:
print(f'Validation loss {val_loss:.6f}. Saving model ...')
torch.save(model.state_dict(), 'checkpoint.pt')
self.val_loss_min = val_loss
def normed_logprob_gradient_score(dataloader, model, estimator):
"""Returns the OOD score function from the paper
"""
scores = []
model.eval()
for x, _ in dataloader:
# require gradient of input
x.requires_grad = True
# forward pass
outs = model(x)
logprobs = estimator(outs)
# get the logprob gradients
x.retain_grad()
logprobs.backward()
scores.extend(-torch.norm(x.grad, dim=1).detach().numpy())
return scores
def print_stats(tensor):
"""Prints various statistics of a tensor"""
print(f"shape: {tensor.size()}" +
f" | min/max: {tensor.min():.3f}/{tensor.max():.3f}" +
f" | mean: {tensor.mean():.3f} | stddev: {tensor.std():.3f}")
def plot_density(samples, filename=None):
"""Plots probability density of generated samples
Args:
samples
"""
fig, ax = plt.subplots()
data = [list(l) for l in zip(*samples)]
# plot parameters
plt.xlim(MIN, MAX)
plt.ylim(MIN, MAX)
plt.title("Estimated Probability Density")
plt.xlabel("x")
plt.ylabel("y")
sns.kdeplot(*data, shade=True, cmap="Blues", ax=ax)
ax.collections[0].set_alpha(0)
if filename is None:
plt.show()
else:
plt.savefig(filename, dpi=300)
plt.close()
def energy(x):
"""Returns numpy array of energy function outputs
"""
return -torch.logsumexp(x, axis=1).detach().numpy()
def model_energies(model, n, bmin=-1, bmax=1):
"""Computes energies in a grid using a trained model
"""
x = torch.linspace(bmax, bmin, n)
y = torch.linspace(bmin, bmax, n)
points = torch.stack(torch.meshgrid(x, y), dim=-1)
# fill the density map
energy = [model.energy(pts).detach().numpy() for pts in points]
return np.array(energy, dtype=np.float32)
def density_map(model,
n, samples,
bmin=-1, bmax=1,
filename="images/density/dplot.jpg"):
"""Plots loglikehood heatmap of learned model
Args:
model (nn.Module): learned model
bmin (float): min x/y value
bmax (float): max x/y value
n (int): number of points to plot sqrted
samples (int): number of samples from prior
"""
x = torch.linspace(bmax, bmin, n)
y = torch.linspace(bmin, bmax, n)
points = torch.stack(torch.meshgrid(x, y), dim=-1)
# fill the density map
density = [model.likelihood(pts, samples).detach().numpy() for pts in points]
density = np.array(density, dtype=np.float32)
# plot density map
plt.figure()
plt.imshow(density, extent=[bmin, bmax, bmin, bmax])
plt.savefig(filename, dpi=300)
def plot_loss(losses, filename=None):
"""Plot losses from an array
"""
plt.figure()
plt.title("Loss Curve")
plt.xlabel("Iterations")
plt.ylabel("Loss")
plt.plot(losses)
if filename is not None:
plt.savefig(filename, dpi=300)
else:
plt.show()
def energy_map(model, bmin, bmax, n, image=True):
"""Creates heatmap of energy function for 2D data
Args:
model (nn.Module): pytorch model
bmin (float): min x/y value
bmax (float): max x/y value
n (float): number of points to interpolate in [bmin, bmax]
image (bool): return an image for creating gifs
"""
# compute the energies
logits = [model(torch.Tensor([[x, y] for y in np.linspace(bmin, bmax, n)]))
for x in np.linspace(bmin, bmax, n)]
energies = np.array([energy(l) for l in logits])
if image:
emin, emax = energies.min(), energies.max()
return np.uint8(255 * cm.viridis((energies - emin) / (emax - emin)))
return energies
def energy_plot(energies, bmin=-1, bmax=1, filename=None):
"""Saves a heatmap of the energy function
Args:
filename (str): filename for saving the plot
"""
fig = plt.figure()
plt.imshow(energies, extent=[bmin, bmax, bmin, bmax])
plt.colorbar()
plt.savefig(filename, dpi=300, bbox_inches='tight')
def makegif(gif_name, data, **kwargs):
"""Saves a GIF file
"""
imageio.mimwrite(gif_name, data, **kwargs)