Thursday, March 2, 2017

Tensorflow - casting from int to float strange behavior

Leave a Comment

I am working on tensorflow 0.12 and am having problem with casting. The following snippet of code does a strange thing:

sess = tf.InteractiveSession() a = tf.constant(1) b = tf.cast(a, tf.float32) print b.eval() 

I get a value: 6.86574233e-36

I also tried using tf.to_float() and tf.saturate_cast both gave the same result. Please help

2 Answers

Answers 1

sess = tf.InteractiveSession() a = tf.constant(1, tf.int64)  <-------- b = tf.cast(a, tf.float32) print b.eval()  # 1.0 

You need to declare the dtype for your tf.constant: https://www.tensorflow.org/api_docs/python/tf/constant

Answers 2

I checked the code in python3 and python2 for the same tensorflow version as well the code seems to be working correctly as in both the cases I got the following output for python2

print b.eval() 1.0 

I would suggest checking the tensorflow installation or the virtualenv.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment