Assertion Error running Dustin's Coin Flipping Toy Example


#1

Hello, I am new to Edward. I tried running Dustin’s sample coin flipping example, and keep running into an Assertion Error with ed.complete_conditional§. I also run into the same error in completely separate code. What am I doing wrong? Thanks for any guidance. Ikib

from future import absolute_import
from future import division
from future import print_function

import edward as ed
import numpy as np
import six
import tensorflow as tf

from edward.models import Bernoulli, Beta
tf.reset_default_graph()
ed.set_seed(42)

def main(_):

DATA

x_data = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1])

MODEL

p = Beta(1.0, 1.0)
x = Bernoulli(probs=p, sample_shape=10)

COMPLETE CONDITIONAL

p_cond = ed.complete_conditional§

sess = ed.get_session()

print(‘p(probs | x) type:’, p_cond.parameters[‘name’])
param_vals = sess.run({key: val for
key, val in six.iteritems(p_cond.parameters)
if isinstance(val, tf.Tensor)}, {x: x_data})
print(‘parameters:’)
for key, val in six.iteritems(param_vals):
print(’%s:\t%.3f’ % (key, val))

if name == “main”:
tf.app.run()

AssertionError Traceback (most recent call last)
in ()
23
24 if name == “main”:
—> 25 tf.app.run()

c:\users\bikim\appdata\local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py in run(main, argv)
124 # Call the main function, passing through any arguments
125 # to the final program.
–> 126 _sys.exit(main(argv))
127
128

in main(_)
10
11 # COMPLETE CONDITIONAL
—> 12 p_cond = ed.complete_conditional§
13
14 sess = ed.get_session()

c:\users\bikim\appdata\local\conda\conda\envs\tensorflow\lib\site-packages\edward\inferences\conjugacy\conjugacy.py in complete_conditional(rv, cond_set)
156
157 scope_name = scope + str(time.time()) # ensure unique scope when copying
–> 158 log_joint_copy = copy(log_joint, swap_dict, scope=scope_name + ‘swap’)
159 nat_params = tf.gradients(log_joint_copy, s_stat_placeholders)
160

c:\users\bikim\appdata\local\conda\conda\envs\tensorflow\lib\site-packages\edward\util\random_variables.py in copy(org_instance, dict_swap, scope, replace_itself, copy_q, copy_parent_rvs)
268 # op. Therefore copy the op itself.
269 op = tensor.op
–> 270 new_op = copy(op, dict_swap, scope, True, copy_q, False)
271
272 output_index = op.outputs.index(tensor)

c:\users\bikim\appdata\local\conda\conda\envs\tensorflow\lib\site-packages\edward\util\random_variables.py in copy(org_instance, dict_swap, scope, replace_itself, copy_q, copy_parent_rvs)
314 [], # input types; will add them afterwards
315 original_op,
–> 316 op_def)
317
318 # advertise op early to break recursions

c:\users\bikim\appdata\local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py in init(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1730 # Refactor so we don’t have to do this here.
1731 grouped_inputs = self._reconstruct_sequence_inputs(
-> 1732 op_def, inputs, node_def.attr)
1733 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
1734 control_input_ops)

c:\users\bikim\appdata\local\conda\conda\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py in _reconstruct_sequence_inputs(self, op_def, inputs, attrs)
1804 i += input_len
1805
-> 1806 assert i == len(inputs)
1807 return grouped_inputs
1808

AssertionError:


#2

Hi, I encountered this bug today aswell and was able to bypass it in tensorflow 1.8.
The error is actually not specific to the Coin Flipping example, indeed you get this error when you try to use edward’s copy function in tensorflow>=1.8.

There are currently at least 2 tensorflow-1.8 related bugs in edward, which are related to tensorflow switching to the C-API for graphs by default since tf 1.8.
e.g. tf.Session().graph._c_graph is None in tf 1.7 but not in 1.8.
The first bug and a fix is described here.

The second bug, which you are seeing, is because edwards’ copy function creates a tf.Operation with the argument inputs=[] with the comment that the inputs are added later.
However, using the C-API in the backend for the graph, tf.Operation apparently requires non-empty inputs.
In tensorflow 1.8, you may bypass this bug by setting the environment variable TF_C_API_GRAPH_CONSTRUCTION=0. I don’t think it works for current tf 1.9 though, as the constructor of the Operation class changed and does not check for self.graph._c_graph anymore.