04_Tensorflow2数据类型大揭秘:让你的AI不再‘数’白活,玩转数字界的‘魔术手’!

在这里插入图片描述

1. 基本数据类型

Tensorflow的基本数据类型有:数值型、字符串型和布尔型。

2. 数值型

在这里插入图片描述

注意: TensorFlow中将以上统称为张量,不作严格区分,可以通过type()函数获得数值的类型,tf.is_tensor()函数判断数值是否是张量。

总结: 张量是一个多维数组,张量属性包括:ndim(维度)、shape(形状)和dtype(类型)。tf.Tensors对象可以驻留在加速器内存中(如GPU)。TensorFlow提供了丰富的操作库,用于生成和使用张量。

2.1 张量创建

张量创建通过 tf.constant() 来实现。

tf.constant(value, dtype=None, shape=None, name=“Const”)

import tensorflow as tf
# 0维
scalar_tf=tf.constant(3.14)
# 查看张量类型及是否是张量
type(scalar_tf),tf.is_tensor(scalar_tf)
(tensorflow.python.framework.ops.EagerTensor, True)
# 1维
vec_tf=tf.constant([3.14, 0, 1])
type(vec_tf),tf.is_tensor(vec_tf)
(tensorflow.python.framework.ops.EagerTensor, True)
# 2维
max_tf=tf.constant([[3.14, 0, 1],[6.28, 0, 2]])
type(max_tf),tf.is_tensor(max_tf)
(tensorflow.python.framework.ops.EagerTensor, True)
# 3维
tensor_tf=tf.constant([[[3.14, 0, 1],[6.28, 0, 1]],[[1, 2, 3],[4, 5, 6]]])
type(tensor_tf),tf.is_tensor(tensor_tf)
(tensorflow.python.framework.ops.EagerTensor, True)
# 显示tensor内容
tensor_tf
<tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
array([[[3.14, 0.  , 1.  ],
        [6.28, 0.  , 1.  ]],

       [[1.  , 2.  , 3.  ],
        [4.  , 5.  , 6.  ]]], dtype=float32)>

2.2 其他方法

# 创建常量张量,数字后面加个点代表转 float32 类型
a=tf.constant([[1.,2.,3.],[4.,5.,6.]])
a
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)>
# 创建一个 shape 形状的全为 0 元素的张量,dtype='tf.float32' 为默认值,可以自己进行设定
b = tf.zeros([2, 3])
b
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
       [0., 0., 0.]], dtype=float32)>
# 创建一个全为 1 元素的张量
c=tf.ones([2, 3])
c
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
# 改变张量中元素的数据类型,智能由低精度向高精度转化
d=tf.cast(c, float)
d
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
# tf.file(dims, value) 创建维度为 dims,值全为 value 的张量
e=tf.fill([2, 3], 3)
e
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[3, 3, 3],
       [3, 3, 3]])>
# 创建一个标准正态分布,shape:形状;mean:均值,默认是0;stddev:标准差,默认值是1;dtype:数据类型默认是 float32
f=tf.random.normal(shape=(3, 2), mean=2, stddev=1, dtype=tf.float32)
f
<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
array([[1.6208957 , 4.39842   ],
       [3.3178225 , 2.5713987 ],
       [4.0236845 , 0.70376134]], dtype=float32)>
# 创建均值分布张量,minval为最小值,maxval为最大值
h=tf.random.uniform(shape=(2, 3), minval=1, maxval=4, dtype=tf.float32)
h
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1.3163987, 2.2212248, 3.7865143],
       [1.7390326, 1.7601252, 3.8522944]], dtype=float32)>
# 随机打乱数组,只打乱第一维元素
i=tf.random.shuffle(a)
i
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[4., 5., 6.],
       [1., 2., 3.]], dtype=float32)>
# 创建 start 为起点,delta 为变化值,不超过 limit 的等差张量
j=tf.range(start=1, limit=5, delta=1, dtype=tf.float32)
j
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([1., 2., 3., 4.], dtype=float32)>

3. 字符串类型

字符串类型主要用于表示文件的路径;

3.1 字符串类型创建

同样字符串类型的创建也是通过 tf.constant() 来实现

# 字符串创建
string_tf=tf.constant('tensorflow string type')
string_tf
<tf.Tensor: shape=(), dtype=string, numpy=b'tensorflow string type'>

3.2 字符串操作

Tensorflow 中,字符串操作通过 tf.strings 模块进行的,tf.strings 模块包含了一些常见的字符串处理函数,如拼接 join(),长度 length(),切分 split() 等

# 创建字符串
string_tf=tf.constant('tensorflow string type')
# 统计长度
tf.strings.length(string_tf)
<tf.Tensor: shape=(), dtype=int32, numpy=22>

4. 布尔类型

TensorFlow的bool类型用于表达比较运算结果;

# bool类型创建
bool_tf=tf.constant(False)
bool_tf
<tf.Tensor: shape=(), dtype=bool, numpy=False>

5. 数值精度

对于数值类型的张量有不同字节长度的精度,在创建张量时可以设定其精度如:16bit 32bit 64bit;对应TensorFlow中常用的精度类型有:tf.int16,tf.int32,tf.int64, tf.float16,tf. float 32,tf. float 64(tf.double)。

tf.constant(3.1415926, dtype=tf.double)
<tf.Tensor: shape=(), dtype=float64, numpy=3.1415926>

5.1 查看数值精度

通过张量的 dtype 成员属性查看数值的精度

data_tf=tf.constant(3.1415926)
data_tf.dtype
tf.float32

5.2 数值类型转换

Tensorflow 中数据类型是通过 tf.cast 函数实现的

#创建一个张量
data_tf=tf.constant(3.1415926)
#转换其数值精度
data_tf=tf.cast(data_tf,tf.float64)
data_tf.dtype
tf.float64

注意: 同python、c++,数据类型转换需要保证合法性;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾飞开源

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值