[英]Tensorflow - casting from int to float strange behavior
我正在研究tensorflow 0.12,我遇到了鑄造問題。 以下代碼片段做了一件奇怪的事情:
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
。 兩者都給出了相同的結果。
請幫忙。
sess = tf.InteractiveSession()
a = tf.constant(1, tf.int64) <--------
b = tf.cast(a, tf.float32)
print b.eval() # 1.0
您需要聲明dtype
為您tf.constant
: https://www.tensorflow.org/api_docs/python/tf/constant
我檢查了python3和python2中的代碼相同的tensorflow版本以及代碼似乎正常工作,因為在兩種情況下我得到了python2的以下輸出
print b.eval()
1.0
我建議檢查tensorflow安裝或virtualenv。
你的程序沒有錯誤。
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/
測試你的代碼在那里運行,使用
由於我發現這仍然受到一些關注,我應該提到較新版本的tensorflow沒有顯示這種行為,我建議使用tensorflow版本1.13或更高版本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.