Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow - casting from int to float strange behavior

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.

like image 960
aarbelle Avatar asked Feb 21 '17 10:02

aarbelle


1 Answers

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

like image 105
wong2 Avatar answered Oct 02 '22 20:10

wong2