x.type 和 Python 中的 type(x) 有什么区别?
what is the difference between x.type and type(x) in 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.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) 输出不同?这些是由版本特定的实现差异引起的吗?这种差异意味着什么?
theano.tensor
有一个属性 type
,当您说
时,您正在查看该属性
x.type
这类似于 numpy 对象 dtype
其许多对象所具有的属性(如果您熟悉该库)。
另一方面,type
是一个 Python 函数,它查看您传入的对象的实际类型,type(x)
确实是一个
theano.tensor.var.TensorVariable
长话短说,您是在将属性与实际对象类型进行比较。
type(x)
是内置函数。
x.type
是在您的对象中定义的属性。
它们是完全独立的,type(x)
returns 什么类型的对象 x
和 x.type
做任何对象想要它做的事情。在这种情况下,它 returns 一些关于对象类型的信息
正如其他人提到的,type(x)
是 Python 的 builtin function 即 returns 对象的类型。它与 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
returns TensorType(float64, scalar)
- 这不仅向您展示了 T.dscalar
的类型,还告诉您 x
是标量并且是 64 位浮点数。
类型属性的其他例子:
>>> 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
考虑以下几行
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) 输出不同?这些是由版本特定的实现差异引起的吗?这种差异意味着什么?
theano.tensor
有一个属性 type
,当您说
x.type
这类似于 numpy 对象 dtype
其许多对象所具有的属性(如果您熟悉该库)。
另一方面,type
是一个 Python 函数,它查看您传入的对象的实际类型,type(x)
确实是一个
theano.tensor.var.TensorVariable
长话短说,您是在将属性与实际对象类型进行比较。
type(x)
是内置函数。
x.type
是在您的对象中定义的属性。
它们是完全独立的,type(x)
returns 什么类型的对象 x
和 x.type
做任何对象想要它做的事情。在这种情况下,它 returns 一些关于对象类型的信息
正如其他人提到的,type(x)
是 Python 的 builtin function 即 returns 对象的类型。它与 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
returns TensorType(float64, scalar)
- 这不仅向您展示了 T.dscalar
的类型,还告诉您 x
是标量并且是 64 位浮点数。
类型属性的其他例子:
>>> 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