考虑以下几行
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)
为什么type(x)输出在我的情况下是不同的?这些是由版本特定的实现差异引起的,以及这种差异所表示的是什么?
答案 0 :(得分:8)
type(x)
是内置的。
x.type
是您对象中定义的属性。
它们是完全独立的,type(x)
返回对象x
的类型,x.type
执行对象想要的任何操作。在这种情况下,它返回一些有关对象类型的信息
答案 1 :(得分:5)
theano.tensor
有一个属性type
x.type
这与它们的许多对象所携带的numpy对象dtype
属性类似(如果您熟悉该库)。
另一方面,type
是一个Python函数,它查看传入的对象的实际类型,type(x)
确实是
theano.tensor.var.TensorVariable
简而言之,您将属性与实际对象类型进行比较。
答案 2 :(得分:0)
正如其他人所提到的,type(x)
是Python的builtin function,它返回对象的类型。它与Theano本身无关。这个内置函数可以应用于任何Python对象(Python中的所有内容都是对象)。例如,
type(1)
是int
,type(True)
是bool
,type(lambda x: x * x)
是function
等。有趣的是,您可以在type
上致电type
(包括type
在内的所有内容都是对象) - type(type)
为type
。
顺便说一下,type(T.dscalar)
是TensorType
(确切地说theano.tensor.type.TensorType
)。
x.type
是对象x
的属性。它指回type(T.dscalar)
。 x.type
会返回TensorType(float64, scalar)
- 这不仅会向您显示T.dscalar
的类型,还会告诉您x
是标量且它是64位浮点数。
type属性的其他示例:
>>> iv = T.ivector()
>>> iv.type
TensorType(int32, vector) # iv is a vector of 32-bit ints
>>> fm = T.fmatrix()
>>> fm.type
TensorType(float32, matrix) # fm is a matrix of 32-bit floats
>>> lt3 = T.ltensor3()
>>> lt3.type
TensorType(int64, 3D) # lt3 is a 3D array of 64-bit ints