Use context manager for automatic casting in tensorflow

Viewed 15

To not leave computationally expensive casting undetected tensorflow throws an error each time you try to use an operation on tensors with incompatible types. The following code throws an InvalidArgumentError:

tf.constant(2) + tf.constant(3.) 

To fix this every tensor needs to be casted:

tf.cast(tf.constant(2), tf.float32) + tf.constant(3.)

This is really annoying and ugly. For instance in numpy they just do it automatically. Is there a possibility to turn it off with a context manager to allow numpy like syntax? Just like this:

with CastingAutomatically:
    t = tf.constant(2) + tf.constant(3.)
0 Answers
Related