1. 程式人生 > 實用技巧 >2.1 tensorflow2.x資料型別和四種轉換方法

2.1 tensorflow2.x資料型別和四種轉換方法

自己開發了一個股票智慧分析軟體,功能很強大,需要的點選下面的連結獲取:

https://www.cnblogs.com/bclshuai/p/11380657.html

1.1.1 常用資料型別

整型有符號

tf.int8:8位整數。 tf.int16:16位整數。 tf.int32:32位整數。 tf.int64:64位整數。

整型無符號

tf.uint8:8位無符號整數。 tf.uint16:16位無符號整數。tf.uint32,tf.uint64

浮點型

tf.float16:16位浮點數。 tf.float32:32位浮點數。 tf.float64:64位浮點數。 tf.double:等同於tf.float64。

字串

tf.string:字串

布林

tf.bool:布林型。True和False

負數

tf.complex64:64位複數。 tf.complex128:128位複數。

1.1.2 tensorflow四種資料型別轉換方法

import tensorflow as tf
ss = tf.compat.v1.Session()
#保證sess.run()能夠正常執行,2.0不支援1.0的session
tf.compat.v1.disable_eager_execution()
a=10
print(a)
b=tf.constant(a,dtype=tf.float64)
#(1)constant指定資料型別轉變
print(b)#Tensor("Const:0", shape=(), dtype=float64)
print(ss.run(b))#10.0
#(2)通過to_int32_tf.to_int64, tf.to_float, tf.to_double轉換
c=tf.compat.v1.to_int64(b);
print(c)#Tensor("ToInt64:0", shape=(), dtype=int64)
print(ss.run(c))#10
#(3)通過cast函式去轉換
d=tf.cast(a, tf.int8)#10,如果是65536則溢位,輸出結果為0
print(ss.run(d))
#(4)飽和轉換(大數字轉小型別,安全轉換)
e=tf.saturate_cast(65536,dtype=tf.int8);
print(ss.run(e))#輸出127