加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python中的x.type和type(x)有什么区别?

发布时间:2020-12-20 12:32:37 所属栏目:Python 来源:网络整理
导读:请考虑以下几行 import theano.tensor as T x = T.dscalar('x')y = T.dscalar('y')z = x+y 然后, In [15]: type(x)Out[15]: theano.tensor.var.TensorVariable 而, In [16]: x.typeOut[16]: TensorType(float64,scalar) 为什么type(x)和x.type提供两个不同的
请考虑以下几行

import theano.tensor as T 

x = T.dscalar('x')
y = T.dscalar('y')
z = x+y

然后,

In [15]: type(x)
Out[15]: theano.tensor.var.TensorVariable

而,

In [16]: x.type
Out[16]: TensorType(float64,scalar)

为什么type(x)和x.type提供两个不同的信息?他们传达了什么信息?

我也看到指的是Theano tutorial,

>>> type(x)
<class 'theano.tensor.basic.TensorVariable'>
>>> x.type
TensorType(float64,scalar)

为什么类型(x)输出在我的情况下是不同的?这些是由版本特定的实现差异引起的,这是由这种差异所表示的吗?

解决方法

theano.tensor有一个属性 type,当你说时,你正在看

x.type

这与许多对象携带的numpy对象dtype属性类似(如果您熟悉该库).

另一方面,type是一个Python函数,它查看传入的对象的实际类型,对于类型(x)确实是一个

theano.tensor.var.TensorVariable

总而言之,您将属性与实际对象类型进行比较.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读