2017-02-21 63 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