Tensorflow tf.cond encountering error: TypeError: Failed to convert object of type to Tensor

David Parks Source

Below is a minimal test case in which I create a variable v which I want to initialize to a value of 777 (for the simplified test case).

Note: I can't initialize v with a normal initializer because it depends on a normalization constant computed across all variables (some of which aren't yet created at the time v is created).

My solution (below) is to create a boolean variable full_init_cond that I set to True once I've run some initialization/assign OPs once and use tf.cond to ensure they only run the one time.

import tensorflow as tf

v = tf.Variable(0, trainable=False)
full_init_cond = tf.Variable(False, trainable=False, dtype=tf.bool)

with tf.control_dependencies([v]):
  tf.cond(
    full_init_cond,
    true_fn=lambda: [tf.no_op],
    false_fn=lambda: [tf.assign(v, 777), tf.assign(full_init_cond, True)]
  )

I'm receiving the following error on the tf.cond line:

TypeError: Failed to convert object of type <class 'function'> to Tensor. Contents: <function no_op at 0x7f39abf51400>. Consider casting elements to a supported type.

I'm not making much sense of this error.


Update:

To my surprise this simple test seems to produce a valid tensor:

tf.cond(full_init_cond, tf.no_op, tf.no_op)

Thought this one fails:

tf.cond(full_init_cond, lambda: tf.no_op, lambda: tf.no_op)

My confusion continues... Tensorflow version 1.5 by the way.

pythontensorflow

Answers

answered 6 months ago David Parks #1

It turns out that it didn't like the tf.no_op statement.

tf.cond requires that you return the same number and type of operations for either true|false conditions. So instead of tf.no_op I've replaced it with the tf.identity operation as an appropriate stand-in for no_op:

import tensorflow as tf

v = tf.Variable(0, trainable=False)
full_init_cond = tf.Variable(False, trainable=False, dtype=tf.bool)

with tf.control_dependencies([v]):
  x = tf.cond(
    full_init_cond,
    true_fn=lambda: [tf.identity(v), tf.identity(full_init_cond)],
    false_fn=lambda: [tf.assign(v, 777), tf.assign(full_init_cond, True)]
  )

print(x)

comments powered by Disqus