tensorflow的数据类型

数据类型包括:

int,float,double,bool,String

数据主要有:

list(列表),np.array(np的数组), tf.Tensor (tf的张量)

什么是张量?

scalar(标量),venctor(向量),matrix(矩阵),tensor(rank>2)

这些都可以是tensor,但是一般指向量或者矩阵维度大于2的豆角tensor

 

 

 

 

 

 

 

 

import  tensorflow as tf
import numpy as np
#创建一个整型
tf.constant(1)

#创建一个浮点型
tf.constant(1.)

#双精度
tf.constant(1.,dtype=tf.double)

#bool型
tf.constant([True,False])

#字符串型
tf.constant("hello,world")

with tf.device("cpu"):
    a=tf.constant([1])

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

a.device #返回当前tensor所在系统的名称(cpu或者gpu)
b.device

#当一个tensor从cpu转向gpu,或者gpu转向cpu
aa = a.gpu()
aa.device

#tensor转换到numpy
bb = b.numpy()
#bb的维度
bb.ndim
bb.shape

#判断某一个是否是tensor
tf.is_tensor(b)
#numpy默认是64位,转换为tensor的时候需要指定成32位才会转换

a=np.arange(4)

aa=tf.convert_to_tensor(a,dtype=tf.int32)

#专门数据转换其他数据类型,数据转换基本都是cast
aa=tf.cast(aa,dtype=tf.float32)

#整型转换布尔型
b = tf.constant([0,1])
tf.cast(b,dtype=tf.bool)

a=tf.range(5)

c=tf.Variable(a)
c.dtype
#类似激活函数,一个tensor被Variable包装以后,会自动获取可以求导的特性,神经网的w
d=tf.Variable(a,name="input_data")
d.name
d.trainable #可以被训练,求导,True

#tensor是gpu上面的,如果要吧数据返回到cpu,需要转换成numpy
a.numpy()


 

小结:

 tf.convert_to_tensor把list和numpy转换为tensor

cast是专用tensor转换api

tf.Variable(a,name="input_data")把数据设置成可以自动求导

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值