-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset_prior.py
48 lines (39 loc) · 1.49 KB
/
set_prior.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
import tensorflow as tf
import tensorflow_probability as tfp
import matplotlib.pyplot as plt
tfpl = tfp.layers
tfd = tfp.distributions
tfkl = tf.keras.layers
tfb = tfp.bijectors
class SetPrior(tf.keras.Model):
def __init__(self, event_size, *args, **kwargs):
super(SetPrior, self).__init__()
self.event_size = event_size
mvnd_input_size = 2 # size 2 because loc and scale inputs
self.parametrization = tfpl.VariableLayer([self.event_size, mvnd_input_size],
name='loc', dtype=tf.float32)
self.learnable_mvndiag = tfpl.DistributionLambda(
make_distribution_fn=lambda t: tfd.MultivariateNormalDiag(
loc=t[..., 0],
scale_diag=tf.exp(t[..., 1])
)
)
def call(self, batch_size):
# doesnt matter what we pass in here as tf.VariableLayer ignores input (an error gets thrown if empty though)
params = self.parametrization(None)
tiled = tf.tile(tf.expand_dims(params, 0), [batch_size, 1, 1])
samples = self.learnable_mvndiag(tiled)
return samples
if __name__ == '__main__':
batch_size = 1000
event_size = 2
# set_sizes = [109, 85, 73, 100, 124, 151]
prior = SetPrior(event_size)
distribution = prior(batch_size)
sample = distribution.sample()
plt.scatter(sample[..., 0], sample[..., 1])
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.draw()
plt.waitforbuttonpress()
plt.close()