tensorflow: If a branch contains summaries, tf.cond doesn't support Operations as branches

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS Mojave (10.14.2)
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): v1.12.0-rc2-3-ga6d8ffae09 1.12.0
  • Python version: 3.6.6 (Anaconda)

Describe the current behavior The code below produces a TypeError. The bug seems to rely on a combination of:

  1. tf.cond
  2. a tf.contrib.summary in one of the branches
  3. tf.group

Describe the expected behavior The code should run successfully.

Code to reproduce the issue

import tensorflow as tf

writer = tf.contrib.summary.create_file_writer('tb')
with writer.as_default(), tf.contrib.summary.always_record_summaries():
    op = tf.cond(
        tf.random.normal([]) >= 0,
        lambda: tf.group(tf.contrib.summary.scalar('loss', 0.2)),
        tf.no_op)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(tf.contrib.summary.summary_writer_initializer_op())
sess.run(op)

Other info / logs Here’s the traceback:

Traceback (most recent call last):
  File "repro.py", line 8, in <module>
    tf.no_op)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2086, in cond
    orig_res_t, res_t = context_t.BuildCondBranch(true_fn)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 1941, in BuildCondBranch
    original_result)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 381, in map_structure
    structure[0], [func(*x) for x in entries])
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 381, in <listcomp>
    structure[0], [func(*x) for x in entries])
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 81, in identity
    return gen_array_ops.identity(input, name=name)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3454, in identity
    "Identity", input=input, name=name)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
    raise err
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1146, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/Users/dmz/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 6168, in _operation_conversion_error
    name, as_ref))
TypeError: Can't convert Operation 'cond/group_deps' to Tensor (target dtype=None, name='input', as_ref=False)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@lsgrep fantastic! Thank you!