tensorflow2.1的数据类型

  • TensorFlow2.1的数据类型有:

int, float, double

bool

string

首先我们简单的创建数据类型:

注:constant,我们TensorFlow1.0版本时,我们将它理解为一个常量,但在TensorFlow2.0时我们将它就理解一个普通的Tensor

import tensorflow as tf

# 创建一个整型的Tensor
tf.constant(1)
Out[3]: <tf.Tensor: shape=(), dtype=int32, numpy=1>

# 带有小数点,就当成一个浮点型的tensor
tf.constant(1.0)
Out[4]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>
tf.constant(2.2)
Out[5]: <tf.Tensor: shape=(), dtype=float32, numpy=2.2>

# 指定一个double类型的tensor
tf.constant(2., dtype = tf.double)
Out[6]: <tf.Tensor: shape=(), dtype=float64, numpy=2.0>

# 创建一个bool型的tensor
tf.constant([True, False])
Out[7]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>

# 创建一个string类型的tensor
tf.constant('hello, world.')
Out[8]: <tf.Tensor: shape=(), dtype=string, numpy=b'hello, world.'>
  • 对于tensor一些常见的属性

device: 返回一个string类型,指的是一个设备名字

CPU上的tensor和GPU上的tensor的区别:对于一个GPU上的tensor只能在GPU上的一些操作,对于一个CPU上的tensor只能在CPU上的一些操作

# 通过cpu、gpu的环境创建
with tf.device("cpu"):
    a = tf.constant([1])
with tf.device("gpu"):
    b = tf.constant([4])

# 查看设备名
a.device   # Out[11]: '/job:localhost/replica:0/task:0/device:CPU:0'
b.device   # Out[12]: '/job:localhost/replica:0/task:0/device:CPU:0'

# 将tensor在GPU和CPU上互相转移
aa = a.gpu()
aa.device  # Out[14]: '/job:localhost/replica:0/task:0/device:GPU:0'

bb = b.cpu()
bb.device  # Out[16]: '/job:localhost/replica:0/task:0/device:CPU:0'

# 将tensor转换成一个numpy类型
b.numpy()
Out[17]: array([4])

# 查看shape
b.shape
Out[19]: TensorShape([1])

# 查看ndim和rank
b.ndim
Out[20]: 1
tf.rank(b)

Out[21]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
tf.rank(tf.ones([3, 4, 2]))
Out[22]: <tf.Tensor: shape=(), dtype=int32, numpy=3>

# 查看name:(tensorflow1.0遗留下来的,意义不大)
b.name
AttributeError: Tensor.name is meaningless when eager execution is enabled.
  • 判断一个物体是否是一个tensor
import numpy as np

a = tf.constant([1.])
b = tf.constant([True, False])
c = tf.constant('hello, world.')
d = np.arange(4)

# 判断是否为tensor
isinstance(a, tf.Tensor)
Out[35]: True
tf.is_tensor(b)
Out[33]: True
tf.is_tensor(d)
Out[36]: False

# 查看数据类型
a.dtype, b.dtype, c.dtype
Out[39]: (tf.float32, tf.bool, tf.string)

# 对数据类型进行一个比较
a.dtype == tf.float32
Out[41]: True
c.dtype == tf.string
Out[44]: True
  • 类型之间的相互转化
a = np.arange(5)
a
Out[46]: array([0, 1, 2, 3, 4])
a.dtype
Out[47]: dtype('int32')

# 将numpy转换成tensor
dtype('int32')
aa = tf.convert_to_tensor(a)
# <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
aa = tf.convert_to_tensor(a, dtype = tf.int64)  # 指定转换的类型
# <tf.Tensor: shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4], dtype=int64)>

# 将tensor的数据类型之间的转换
tf.cast(aa, dtype = tf.float32)
Out[53]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
aaa = tf.cast(aa, dtype = tf.double)
Out[55]: <tf.Tensor: shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>
tf.cast(aaa, dtype = tf.int32)
Out[56]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
  • 正型(int)& 布尔型(bool)之间的一个转换
b = tf.constant([0, 1])
tf.cast(b, dtype = tf.bool)
Out[58]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False,  True])>

bb = tf.cast(b, dtype = tf.bool)
tf.cast(bb, tf.int32)
Out[61]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1])>
  • 一个特殊的数据类型tf.Variable
a = tf.range(5)
a
Out[63]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>

b = tf.Variable(a)
b.dtype
Out[66]: tf.int32
b.name
Out[67]: 'Variable:0'

b = tf.Variable(a, name = 'input_data')
b.name
Out[70]: 'input_data:0'
b.trainable   # 是否可训练
Out[72]: True

# 推荐使用is_tensor()
isinstance(b, tf.Tensor)
Out[73]: False
isinstance(b, tf.Variable)
Out[74]: True
tf.is_tensor(b)
Out[75]: True

b.numpy()
Out[77]: array([0, 1, 2, 3, 4])
  • 如何将一个Tensor转换为一个numpy

将GPU上的tensor转换为CPU上的numpy

使用一个numpy方法就可以转换

a.numpy()
Out[78]: array([0, 1, 2, 3, 4])
b.numpy()
Out[79]: array([0, 1, 2, 3, 4])

a = tf.ones([])
a
Out[81]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>
a.numpy()
Out[82]: 1.0
int(a)
Out[83]: 1
float(a)
Out[84]: 1.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值