python怎么获取tensor()数据类型中的值(python,tensor(),开发技术)

时间:2024-05-08 05:29:00 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

获取tensor()数据类型的值

一、问题

python怎么获取tensor()数据类型中的值

只想要216.8973那个数。

二、解决方法

1、单个tensor

tensor.item()

就可以得到216.8973。

2、多个tensor

tensor.tolist()

python怎么获取tensor()数据类型中的值

tensorflow笔记:tensor数据类型

常见的数据类型载体

  • list

  • np.array

  • tf.tensor

  • list: 可以存储不同数据类型,缺点不适合存储较大的数据,如图片

  • np.array: 解决同类型大数据数据的载体,方便数据运算,缺点是在深度学习之前就设计好的,不支持GPU

  • tf.tensor:更适合深度学习,支持GPU

Tensor是什么

  • scalar: 1.1

  • vector:[1.1] , [1.1,2.2,……]

  • matrix:[[1,2,3,],[4,5,6],[7,8,9]]

  • torsor:rank > 2 (一般指的是维度大于2的数据)

但是,在tensorflow里面我们把数据的数据都叫tensor

Tensor支持的类型

  • int, float, double

  • bool

  • string

创建不同类型的Tensor

importtensorflowastf#创建一个整型的数据tf.constant(1)#Out[3]:<tf.Tensor:shape=(),dtype=int32,numpy=1>#注意因为这里的constant就是一个普通的tensor,不要理解为常量了(TF1.0是代表一个常量)#创建一个浮点类型的数据tf.constant(1.)#Out[4]:<tf.Tensor:shape=(),dtype=float32,numpy=1.0>#若给定一个浮点型的数据,但是指定为int类型会报错tf.constant(2.2,dtype=tf.int32)#TypeError:Cannotconvert2.2toEagerTensorofdtypeint32#给一数指定双精度tf.constant(2.,dtype=tf.double)#Out[6]:<tf.Tensor:shape=(),dtype=float64,numpy=2.0>#创建bool类型的数据tf.constant([True,False])#Out[7]:<tf.Tensor:shape=(2,),dtype=bool,numpy=array([True,False])>#创建字符串型数据(很少用)tf.constant("hello,world")#Out[8]:<tf.Tensor:shape=(),dtype=string,numpy=b'hello,world'>

Tensor Property

下面开始介绍Tensor常用的属性

tf.device

importtensorflowastfwithtf.device("cpu"):a=tf.constant([1])withtf.device("gpu"):b=tf.range(6)print(a.device)print(b.device)#数据在CPU和GPU上的转换aa=a.gpu()print(aa.device)bb=b.cpu()print(bb.device)

输出结果:

/job:localhost/replica:0/task:0/device:CPU:0
/job:localhost/replica:0/task:0/device:GPU:0
/job:localhost/replica:0/task:0/device:GPU:0
/job:localhost/replica:0/task:0/device:CPU:0

转换为numpy

c=tf.range(10)#Out[14]:<tf.Tensor:shape=(10,),dtype=int32,numpy=array([0,1,2,3,4,5,6,7,8,9])>c.numpy()#Out[15]:array([0,1,2,3,4,5,6,7,8,9])

Tensor的维度与形状

d=tf.range(10)d.shape#Out[17]:TensorShape([10])d.ndim#Out[18]:1#用rank查看tensor的维度(秩):返回的是一个tensor类型的数据tf.rank(d)#Out[19]:<tf.Tensor:shape=(),dtype=int32,numpy=1>tf.rank(tf.ones([3,4,2]))#Out[20]:<tf.Tensor:shape=(),dtype=int32,numpy=3>#tf.name#是Tensorflow1.0中的概念,现在基本已经淘汰了

python中判断一个数据是不是Tensor

importnumpyasnpimporttensorflowastfa=tf.constant(1.)b=tf.constant([True,False])c=tf.constant("hello,world")d=np.arange(4)isinstance(a,tf.Tensor)#Out[27]:Truetf.is_tensor(b)#Out[28]:Truetf.is_tensor(d)#Out[29]:Falsea.dtype,b.dtype,c.dtype,d.dtype#Out[32]:(tf.float32,tf.bool,tf.string,dtype('int32'))a.dtype==tf.float32Out[33]:Truec.dtype==tf.stringOut[34]:True

数据类型的转换

a=np.arange(5)a.dtypeOut[36]:dtype('int32')aa=tf.convert_to_tensor(a)#numpy数据转化方法为.astype(np.int64)#Out[38]:<tf.Tensor:shape=(5,),dtype=int32,numpy=array([0,1,2,3,4])>aa=tf.convert_to_tensor(a,dtype=tf.float32)#Out[40]:<tf.Tensor:shape=(5,),dtype=float32,numpy=array([0.,1.,2.,3.,4.],dtype=float32)>#用头tf.cast()数据转化tf.cast(aa,dtype=tf.float32)#Out[41]:<tf.Tensor:shape=(5,),dtype=float32,numpy=array([0.,1.,2.,3.,4.],dtype=float32)>aaa=tf.cast(aa,dtype=tf.double)#Out[43]:<tf.Tensor:shape=(5,),dtype=float64,numpy=array([0.,1.,2.,3.,4.])>tf.cast(aaa,dtype=tf.int32)#Out[44]:<tf.Tensor:shape=(5,),dtype=int32,numpy=array([0,1,2,3,4])>#bool与int的转化b=tf.constant([0,1])tf.cast(b,tf.bool)#Out[46]:<tf.Tensor:shape=(2,),dtype=bool,numpy=array([False,True])>bb=tf.cast(b,dtype=tf.bool)tf.cast(bb,tf.int32)#Out[48]:<tf.Tensor:shape=(2,),dtype=int32,numpy=array([0,1])>

tf.Variable

tf.Variable在tensorflow中相比tf.constan一样也是Tensor,tf.Variable特指Tensorflow中哪些可以优化的参数,比如自动求导。

tf.Variable可以理解为是专门为神经网络所设立的一个类型。

a=tf.range(5)b=tf.Variable(a)#Out[51]:<tf.Variable'Variable:0'shape=(5,)dtype=int32,numpy=array([0,1,2,3,4])>b.dtype#Out[52]:tf.int32b.name#Out[53]:'Variable:0'b=tf.Variable(a,name="input_data")b.name#Out[55]:'input_data:0'b.trainable#Out[56]:Trueisinstance(b,tf.Tensor)#Out[57]:Falseisinstance(b,tf.Variable)#Out[58]:Truetf.is_tensor(b)#Out[59]:Trueb.numpy()#Out[60]:array([0,1,2,3,4])

将Tensor类型转化为python中的数据类型

a=tf.ones([])#Out[63]:<tf.Tensor:shape=(),dtype=float32,numpy=1.0>a.numpy()#Out[64]:1.0int(a)#Out[65]:1float(a)#Out[66]:1.0
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:python怎么获取tensor()数据类型中的值的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:C++中的四种类型转换符是什么下一篇:

21 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18