首页 > 技术文章 > 2.1 tensorflow2.x数据类型和四种转换方法

bclshuai 2020-10-21 19:23 原文

自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

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

  

推荐阅读