tensorflow2学习第一天

第一天学习数据类型

数据类型

import tensorflow as tf
tf.constant(1)
<tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>

就是一个整数类型的1

tf.constant(1.)
<tf.Tensor: id=1, shape=(), dtype=float32, numpy=1.0>

数值类型的学习

scalar是一个单实数,维度为0,shape[]

a=1.2
tf.__version__
'2.0.0-rc0'

标量一个数字
向量一个数组
矩阵多维数组
对于tensorflow2来说,tensor为其格式主要对与数据的维度大于2

aa = tf.constant(1.2) #创建一个标量
type(a)
float
type(aa)
tensorflow.python.framework.ops.EagerTensor
tf.is_tensor(aa)
True
x = tf.constant([1,2.,3])
x
<tf.Tensor: id=3, shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>

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

x.numpy()
array([1., 2., 3.], dtype=float32)

以上的张量定义可以直接定义

与标量不同,向量的定义须通过 List 容器传给 tf.constant()函数。例如,创建一个元素
的向量:

b = tf.constant([2.3])
b,b.shape
(<tf.Tensor: id=4, shape=(1,), dtype=float32, numpy=array([2.3], dtype=float32)>,
 TensorShape([1]))
b = tf.constant([1,2,3.])
b,b.shape
(<tf.Tensor: id=5, shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>,
 TensorShape([3]))

创建两行两列向量

bb = tf.constant([[1,2.],[3,4]])
print(bb)
tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)
bb,bb.shape
(<tf.Tensor: id=6, shape=(2, 2), dtype=float32, numpy=
 array([[1., 2.],
        [3., 4.]], dtype=float32)>,
 TensorShape([2, 2]))
bb
<tf.Tensor: id=6, shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
创建三维张量
bc = tf.constant([[1,2],[3,4],[5,6]])
bc
<tf.Tensor: id=8, shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4],
       [5, 6]])>
bb = tf.constant([[[1,2],[3,4]],[[5,6],[7,8]]])

#这是二维向量,代表三行两列,
#小技巧  ::: 可以先写方括号,确定类别,然后再向里面填数据
bb
<tf.Tensor: id=10, shape=(2, 2, 2), dtype=int32, numpy=
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])>

代表三维向量,两行两列,每个数据中又有是三个值,可以这么理解,或者理解为数学中的三维数组

字符串的类型

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

a = tf.constant('HELLO cangxioashu') #创建字符串
a
<tf.Tensor: id=13, shape=(), dtype=string, numpy=b'HELLO cangxioashu'>

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

tf.strings.lower(a)
<tf.Tensor: id=14, shape=(), dtype=string, numpy=b'hello cangxioashu'>

字符串使用较少

布尔类型

a = tf.constant(True)
a
<tf.Tensor: id=15, shape=(), dtype=bool, numpy=True>
a = tf.constant([True,False])  #创建bool类型的数据
a
<tf.Tensor: id=16, shape=(2,), dtype=bool, numpy=array([ True, False])>

tensorflow的bool和pyhon不能通用

a = tf.constant([True])
a is True  #对象不等价
False
a == True  #数值的结果
<tf.Tensor: id=21, shape=(1,), dtype=bool, numpy=array([ True])>
a
<tf.Tensor: id=17, shape=(1,), dtype=bool, numpy=array([ True])>

数值的精度

对于数值类型的张量,可以保存为不同字节长度的精度,如浮点数 3.14 既可以保存为
16 位(Bit)长度,也可以保存为 32 位甚至 64 位的精度。位越长,精度越高,同时占用的内
存空间也就越大。常用的精度类型有 tf.int16、tf.int32、tf.int64、tf.float16、tf.float32、
tf.float64 等,其中 tf.float64 即为 tf.double

创建向量保存精度
tf.constant(216541,dtype=tf.int16)
<tf.Tensor: id=22, shape=(), dtype=int16, numpy=19933>
tf.constant(12313,dtype=tf.float32)
<tf.Tensor: id=23, shape=(), dtype=float32, numpy=12313.0>
tf.constant(7623547267234,dtype=tf.int16)
<tf.Tensor: id=24, shape=(), dtype=int16, numpy=-22366>

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

import numpy as np
np.pi
3.141592653589793
tf.constant(np.pi,dtype=tf.float32)
<tf.Tensor: id=25, shape=(), dtype=float32, numpy=3.1415927>

采用的数据的维数越高,那么保存得数据就越准确

对于大部分深度学习算法,一般使用 tf.int32 和 tf.float32 可满足大部分场合的运算精
度要求,部分对精度要求较高的算法,如强化学习某些算法,可以选择使用 tf.int64 和
tf.float64 精度保存张量。

读取精度

通过dtype属性可以查询精度

print('before:',a.dtype) # 读取原有张量的数值精度
before: <dtype: 'bool'>
a = tf.constant(625364)
print('before:',a.dtype) # 读取原有张量的数值精度
before: <dtype: 'int32'>

精度的转换

a = tf.cast(a,tf.float32)
a
<tf.Tensor: id=27, shape=(), dtype=float32, numpy=625364.0>

类型的转换

转换为低精度

a = tf.constant(np.pi,dtype=tf.float16)
a
<tf.Tensor: id=29, shape=(), dtype=float16, numpy=3.14>
tf.cast(a,tf.float32)
<tf.Tensor: id=30, shape=(), dtype=float32, numpy=3.140625>

进行类型转换时,需要保证转换操作的合法性,例如将高精度的张量转换为低精度的张量
时,可能发生数据溢出隐患:


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值