数据类型
tensorflow的基础是数据类型,依赖特有的函数进行创建,此部分介绍与演示创建最基础常用的数据类型
数值类型、 字符串类型和布尔类型
数值类型
- 标量 0,1,2,3,4,5
创建标量实例:
import tensorflow as tf
a = tf.constant(1.2)
b = tf.constant(5.4)
print(a+b)
print(type(a))
print(tf.is_tensor(a))
out:
tf.Tensor(6.6000004, shape=(), dtype=float32)
<class 'tensorflow.python.framework.ops.EagerTensor'>
True
- 向量 [0],[1],[0,1,2,3,4,5,5.6] 维度dim为1, shape为[n]
创建向量实例:
import tensorflow as tf
x = tf.constant([1,3,5,6.7])
print(x)
print(x.numpy)
out:
tf.Tensor([1. 3. 5. 6.7], shape=(4,), dtype=float32)
<bound method _EagerTensorBase.numpy of <tf.Tensor: id=0, shape=(4,), dtype=float32, numpy=array([1. , 3. , 5. , 6.7], dtype=float32)>>
- 矩阵 [[1,2],[3,4]] 维度dim为2,shape为[n,m]
创建矩阵实例:
import tensorflow as tf
b=tf.constant([[1,3],[2,4]])
print(b)
print(b.shape)
out:
tf.Tensor(
[[1 3]
[2 4]], shape=(2, 2), dtype=int32)
(2, 2)
- 张量 维度dim>2,shape为[n,m,…,k] ,每个维度也叫做轴,每个维度可以根据实际需要赋予物理意义,
如,shape为[2,32,32,3]可以表示为2张图片,高32,宽32,RGB(颜色)通道为3
创建张量实例:
import tensorflow as tf
b=tf.constant([[[1,3],[2,4]],[[7,8],[6,7]]])
print(b)
out:
tf.Tensor(
[[[1 3]
[2 4]]
[[7 8]
[6 7]]], shape=(2, 2, 2), dtype=int32)
字符串类型
import tensorflow as tf
a=tf.constant('Hello,This Beautiful World')
print(a)
out:
tf.Tensor(b'Hello,This Beautiful World', shape=(), dtype=string)
功能 | 命令 |
---|---|
小写化 | lower() |
拼接 | join() |
长度 | length() |
切分 | split() |
help(tf.strings.lower) 用于查看函数的帮助信息
小写化操作:
import tensorflow as tf
a=tf.constant('Hello,This Beautiful World')
print(a)
b=tf.strings.lower(a)
print(b)
out:
tf.Tensor(b'Hello,This Beautiful World', shape=(), dtype=string)
tf.Tensor(b'hello,this beautiful world', shape=(), dtype=string)
布尔类型
import tensorflow as tf
a=tf.constant(True)
b=tf.constant([True,False])
print(a,'\n',b)
print(a is True)#TF 布尔类型张量与 python 布尔类型比较
print(a == False)#数值比较,**以张量形式返回值**
out:
tf.Tensor(True, shape=(), dtype=bool)
tf.Tensor([ True False], shape=(2,), dtype=bool)
False
tf.Tensor(False, shape=(), dtype=bool)
数值精度
- 常用有tf.int16、 tf.int32、 tf.int64、 tf.float16、 tf.float32、tf.float64等数值类型
- tf.bool布尔类型
- 选择适当的精度,保证计算精度,防止溢出错误(小类型存储大数)
- 长度越长,储存精度越高,内存占用越大 ,执行速度越慢
import tensorflow as tf
a = tf.constant(128, dtype = tf.int64)
print('before:',a.dtype)#读取原精度
b = tf.cast(a,tf.float32)#精度转换操作
print('after:',b.dtype)
out:
before: <dtype: 'int64'>
after: <dtype: 'float32'>
待优化张量
- 为了区分需要计算梯度的张量和不需要计算梯度的张量,引入专有数据类型tf.Variable,tf.Variable数据类型在普通张量的基础上添加了name,trainable 等属性来支持计算图的构建。
- 用于计算过程中不断更新的参数储存,如W , b,方便跟踪相关梯度的信息
- trainable属性表征当前张量是否需要被优化,创建 Variable 对象时是默认启用优化标志,可以设置
trainable=False 来设置张量不需要优化 - 待优化张量可视为普通张量的特殊类型, 普通张量其实也可以通过 GradientTape.watch()方
法临时加入跟踪梯度信息的列表,从而支持自动求导功能。
普通张量转tf.Variable
import tensorflow as tf
a = tf.constant([1,2,3,4,5,6])
b = tf.Variable(a)
print(a,'\n',b)
print(b.name,b.trainable)
out:
tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
<tf.Variable 'Variable:0' shape=(6,) dtype=int32, numpy=array([1, 2, 3, 4, 5, 6])>
Variable:0 True
直接创建:tf.Variable
import tensorflow as tf
b = tf.Variable([1,2,3,4])
print(b)
out:
<tf.Variable 'Variable:0' shape=(4,) dtype=int32, numpy=array([1, 2, 3, 4])>