tensorflow2学习-数据类型

数据类型

1、list

2、nd.array

无法进行GPU的计算

3、tf.Tensor

3.1可以包含int, float,double, bool, string

tf.constant(1)
tf.constant(2.)
tf.constant(1.1,dtype = tf.int32)  #报错
tf.constant(2.,dtype = tf.double)
tf.constant([True,Flase])
tf.constant('hello')
with tf.device('cpu'):
	a = tf.constant([1])
with tf.device('gpu'):
	b = tf.range(4)
a.device #返回所在GPU还是CPU运行
aa = a.gpu() #cpu和GPU的转换

b.numpy() #将tenson装换为numpy
a.ndim
tf.rank(b)

3.2、类型转换:

isinstance(a, tf.Tensor) #判断是否为tf
tf.is_tensor(b)    #推荐使用
a.dtype()   #查看类型

a = np.arange(5)   #int64
aa = tf.convert_to_tensor(a, dtype = tf.int32) 
tf.cast(aa, dtype= tf.float32)

b = tf.constant([0,1])
tf.cast(b, dtype = tf.bool)  #bool类型的转换

tf.Variable:针对可以优化的参数,如w, b

a = tf.range(5)
b = tf.Variable(a)
b.dtype  #tf.int32
b.name   #'Variable: 0 '
b = tf.Variable(a, name = 'input_data')
b.trainable   #True
isinstance(b, tf.Tensor)  #False  
isinstance(b, tf.Variable)  #True
tf.is_tensor(b)  #True
# 因此在上述过程中,推荐使用is_tensor()
a.numpy()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值