-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenergy.py
190 lines (146 loc) · 5.87 KB
/
energy.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
import numpy as np
import matplotlib.pyplot as plt
from deeprob.spn.structure.leaf import Gaussian
from deeprob.spn.learning.wrappers import learn_estimator
from deeprob.spn.algorithms.sampling import sample
from deeprob.spn.models.ratspn import GaussianRatSpn
from deeprob.flows.models.realnvp import RealNVP1d
from deeprob.flows.models.maf import MAF
from deeprob.torch.routines import train_model
def energy_domain(domain, resolution=256):
left, right = domain
x = np.linspace(left, right, num=resolution)
y = np.linspace(left, right, num=resolution)
x, y = np.meshgrid(x, y)
return np.column_stack([x.flatten(), y.flatten()])
def w1(z):
return np.sin(2.0 * np.pi * z[:, 0] / 4.0)
def w2(z):
return 3.0 * np.exp(-0.5 * ((z[:, 0] - 1.0) / 0.6) ** 2)
def w3(z):
return 3.0 / (1.0 + np.exp((1.0 - z[:, 0]) / 0.3))
def moons_pdf(z):
u = 0.5 * ((np.linalg.norm(z, axis=1) - 2.0) / 0.4) ** 2
v = np.exp(-0.5 * ((z[:, 0] - 2.0) / 0.6) ** 2)
w = np.exp(-0.5 * ((z[:, 0] + 2.0) / 0.6) ** 2)
p = np.exp(-u) * (v + w)
return p / np.sum(p)
def waves_pdf(z):
p = np.exp(-0.5 * ((z[:, 1] - w1(z)) / 0.4) ** 2)
return p / np.sum(p)
def split_pdf(z):
u = np.exp(-0.5 * ((z[:, 1] - w1(z)) / 0.35) ** 2)
v = np.exp(-0.5 * ((z[:, 1] - w1(z) + w2(z)) / 0.35) ** 2)
p = u + v
return p / np.sum(p)
def nails_pdf(z):
u = np.exp(-0.5 * ((z[:, 1] - w1(z)) / 0.4) ** 2)
v = np.exp(-0.5 * ((z[:, 1] - w1(z) + w3(z)) / 0.35) ** 2)
p = u + v
return p / np.sum(p)
def generate_samples(pdf, domain, n_samples, noise=0.05):
probs = pdf(domain)
indices = np.arange(len(domain))
idx = np.random.choice(indices, size=n_samples, p=probs)
points = domain[idx] + np.random.randn(n_samples, 2) * noise
return points.astype(np.float32)
def energy_plot_pdf(ax, pdf):
ax.imshow(pdf, interpolation='bilinear', cmap='jet')
ax.axis('off')
def energy_plot_samples(ax, domain, resolution, samples):
left, right = domain
hist, _, _ = np.histogram2d(
samples[:, 1], samples[:, 0],
bins=resolution,
range=[[left, right], [left, right]],
density=True
)
energy_plot_pdf(ax, hist)
def spn_sample_energy(data, n_samples):
distributions = [Gaussian, Gaussian]
spn = learn_estimator(
data, distributions,
learn_leaf='mle',
split_rows='gmm',
split_cols='random',
min_rows_slice=512,
split_rows_kwargs={'n': 8},
verbose=False
)
nans = np.tile(np.nan, [n_samples, 2])
return sample(spn, nans, inplace=True)
def rat_sample_energy(data, n_samples):
n_train = int(0.9 * len(data))
data_valid = data[n_train:]
data_train = data[:n_train]
model = GaussianRatSpn(
in_features=2, optimize_scale=True,
rg_depth=1, rg_repetitions=1, rg_batch=16, rg_sum=16
)
train_model(
model, data_train, data_valid, setting='generative',
lr=1e-3, batch_size=256, epochs=100, patience=1, verbose=False
)
model.eval()
return model.sample(n_samples).cpu().numpy()
def nvp_sample_energy(data, n_samples):
n_train = int(0.9 * len(data))
data_valid = data[n_train:]
data_train = data[:n_train]
model = RealNVP1d(in_features=2, n_flows=10, units=128, batch_norm=False)
train_model(
model, data_train, data_valid, setting='generative',
lr=1e-4, batch_size=256, epochs=100, patience=1, checkpoint='energy-checkpoint.pt', verbose=False
)
model.eval()
return model.sample(n_samples).cpu().numpy()
def maf_sample_energy(data, n_samples):
n_train = int(0.9 * len(data))
data_valid = data[n_train:]
data_train = data[:n_train]
model = MAF(in_features=2, n_flows=10, units=128, batch_norm=False)
train_model(
model, data_train, data_valid, setting='generative',
lr=1e-4, batch_size=256, epochs=100, patience=1, checkpoint='energy-checkpoint.pt', verbose=False
)
model.eval()
return model.sample(n_samples).cpu().numpy()
if __name__ == '__main__':
domain = (-4.0, 4.0)
resolution = 128
n_samples = 100_000
pdfs_domain = energy_domain(domain, resolution)
# The PDFs functions
pdfs = {
'moons': moons_pdf,
'waves': waves_pdf,
'split': split_pdf,
'nails': nails_pdf
}
# Compute the grid-like shape pdf
pdfs_grid = {k: p(pdfs_domain).reshape(resolution, resolution) for k, p in pdfs.items()}
# Generate some data samples
samples = {k: generate_samples(p, pdfs_domain, n_samples, noise=0.05) for k, p in pdfs.items()}
# Initialize the result plot
fig, axs = plt.subplots(figsize=(20, 16), ncols=5, nrows=len(samples), squeeze=False)
# Learn a model for each density function
for i, energy in enumerate(samples):
print('Model: SPN - Energy: ' + energy)
energy_plot_samples(axs[i, 0], domain, resolution, spn_sample_energy(samples[energy], n_samples))
axs[0, 0].set_title('SPN', fontdict={'fontsize': 32})
print('Model: RAT-SPN - Energy: ' + energy)
energy_plot_samples(axs[i, 1], domain, resolution, rat_sample_energy(samples[energy], n_samples))
axs[0, 1].set_title('RAT-SPN', fontdict={'fontsize': 32})
print('Model: RealNVP - Energy: ' + energy)
energy_plot_samples(axs[i, 2], domain, resolution, nvp_sample_energy(samples[energy], n_samples))
axs[0, 2].set_title('RealNVP', fontdict={'fontsize': 32})
print('Model: MAF - Energy: ' + energy)
energy_plot_samples(axs[i, 3], domain, resolution, maf_sample_energy(samples[energy], n_samples))
axs[0, 3].set_title('MAF', fontdict={'fontsize': 32})
# Plot the true density functions
for i, energy in enumerate(pdfs_grid):
energy_plot_pdf(axs[i, -1], pdfs_grid[energy])
axs[0, -1].set_title('True', fontdict={'fontsize': 32})
# Save the results
plt.tight_layout()
plt.savefig('energy.png')