-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvariance_tests.py
151 lines (103 loc) · 4.28 KB
/
variance_tests.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
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
from mv_kumaraswamy_sampler import KumaraswamyStickBreakingProcess
# set random seeds
np.random.seed(123)
tf.set_random_seed(123)
def irg_variance_test(x, alphas, alpha_prior, N_trials):
# get useful numbers
K = x.shape[0]
# compute optimal posterior parameters
alpha_star = alpha_prior + x
print('alpha max = {:.2f}'.format(np.max(alpha_star)))
# initialize gradients
gradients = np.zeros([len(alphas), N_trials, K])
# reset graph with new session
tf.reset_default_graph()
with tf.Session() as sess:
# declare training variable
alpha_ph = tf.placeholder(tf.float32, [1, K])
# set prior as a TF constant
alpha_prior = tf.constant(alpha_prior, dtype=tf.float32)
# declare sampler
sampler = tf.distributions.Dirichlet(alpha_ph)
pi = sampler.sample()
# compute the expected log likelihood
ll = tf.reduce_sum(x * tf.log(pi))
# compute the ELBO
elbo = ll - sampler.kl_divergence(tf.distributions.Dirichlet(alpha_prior))
# compute gradient
grad = tf.gradients(xs=[alpha_ph], ys=elbo)
# loop over the alphas
for i in range(len(alphas)):
# set alpha for this test
alpha = alpha_star * np.ones([1, K])
alpha[0, 0] = alphas[i]
# compute the gradient over the specified number of trials
for j in range(N_trials):
gradients[i, j] = sess.run(grad, feed_dict={alpha_ph: alpha})[0]
# print update
a_per = 100 * (i + 1) / len(alphas)
n_per = 100 * (j + 1) / N_trials
update_str = 'Alphas done: {:.2f}%, Trials done: {:.2f}%'.format(a_per, n_per)
print('\r' + update_str, end='')
print('')
# return the gradients
return gradients
def mvk_variance_test(x, alphas, alpha_prior, N_trials, mc_samples=10):
# get useful numbers
K = x.shape[0]
# compute optimal posterior parameters
alpha_star = alpha_prior + x
print('alpha max = {:.2f}'.format(np.max(alpha_star)))
# initialize gradients
gradients = np.zeros([len(alphas), N_trials, K])
# reset graph with new session
tf.reset_default_graph()
cfg = tf.ConfigProto()
cfg.gpu_options.allow_growth = True
with tf.Session(config=cfg) as sess:
# declare training variable
alpha_ph = tf.placeholder(tf.float32, [1, K])
# duplicate it for MC-sampling
alpha_mc = tf.tile(alpha_ph, [mc_samples, 1])
# set prior as a TF constant
alpha_prior = tf.constant(alpha_prior, dtype=tf.float32)
# declare sampler
sampler = KumaraswamyStickBreakingProcess(dkl_taylor_order=5)
pi, i_perm = sampler.sample(alpha_mc)
# compute the expected log likelihood
ll = tf.reduce_mean(tf.reduce_sum(x * tf.log(pi + 1e-6), axis=1))
# compute the ELBO
elbo = ll - tf.reduce_mean(sampler.kl_divergence(alpha=alpha_mc, alpha_prior=alpha_prior, i_perm=i_perm))
# compute gradient
grad = tf.gradients(xs=[alpha_ph], ys=elbo)
# loop over the alphas
for i in range(len(alphas)):
# set alpha for this test
alpha = alpha_star * np.ones([1, K])
alpha[0, 0] = alphas[i]
# compute the gradient over the specified number of trials
for j in range(N_trials):
gradients[i, j] = sess.run(grad, feed_dict={alpha_ph: alpha})[0]
# print update
a_per = 100 * (i + 1) / len(alphas)
n_per = 100 * (j + 1) / N_trials
update_str = 'Alphas done: {:.2f}%, Trials done: {:.2f}%'.format(a_per, n_per)
print('\r' + update_str, end='')
print('')
# return the gradients
return gradients
if __name__ == '__main__':
K = 100
N = 100
p_true = np.random.dirichlet(np.ones(K))
x = np.random.multinomial(n=N, pvals=p_true)
alphas = np.linspace(1.01, 3.0, 100)
grads = mvk_variance_test(x=x, alphas=alphas, alpha_prior=np.ones(K), N_trials=100)
# take the variance across samples
grad_var = np.var(grads, axis=1)
plt.figure()
plt.plot(alphas, grad_var[:, 0])
plt.show()