Tensorflow之数据类型

import tensorflow as tf

#定义常量tf.string是字符串类型
hello = tf.constant('Hello,world!', dtype=tf.string)
sess=tf.Session()
print(sess.run(hello))
#定义常量tf.bool是布尔类型,如果改为‘false’就会报错,显示更改为字符串类型
boolean  = tf.constant(False, dtype=tf.bool)
print(sess.run(boolean))
#定义常量tf.int8,int16,int32,int64类型(有符号整形)
int  = tf.constant(100, dtype=tf.int8)
print(sess.run(int))
#定义常量tf.uint8,tf.uint16(无符号整形)
uint  = tf.constant(100, dtype=tf.uint8)
print(sess.run(uint))
#定义常量tf.float16,tf.float32,tf.float64,tf.double(浮点数)
float  = tf.constant(100, dtype=tf.float16)
print(sess.run(float))
#定义常量tf.complex64,tf.complex128(复数)
complex  = tf.constant(100, dtype=tf.complex64)
print(sess.run(complex))

输出

b'Hello,world!'
False
100
100
100.0
(100+0j)

(1)现在搞清楚int8如果数据越界怎么处理?

import tensorflow as tf


int = tf.constant(1000, dtype=tf.int8)
sess=tf.Session()
print(sess.run(int))

实验结果:

-24

1000转化成二进制为
1111101000
在int8中,取后八个字节11101000第8个字节为1表示负数,负数值为1转化为0,0转化为1然后加1
00010111+1=00011000=24。
如果1000换成488输出结果一样。
(2)搞清楚uint8如果数据越界怎么处理?

import tensorflow as tf


uint = tf.constant(1000, dtype=tf.uint8)
sess=tf.Session()
print(sess.run(uint))

实验结果

232

直接获取后8位转化成10进制为
11101000=232
tensorflow定义张量常量

import tensorflow as tf

ten=tf.constant(-1.0,dtype=tf.float16)
sess=tf.Session()
print(sess.run(ten))
tensor = tf.constant(-1.0, shape=[2, 3])
print(sess.run(tensor))
tensor=tf.constant(['b','a','c'],dtype=tf.string)
print(sess.run(tensor))
tensor=tf.constant([1,'a','c'])
print(sess.run(tensor))

其中shape表示常量的形状,如果不指定,那么为-1.0,如果指定shape=[2,3]然后结果为
[[-1. -1. -1.]
[-1. -1. -1.]]
在定义张量的时候,dtype=tf.string表示张量里面的数值都为string
同一个张量里面的数据类型必须保持一致(跟java中list相同),如果没有一致,则会报错,tensor=tf.constant([1,’a’,’c’])就会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值