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四种数据类型转换方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

  

posted @   一字千金  阅读(2328)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示