tensorflow从入门到放弃再到精通(4.1):TensorFlow 基础语法以及数据类型-数据类型以及数值精度

4.1 数据类型

4.1.1 数值类型

  • 标量(Scalar)。单个的实数,如 1.2, 3.4 等,维度(Dimension)数为 0,shape 为[]。

  • 向量(Vector)。𝑛个实数的有序集合,通过中括号包裹,如[1.2],[1.2,3.4]等,维度数 为 1,长度不定,shape 为[𝑛]。

  • 矩阵(Matrix)。𝑛行𝑚列实数的有序集合,如[[1,2],[3,4]],维度数为 2,每个维度上的长度不定,shape 为[𝑛, 𝑚]。

  • 张量(Tensor)。所有维度数dim > 2的数组统称为张量。张量的每个维度也作轴(Axis),一般维度代表了具体的物理含义,比如 Shape 为[2,32,32,3]的张量表示图片数据,其中 2 代表了 2 张图片,32 代表了高、宽均为 32,3 代表了 RGB 共 3 个 通道。

标量在 TensorFlow 是如何创建

a = 1.2 # python语言方式创建标量
aa = tf.constant(1.2) # TF方式创建标量
type(a), type(aa), tf.is_tensor(aa) #查看输出的类型
#(float, tensorflow.python.framework.ops.EagerTensor, True)

#如果使用print
x = tf.constant([1,2.,3.3])
print(x)
#<tf.Tensor: id=165, shape=(3,), dtype=float32, numpy=array([1. , 2. , 3.3],dtype=float32)>

其中 id 是 TensorFlow 中内部索引对象的编号,shape 表示张量的形状,dtype 表示张量的数 值精度,张量 numpy()方法可以返回 Numpy.array 类型的数据。

创建一个向量

与标量不同,向量的定义须通过 List 容器传给 tf.constant()函数。

 a = tf.constant([1.2]) # 创建一个元素的向量

 a = tf.constant([1,2, 3.]) # 创建3个元素的向量

创建一个矩阵

 a = tf.constant([[1,2],[3,4]]) # 创建 2 行 2 列的矩阵

创建一个张量

a = tf.constant([[[1,2],[3,4]],[[5,6],[7,8]]]) # 创建3维张量

4.1.2 字符串类型

TensorFlow 还支持字符串(String) 类型的数据,例如在 表示图片数据时,可以先记录图片的路径字符串,再通过预处理函数根据路径读取图片张 量。通过传入字符串对象即可创建字符串类型的张量。

a = tf.constant('Hello, Deep Learning.') # 创建字符串

#在 tf.strings 模块中,提供了常见的字符串类型的工具函数,如小写化 lower()、拼接 join()、长度 length()、切分 split()等
tf.strings.lower(a) # 小写化字符串

4.1.3 布尔类型

为了方便表达比较运算操作的结果,TensorFlow 还支持布尔类型(Boolean,简称 bool) 的张量。布尔类型的张量只需要传入 Python 语言的布尔类型数据,转换成 TensorFlow 内 部布尔型。

a = tf.constant(True) # 创建布尔类型标量

#同样地,创建布尔类型的向量
a = tf.constant([True, False]) # 创建布尔类型向量

#TensorFlow 的布尔类型和 Python 语言的布尔类型并不等价
a = tf.constant(True) # 创建TF布尔张量
a is True # TF布尔类型张量与python布尔类型比较,结果是False
a == True # 仅数值比较 ,结果是False

4.2 数值精度

对于数值类型的张量,可以保存为不同字节长度的精度,例如浮点型3.14可以保存为16 位(Bit)长度,也可以保存为 32 位甚至 64 位的精度。精度越高,占用的内存空间也就越大。

常见的精度类型有tf.int16、tf.int32、tf.int64、tf.float16、tf.float32,、tf.float64等

#创建指定精度的张量
tf.constant(123456789, dtype=tf.int16)
tf.constant(123456789, dtype=tf.int32)

#结果,因为数值太大,使用精度低的不能全部保存
#<tf.Tensor: id=33, shape=(), dtype=int16, numpy=-13035>
#<tf.Tensor: id=35, shape=(), dtype=int32, numpy=123456789> 

可以看到,保存精度过低时,数据 123456789 发生了溢出,得到了错误的结果,一般使用 tf.int32、tf.int64 精度。对于浮点型,高精度就能保存更精确地数据,例如使用tf.float32保存π时,实际保存为3.1415927

import numpy as np
np.pi #从numpy中导入pi常量
tf.constant(np.pi, dtype=tf.float32)

#结果
#<tf.Tensor: id=29, shape=(), dtype=float32, numpy=3.1415927>


#########使用tf.float64精度


tf.constant(np.pi, dtype=tf.float64)

#结果
#<tf.Tensor: id=31, shape=(), dtype=float64, numpy=3.141592653589793>

对大部分场景来说使用tf.int32或者tf.float32保存精度,需要使用强化算法的时候使用tf.int64 tf.float64精度保存

4.2.1 读取精度

通过访问张量的 dtype 成员属性可以判断张量的保存精度

# 读取原有张量的数据精度
print('before:',a.dtype)    

# 如果精度不符合要求,则进行转换 
if a.dtype != tf.float32: 
     # tf.cast 函数可以完成精度转换 
     a = tf.cast(a,tf.float32)

# 打印转换后的精度
print('after :',a.dtype)


#结果
#before: <dtype: 'float16'> 
#after : <dtype: 'float32'>

4.2.2 精度转换

系统的每个模块使用的数据类型、数值精度可能各不相同,需要使用tf.cast来转换

# 创建 tf.float16 低精度张量
a = tf.constant(np.pi, dtype=tf.float16)
# 转化为高精度
tf.cast(a, tf.double)


###注意在高精度转低精度的时候可能会出现溢出的情况
a = tf.constant(123456789, dtype=tf.int32) 
tf.cast(a, tf.float16)
#结果
#<tf.Tensor: id=38, shape=(), dtype=int16, numpy=-13035> 
#因为精度超了,所以出现了越界的问题


#布尔类型与整型之间相互转换也是合法的
a = tf.constant([True, False])
tf.cast(a, tf.int32)
#结果
#<tf.Tensor: id=48, shape=(2,), dtype=int32, numpy=array([1, 0])>


a = tf.constant([-1, 0, 1, 2]
tf.cast(a, tf.bool)
#结果
<tf.Tensor: id=51, shape=(4,), dtype=bool, numpy=array([ True, False,  True,  True])>


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值