108、TensorFlow 類型轉換


# 除了維度之外Tensorflow也有數據類型
# 請參考 tf.DataType
# 一個張量只能有一個類型
# 可以使用tf.cast,將一個張量從一個數據類型轉換到另一個數據類型
# 下面代碼顯示的就是將一個張量的類型從integer類型轉換為float類型
import tensorflow as tf
float_tensor = tf.cast(tf.constant([1, 2, 3]), dtype=tf.float32)
# 當從一個python對象來創建張量的時候可以手動指定數據類型
# 如果你不手動指定數據類型
# TensorFlow 自動地將 Python integer 類型轉換為tf.int32 並且將 python的 float類型轉換為 tf.float32
# 另外TensorFlow使用相同的規則將numpy對象轉換為Array
init = tf.global_variables_initializer()
sess = tf.Session()
print(sess.run(float_tensor))

下面是類型轉換的結果:

2018-02-16 21:44:46.674417: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
[ 1.  2.  3.]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM