Simple Hierarchical Distribution

I’ve been trying to use KLqp to infer the one of the parameters of a Beta distribution (hierarchical because Beta -> Bernoulli). It doesn’t fail when iterations is small, (e.g. < 500), but when I raise it to 1000 it fails.

This is my code:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import edward as ed
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

from edward.models import Normal, Empirical, Bernoulli, Uniform, Beta

plt.style.use('ggplot')
ed.set_seed(42)
print("tensorflow version: " + str(tf.__version__))
print("edward version: " + str(ed.__version__))

# Sample data points
N = 1000  # number of data points

with tf.name_scope("sampler"):
    alpha = 2.0
    beta = 2.0
    theta = Beta(alpha, beta)
    x = Bernoulli(tf.ones(N) * theta)
    
with tf.Session() as s:
    s.run([])
    x_samples = x.sample().eval()
    x_samples = tf.cast(x_samples, tf.int32)

with tf.name_scope('model'):
    alpha = Uniform(low=[0.0], high=[1.0], name="e_bernoulli/p")
    beta = 2.0
    theta = Beta(alpha, beta)
    x = Bernoulli(tf.ones(N) * theta)

with tf.name_scope('posterior'):
    alpha_pos = Beta(tf.Variable([2.0]), tf.Variable([2.0]))

inference = ed.KLqp({alpha: alpha_pos}, data={x: x_samples})
inference.run(n_samples=50, n_iter=1000, logdir='log/n_samples_5')    

sess = ed.get_session()
prob_estimates = []
for i in range(1000):
    prob_estimates.append(sess.run(alpha_pos))
print(np.mean(prob_estimates))

And my output is

bash4.2: ./alarm.py
tensorflow version: 1.3.0
edward version: 1.3.4
2017-10-12 04:55:12.998399: 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-10-12 04:55:12.998424: 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-10-12 04:55:12.998432: 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-10-12 04:55:12.998438: 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.
(1000,)
 960/1000 [ 96%] ████████████████████████████   ETA: 1s | Loss: 687.553Traceback (most recent call last):
  File "./alarm.py", line 45, in <module>
    inference.run(n_samples=50, n_iter=1000, logdir='log/n_samples_5')    
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/inference.py", line 144, in run
    info_dict = self.update()
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/variational_inference.py", line 161, in update
    summary = sess.run(self.summarize, feed_dict)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
    options, run_metadata)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Nan in summary histogram for: gradient/posterior/Variable_1/0
	 [[Node: gradient/posterior/Variable_1/0 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](gradient/posterior/Variable_1/0/tag, gradients_1/AddN_2)]]

Caused by op u'gradient/posterior/Variable_1/0', defined at:
  File "./alarm.py", line 45, in <module>
    inference.run(n_samples=50, n_iter=1000, logdir='log/n_samples_5')
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/inference.py", line 123, in run
    self.initialize(*args, **kwargs)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/klqp.py", line 107, in initialize
    return super(KLqp, self).initialize(*args, **kwargs)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/variational_inference.py", line 76, in initialize
    grad, collections=[self._summary_key])
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/summary/summary.py", line 192, in histogram
    tag=tag, values=values, name=scope)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/gen_logging_ops.py", line 129, in _histogram_summary
    name=name)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Nan in summary histogram for: gradient/posterior/Variable_1/0
	 [[Node: gradient/posterior/Variable_1/0 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](gradient/posterior/Variable_1/0/tag, gradients_1/AddN_2)]]

Could anyone point out what I’m doing wrong? It would be greatly appreciated!

In the line

alpha_pos = Beta(tf.Variable([2.0]), tf.Variable([2.0]))

you wrote free parameters initialized at 2.0 for the concentration parameters of Beta. During optimization, they are not constrained to be non-negative; I recommend softplus on the tf.Variables.

Also: note the default arg to Bernoulli is the logit probabilities and not the normalized probabilities. Here you should write x = Bernoulli(probs=tf.ones(N) * theta).

Thanks!

I made the changes

x = Bernoulli(probs=tf.ones(N) * theta)

and

alpha_pos = Beta(tf.nn.softplus(tf.Variable([2.0])), tf.nn.softplus(tf.Variable([2.0])))

However, I’m still getting a similar error.

tensorflow version: 1.3.0
edward version: 1.3.4
2017-10-12 11:06:48.550191: 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-10-12 11:06:48.550213: 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-10-12 11:06:48.550219: 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-10-12 11:06:48.550225: 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.
 140/1000 [ 14%] ████                           ETA: 174s | Loss: 975.839Traceback (most recent call last):
  File "./alarm.py", line 42, in <module>
    inference.run(n_samples=50, n_iter=1000, logdir='log/n_samples_5')    
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/inference.py", line 144, in run
    info_dict = self.update()
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/variational_inference.py", line 161, in update
    summary = sess.run(self.summarize, feed_dict)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
    options, run_metadata)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Nan in summary histogram for: gradient/posterior/Variable_1/0
	 [[Node: gradient/posterior/Variable_1/0 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](gradient/posterior/Variable_1/0/tag, gradients/inference/sample_49/posterior/Softplus_1_grad/SoftplusGrad)]]

Caused by op u'gradient/posterior/Variable_1/0', defined at:
  File "./alarm.py", line 42, in <module>
    inference.run(n_samples=50, n_iter=1000, logdir='log/n_samples_5')
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/inference.py", line 123, in run
    self.initialize(*args, **kwargs)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/klqp.py", line 107, in initialize
    return super(KLqp, self).initialize(*args, **kwargs)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/edward/src/edward/edward/inferences/variational_inference.py", line 76, in initialize
    grad, collections=[self._summary_key])
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/summary/summary.py", line 192, in histogram
    tag=tag, values=values, name=scope)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/gen_logging_ops.py", line 129, in _histogram_summary
    name=name)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/qiyatung/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Nan in summary histogram for: gradient/posterior/Variable_1/0
	 [[Node: gradient/posterior/Variable_1/0 = HistogramSummary[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](gradient/posterior/Variable_1/0/tag, gradients/inference/sample_49/posterior/Softplus_1_grad/SoftplusGrad)]]