2017-02-21 61 views
13

我正在处理tensorflow 0.12并且遇到了铸造问题。 下面的代码片段做了一件怪事:Tensorflow - 从int转换为浮动奇怪行为

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

我得到一个值: 6.86574233e-36

我也使用tf.to_float()tf.saturate_cast尝试。两者都给出了相同的结果。

请帮忙。

+0

我无法在TF 1.0中重现您的问题。 – MikiBear

+0

你是否在gpu上运行Tensorflow,如果是,你使用的是什么gpu卡? Tensorflow编译了哪些cuda版本?有时候,当使用不受cuda版本支持的GPU时,会发生奇怪的事情而没有任何错误消息。 – BlueSun

+0

嗨,我在CPU上运行:英特尔至强E5-2680 – aarbelle

回答

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

您需要声明dtypetf.constanthttps://www.tensorflow.org/api_docs/python/tf/constant

+0

这不能解决问题。当我这样做时,我打赌b = 0似乎无论我使用的类型uint8,uint16,int 32,int64都在铸造后给出不同的结果 – aarbelle

+0

我的代码为'b.eval()'提供'0'? – wong2

+0

是的,但问题比这更深。真正的问题是读取保存为uint8的png图像。在tf.image.decode_png(path,dtype = tf.uint8)之后,我获得了很好的值。但后来当我尝试抛出浮动我得到垃圾 – aarbelle

0

我检查了代码python3与python2为同一tensorflow版本以及代码似乎是在两种情况下正常工作,我得到python2的以下输出

print b.eval() 
1.0 

我会建议检查张量流安装或virtualenv。

0

程序中没有错误。

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

这是TF https://codeenv.com/env/run/gXGpnR/
在线环境中测试你的代码中有运行,使用

  • 点击test_tf.py
  • 添加代码
  • 在左侧CLI,键入ipython test_tf.py