Dear Dustin. I’m so sorry for this huge delay. Please, let me show you some code about what I want. It is based on this post https://pymc-devs.github.io/pymc3/notebooks/dp_mix.html which is a model constructed using pymc3 (outputs 18, 19 and 20): model with stick_breaking methodology. The stick_breaking function is
def stick_breaking(beta):
portion_remaining = tt.concatenate([[1], tt.extra_ops.cumprod(1 - beta)[:-1]])
return beta * portion_remaining
and the model
with pm.Model() as model:
alpha = pm.Gamma('alpha', 1., 1.)
beta = pm.Beta('beta', 1., alpha, shape=K)
w = pm.Deterministic('w', stick_breaking(beta))
#
tau = pm.Gamma('tau', 1., 1., shape=K)
lambda_ = pm.Uniform('lambda', 0, 5, shape=K)
mu = pm.Normal('mu', 0, tau=lambda_ * tau, shape=K)
obs = pm.NormalMixture('obs', w, mu, tau=lambda_ * tau,
observed=old_faithful_df.std_waiting.values)
I want to rewrite this code to Edward and plot the posterior expected mixture weight of each component (output 23)
fig, ax = plt.subplots(figsize=(8, 6))
plot_w = np.arange(K) + 1
ax.bar(plot_w - 0.5, trace['w'].mean(axis=0), width=1., lw=0);
ax.set_xlim(0.5, K);
ax.set_xlabel('Component');
ax.set_ylabel('Posterior expected mixture weight');
I’ve transformed previous code to Edward by using your example in https://github.com/blei-lab/edward/blob/master/examples/mixture_gaussian_mh.py changing pi ~ Dirichlet
by the following
# pi = Dirichlet(concentration=tf.constant([1.0] * K))
alpha = Gamma(concentration=2.0, rate=2.0, name = "alpha")
beta = Beta(concentration1=1.0, concentration0=alpha, sample_shape = K, name = "beta")
pi = tf.concat([tf.reshape(beta[0],[1]), tf.reshape(tf.multiply(beta[1:],tf.cumprod(1 - beta[:-1])), [K-1])], 0)
mu = Normal(loc=tf.zeros([K, D]), scale=tf.ones([K, D]))
sigma = InverseGamma(concentration=tf.ones([K, D]), rate=tf.ones([K, D]))
c = Categorical(logits=tf.tile(tf.reshape(ed.logit(pi), [1, K]), [N, 1]))
x = Normal(loc=tf.gather(mu, c), scale=tf.gather(sigma, c))
Then the inference is
T = 5000
# qpi = Empirical(params=tf.Variable(tf.ones([T, K]) / K))
qalpha = Empirical(params=tf.Variable(tf.ones([T])))
qbeta = Empirical(params=tf.Variable(tf.ones([T,K])))
qmu = Empirical(params=tf.Variable(tf.zeros([T, K, D])))
qsigma = Empirical(params=tf.Variable(tf.ones([T, K, D])))
qc = Empirical(params=tf.Variable(tf.zeros([T, N], dtype=tf.int32)))
# gpi = Dirichlet(concentration=tf.constant([1.4, 1.6]))
galpha = Gamma(concentration=1.0, rate=1.0)
gbeta = Beta(concentration1=1.0, concentration0=1.0, sample_shape=K)
gmu = Normal(loc=tf.constant([[1.0, 1.0], [-1.0, -1.0]]),
scale=tf.constant([[0.5, 0.5], [0.5, 0.5]]))
gsigma = InverseGamma(concentration=tf.constant([[1.1, 1.1], [1.1, 1.1]]),
rate=tf.constant([[1.0, 1.0], [1.0, 1.0]]))
gc = Categorical(logits=tf.zeros([N, K]))
inference = ed.MetropolisHastings(
latent_vars={alpha:qalpha, beta:qbeta, mu: qmu, sigma: qsigma, c: qc},
proposal_vars={alpha:galpha, beta:gbeta, mu: gmu, sigma: gsigma, c: gc},
data={x: x_data})
but I have the following error
[angel:~/ownCloud … local-density/Edward] [tensorflow] 2s 1 $ python GausianMixtures2DMetropolis.py
2017-05-26 10:06:18.255928: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 10:06:18.255951: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 10:06:18.255967: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 10:06:18.255973: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 10:06:18.255995: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Traceback (most recent call last):
File "GausianMixtures2DMetropolis.py", line 282, in <module>
info_dict = inference.update()
File "/home/angel/src/edward/edward/inferences/monte_carlo.py", line 130, in update
_, accept_rate = sess.run([self.train, self.n_accept_over_t], feed_dict)
File "/home/angel/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/home/angel/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 982, in _run
feed_dict_string, options, run_metadata)
File "/home/angel/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1032, in _do_run
target_list, options, run_metadata)
File "/home/angel/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [] [Condition x > 0 did not hold element-wise: x = ] [concat:0] [1 0]
[[Node: inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_STRING, DT_FLOAT], summarize=3, _device="/job:localhost/replica:0/task:0/cpu:0"](inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/Switch, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/data_0, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/data_1, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/data_2, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/Switch_1)]]
Caused by op u'inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert', defined at:
File "GausianMixtures2DMetropolis.py", line 276, in <module>
inference.initialize()
File "/home/angel/src/edward/edward/inferences/monte_carlo.py", line 98, in initialize
self.train = self.build_update()
File "/home/angel/src/edward/edward/inferences/metropolis_hastings.py", line 119, in build_update
zold = copy(z, dict_swap_old, scope=scope_old)
File "/home/angel/src/edward/edward/util/random_variables.py", line 228, in copy
kwargs[key] = copy_default(value, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 84, in copy_default
x = copy(x, *args, **kwargs)
File "/home/angel/src/edward/edward/util/random_variables.py", line 244, in copy
new_op = copy(op, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 308, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 244, in copy
new_op = copy(op, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 308, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 244, in copy
new_op = copy(op, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 308, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 244, in copy
new_op = copy(op, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 308, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 244, in copy
new_op = copy(op, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 298, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 308, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 244, in copy
new_op = copy(op, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 298, in copy
elem = copy(x, dict_swap, scope, True, copy_q)
File "/home/angel/src/edward/edward/util/random_variables.py", line 290, in copy
op_def)
File "/home/angel/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): assertion failed: [] [Condition x > 0 did not hold element-wise: x = ] [concat:0] [1 0]
[[Node: inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_STRING, DT_FLOAT], summarize=3, _device="/job:localhost/replica:0/task:0/cpu:0"](inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/Switch, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/data_0, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/data_1, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/data_2, inference_140093265818960/old/assert_positive/assert_less/Assert/AssertGuard/Assert/Switch_1)]]