美文网首页
TensorFlow2.0的数据类型

TensorFlow2.0的数据类型

作者: 酵母小木 | 来源:发表于2020-01-29 19:38 被阅读0次

最近的新型冠状病毒肺炎来势汹汹。宅在家挺无聊的,还是学习有趣。将今天观看的视频做个学习笔记整理一下。昨天把TensorFlow的开发环境配置好,到时候还得整理下关于开发环境的配置问题。

时间:2020年1月29日

数据载体:Tensor

和科学计算库numpy相比,支持GPU和自动求导,偏重神经网络计算。

//创建Tensor
tf.constant(
    value,
    dtype=None,
    shape=None,
    name='Const'
)

//整型
+ tf.constant(1) 
- <tf.Tensor: id=2, shape=(), dtype=int32, numpy=1>
// shape为尺寸,dtype为数据类型,numpy为对应的numpy数据

//单精度浮点型
+ tf.constant(1.)
- <tf.Tensor: id=4, shape=(), dtype=float32, numpy=1.0>

//双精度浮点型
+ tf.constant(2., dtype=tf.double)  //指定数据类型,指定类型精度需要高于数据的类型
- <tf.Tensor: id=7, shape=(), dtype=float64, numpy=2.0>

//布尔型
+ tf.constant([True, False])
- <tf.Tensor: id=9, shape=(2,), dtype=bool, numpy=array([ True, False])>

//字符串型
+ tf.constant('hello,world.')
- <tf.Tensor: id=14, shape=(), dtype=string, numpy=b'gello,world.'>

1.属性(Property)

  • device:返回当前Tensor所在设备的名字,string类型
  • numpy:将Tensor数据类型转换为numpy对应的数据类型
  • shape:返回类似List的shape
  • ndim:返回数据维度
  • rank:返回张量的秩,Tensor数据类型
    【注】张量的秩是唯一选择张量的每个元素所需的索引的数量
//练习程序示例,运行环境【ipython】
//设置相关Tensor对象的运行设备
with tf.device("cpu"):
a = tf.constant([1])

with tf.device("gpu"):
b = tf.range(4)

//Tensor对应的操作必须是在同一设备上进行的,使用device属性可以查看Tensor对象的设备
a.device
b.device

2.判断Tensor类型

  • 判断参数是否是tf.Tensor
isinstance(a, tf.Tensor)  //a为参数
tf.is_tensor(b)                //b为参数
  • 输出tf.Tensor的数据类型
a.dtype
//判断tf.Tensor的类型
a.dtype == tf.float32
b.dtype == tf.float64
c.dtype == tf.string
d.dtype == tf.bool

3.Tensor数据类型转换

  • numpy与Tensor数据之间的转换
a = np.arange(5)
a.dtype
# dtype('int64')

//numpy转换为Tensor
aa = tf.convert_to_tensor(a)
# <tf.Tensor: id=19, shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])>

//设置目标转换类型
aa = tf.convert_to_tensor(a, dtype=tf.int32)
# <tf.Tensor: id=21, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4]>

//Tensor转换为numpy
aa.numpy()

//对于标量数据类型,可以直接转换
a=tf.ones([])

a.numpy()  # 1.0
int(a)           # 1
float(a)        # 1.0
  • 整型和浮点型、布尔型之间的相互转换
// aa为整型,整型转换为单精度浮点型
tf.cast(aa, dtype=tf.float32)
# <tf.Tensor: id=23, shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.]>

//整型转换为双精度浮点型
aaa = tf.cast(aa, dtype=tf.double)
# <tf.Tensor: id=25, shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.]>

//双精度浮点型转换为整型
tf.cast(aaa, dtype=tf.int32)
# <tf.Tensor: id=27, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4]>

//整型转换为布尔型
b = tf.constant([0, 1])
tf.cast(b, dtype=tf.bool)
# <tf.Tensor: id 29, shape=(2,), dtype=bool, numpy=array([False, True])>

//布尔型转换为整型
bb = tf.cast(b, dtype=tf.bool)
tf.cast(bb, tf.int32)
# <tf.Tensor: id 34, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>

4.tf.Variable数据类型

该类型主要用于可以优化的参数,如神经网络之间的参数和权重

a = tf.range(2)
# <tf.Tensor: id 41, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>

b = tf.Variable(a)
b.dtype   # tf.int32
b.trainable  # True

//isinstance和tf.is_tensor()的差异
isinstance(b, tf.Tensor)  # False
isinstance(b, tf.Variable)  # True
tf.is_tensor(b)    # True

//Variable数据类型转换为numpy数据类型
b.numpy()
# array([0, 1, 2, 3, 4], dtype=int32)

参考资料

相关文章

网友评论

      本文标题:TensorFlow2.0的数据类型

      本文链接:https://www.haomeiwen.com/subject/iuovthtx.html