Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
596f9bca
提交
596f9bca
authored
1月 21, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
0581942d
ffee86c7
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
572 行增加
和
248 行删除
+572
-248
generate_dtype_tensor_table.py
doc/generate_dtype_tensor_table.py
+32
-0
basic.txt
doc/library/tensor/basic.txt
+517
-244
index.txt
doc/library/tensor/index.txt
+2
-2
adding.txt
doc/tutorial/adding.txt
+1
-1
symbolic_graphs.txt
doc/tutorial/symbolic_graphs.txt
+10
-1
basic.py
theano/tensor/basic.py
+10
-0
没有找到文件。
doc/generate_dtype_tensor_table.py
0 → 100644
浏览文件 @
596f9bca
letters
=
[
(
'b'
,
'int8'
),
(
'w'
,
'int16'
),
(
'i'
,
'int32'
),
(
'l'
,
'int64'
),
(
'd'
,
'float64'
),
(
'f'
,
'float32'
),
(
'c'
,
'complex64'
),
(
'z'
,
'complex128'
)
]
shapes
=
[
(
'scalar'
,
()),
(
'vector'
,
(
False
,)),
(
'row'
,
(
True
,
False
)),
(
'col'
,
(
False
,
True
)),
(
'matrix'
,
(
False
,
False
)),
(
'tensor3'
,
(
False
,
False
,
False
)),
(
'tensor4'
,
(
False
,
False
,
False
,
False
)),]
hdr
=
'============ =========== ==== =========== ================================='
print
hdr
print
'Constructor dtype ndim shape broadcastable'
print
hdr
for
letter
in
letters
:
for
shape
in
shapes
:
suff
=
',)'
if
len
(
shape
[
1
])
==
1
else
')'
s
=
'('
+
','
.
join
(
'1'
if
b
else
'?'
for
b
in
shape
[
1
])
+
suff
print
'
%
s
%-10
s
%-10
s
%-4
s
%-10
s
%-20
s'
%
(
letter
[
0
],
shape
[
0
],
letter
[
1
],
len
(
shape
[
1
]),
s
,
shape
[
1
]
)
print
hdr
doc/library/tensor/basic.txt
浏览文件 @
596f9bca
.. currentmodule:: tensor
===========================
Basic Tensor Functionality
===========================
.. _libdoc_tensor_type:
Theano supports any kind of Python object, but its focus is support for
symbolic matrix expressions. When you type,
TensorType
==========
>>> x = T.fmatrix()
.. class:: TensorType(Type)
the ``x`` is a :class:`TensorVariable` instance.
The ``T.fmatrix`` object itself is an instance of :class:`TensorType`.
Theano knows what type of variable ``x`` is because ``x.type``
points back to ``T.fmatrix``.
.. attribute:: broadcastable
This chapter explains the various ways of creating tensor variables,
the attributes and methods of :class:`TensorVariable` and :class:`TensorType`,
and various basic symbolic math and arithmetic that Theano supports for
tensor variables.
.. attribute:: ndim
.. _libdoc_tensor_creation:
.. attribute:: dtype
Creation
========
.. _libdoc_tensor_variable:
Theano provides a list of predefined tensor types that can be used
to create a tensor variables. Variables can be named to facilitate debugging,
and all of these constructors accept an optional ``name`` argument.
For example, the following each produce a TensorVariable instance that stands
for a 0-dimensional ndarray of integers with the name ``'myvar'``:
TensorVariable
==============
>>> x = scalar('myvar', dtype='int32')
>>> x = iscalar('myvar')
>>> x = TensorType(dtype='int32', broadcastable=())('myvar')
.. class:: _tensor_py_operators(object)
Constructors with optional dtype
----------------------------------------
This mix-in class adds convenient attributes, methods, and support for Python operators.
.. function:: scalar(name=None, dtype='float64')
.. method:: reshape(shape, ndim=None)
Return a Variable for a 0-dimensional ndarray
Returns a view of this tensor that has been reshaped as in
numpy.reshape. If the shape is a Variable argument, then you might
need to use the optional `ndim` parameter to declare how many elements
the shape has, and therefore how many dimensions the reshaped Variable
will have.
.. function:: vector(name=None, dtype='float64')
See :func:`reshape`.
Return a Variable for a 1-dimensional ndarray
.. method:: dimshuffle(*pattern
)
.. function:: row(name=None, dtype='float64'
)
Returns a view of this tensor with permuted dimensions. Typically the
pattern will include the integers 0, 1, ... ndim-1, and any number of
'x' characters in dimensions where this tensor should be broadcasted.
Return a Variable for a 2-dimensional ndarray
in which the number of columns is guaranteed to be 1.
See :func:`dimshuffle`.
.. function:: col(name=None, dtype='float64')
.. method:: flatten(ndim=1)
Return a Variable for a 2-dimensional ndarray
in which the number of columns is guaranteed to be 1.
Returns a view of this tensor with `ndim` dimensions, whose shape for the first
`ndim-1` dimensions will be the same as `self`, and shape in the
remaining dimension will be expanded to fit in all the data from self.
.. function:: matrix(name=None, dtype='float64')
See :func:`flatten`.
Return a Variable for a 2-dimensional ndarray
.. attribute:: T
.. function:: tensor3(name=None, dtype='float64')
Transpose of this tensor.
Return a Variable for a 3-dimensional ndarray
>>> x = T.zmatrix()
>>> y = 3+.2j * x.T
.. function:: tensor4(name=None, dtype='float64')
.. note::
In numpy and in Theano, the transpose of a vector is exactly the
same vector! Use `reshape` or `dimshuffle` to turn your vector
into a row or column matrix.
.. class:: TensorVariable(Variable, _tensory_py_operators)
The result of symbolic operations typically have this type.
Return a Variable for a 4-dimensional ndarray
.. class:: TensorConstant(Variable, _tensory_py_operators)
.. #COMMENT
Each of the types described above can be constructed by two methods:
a singular version (e.g., :ref:`dmatrix <libdoc_tensor_creation>`)
and a plural version (:ref:`dmatrices <libdoc_tensor_creation>`).
When called, the singular version takes a single
argument which is the name of the *Variable* we want to make and it
makes a single Variable of that type. The plural version can either take
Python and numpy numbers used in Theano graphs are wrapped in this type.
.. class:: TensorSharedVariable(Variable, _tensory_py_operators)
All Fully-Typed Constructors
----------------------------
This type is returned by :func:`shared` when the initial value is a numpy
ndarray.
The following TensorType instances are provided in the theano.tensor module.
They are all callable, and accept an optional ``name`` argument. So for example:
.. code-block:: python
.. _libdoc_tensor_creation:
from theano.tensor import *
Creation
========
x = dmatrix() # creates one Variable with no name
x = dmatrix('x') # creates one Variable with name 'x'
xyz = dmatrix('xyz') # creates one Variable with name 'xyz'
Theano provides a list of predefined tensor types that can be used
to create a tensor variables. The names of the predefined types follow
a simple recipe :
``<dtype><dimensionality>``
Where ``<dtype>`` is one of (note that this is not a complete list of
possible ``<dtypes>``, it just covers thouse used by the predefined
types):
==== ========== =============== =================
code type domain bits
==== ========== =============== =================
b byte signed integer 8
w word signed integer 16
i integer signed integer 32
l long signed integer 64
f float floating point 32
d double floating point 64
c complex64 complex 64 (two float32)
z complex128 complex 128 (two float64)
==== ========== =============== =================
``<dimensionality>`` is one of:
======== ============= =================================================================
code shape :ref:`broadcastable <libdoc_tensor_broadcastable>` pattern
======== ============= =================================================================
scalar [] [True, True, True, True ]
vector [n] [True, True, True, False] (vectors are used like row vectors)
row [1, n] [True, True, True, False]
col [m, 1] [True, True, False, True ]
matrix [m, n] [True, True, False, False]
tensor3 [m, n, k] [True, False, False, False]
tensor4 [m, n, k, l] [False, False, False, False]
======== ============= =================================================================
So, if you want the type of a row of 32-bit floats, it is available
as :ref:`theano.tensor.frow <libdoc_tensor_type>`.
If you want a matrix of unsigned 32-bit integers it is available as
:ref:`theano.tensor.imatrix <libdoc_tensor_type>`.
Each of the types described above can be constructed by two methods:
a singular version (e.g., :ref:`dmatrix <libdoc_tensor_creation>`)
and a plural version (:ref:`dmatrices <libdoc_tensor_creation>`).
When called, the singular version takes a single
argument which is the name of the *Variable* we want to make and it
makes a single Variable of that type. The plural version can either take
.. #COMMENT
table generated by
$ python Theano/doc/generate_dtype_tensor_table.py
============ =========== ==== =========== =================================
Constructor dtype ndim shape broadcastable
============ =========== ==== =========== =================================
bscalar int8 0 () ()
bvector int8 1 (?,) (False,)
brow int8 2 (1,?) (True, False)
bcol int8 2 (?,1) (False, True)
bmatrix int8 2 (?,?) (False, False)
btensor3 int8 3 (?,?,?) (False, False, False)
btensor4 int8 4 (?,?,?,?) (False, False, False, False)
wscalar int16 0 () ()
wvector int16 1 (?,) (False,)
wrow int16 2 (1,?) (True, False)
wcol int16 2 (?,1) (False, True)
wmatrix int16 2 (?,?) (False, False)
wtensor3 int16 3 (?,?,?) (False, False, False)
wtensor4 int16 4 (?,?,?,?) (False, False, False, False)
iscalar int32 0 () ()
ivector int32 1 (?,) (False,)
irow int32 2 (1,?) (True, False)
icol int32 2 (?,1) (False, True)
imatrix int32 2 (?,?) (False, False)
itensor3 int32 3 (?,?,?) (False, False, False)
itensor4 int32 4 (?,?,?,?) (False, False, False, False)
lscalar int64 0 () ()
lvector int64 1 (?,) (False,)
lrow int64 2 (1,?) (True, False)
lcol int64 2 (?,1) (False, True)
lmatrix int64 2 (?,?) (False, False)
ltensor3 int64 3 (?,?,?) (False, False, False)
ltensor4 int64 4 (?,?,?,?) (False, False, False, False)
dscalar float64 0 () ()
dvector float64 1 (?,) (False,)
drow float64 2 (1,?) (True, False)
dcol float64 2 (?,1) (False, True)
dmatrix float64 2 (?,?) (False, False)
dtensor3 float64 3 (?,?,?) (False, False, False)
dtensor4 float64 4 (?,?,?,?) (False, False, False, False)
fscalar float32 0 () ()
fvector float32 1 (?,) (False,)
frow float32 2 (1,?) (True, False)
fcol float32 2 (?,1) (False, True)
fmatrix float32 2 (?,?) (False, False)
ftensor3 float32 3 (?,?,?) (False, False, False)
ftensor4 float32 4 (?,?,?,?) (False, False, False, False)
cscalar complex64 0 () ()
cvector complex64 1 (?,) (False,)
crow complex64 2 (1,?) (True, False)
ccol complex64 2 (?,1) (False, True)
cmatrix complex64 2 (?,?) (False, False)
ctensor3 complex64 3 (?,?,?) (False, False, False)
ctensor4 complex64 4 (?,?,?,?) (False, False, False, False)
zscalar complex128 0 () ()
zvector complex128 1 (?,) (False,)
zrow complex128 2 (1,?) (True, False)
zcol complex128 2 (?,1) (False, True)
zmatrix complex128 2 (?,?) (False, False)
ztensor3 complex128 3 (?,?,?) (False, False, False)
ztensor4 complex128 4 (?,?,?,?) (False, False, False, False)
============ =========== ==== =========== =================================
Plural Constructors
--------------------------
There are several constructors that can produce multiple variables at once.
These are not frequently used in practice, but often used in tutorial examples to save space!
.. function:: iscalars, lscalars, fscalars, dscalars
Return one or more scalar variables.
.. function:: ivectors, lvectors, fvectors, dvectors
Return one or more vector variables.
.. function:: irows, lrows, frows, drows
Return one or more row variables.
.. function:: icols, lcols, fcols, dcols
Return one or more col variables.
.. function:: imatrices, lmatrices, fmatrices, dmatrices
Return one or more matrix variables.
Each of these plural constructors accepts
an integer or several strings. If an integer is provided, the method
will return that many Variables and if strings are provided, it will
create one Variable for each string, using the string as the Variable's
...
...
@@ -139,114 +192,281 @@ name. For example:
from theano.tensor import *
x = dmatrix() # creates one Variable with no name
x = dmatrix('x') # creates one Variable with name 'x'
xyz = dmatrix('xyz') # creates one Variable with name 'xyz'
x, y, z = dmatrices(3) # creates three Variables with no names
x, y, z = dmatrices('x', 'y', 'z') # creates three Variables named 'x', 'y' and 'z'
x, y, z = dmatrices(3) # creates three matrix Variables with no names
x, y, z = dmatrices('x', 'y', 'z') # creates three matrix Variables named 'x', 'y' and 'z'
Custom tensor types
-------------------
If you wish to use a type of tensor which is not already available
(for example, a 5D tensor) you can build an appropriate type using
:ref:`theano.tensor.TensorType <libdoc_tensor_type>`.
The first argument you pass is the `dtype` and the second is the
`broadcastable pattern`.
Where `dtype` is one of (a complete list of supported dtypes):
================= =================== =================
dtype domain bits
================= =================== =================
``'int8'`` signed integer 8
``'int16'`` signed integer 16
``'int32'`` signed integer 32
``'int64'`` signed integer 64
``'uint8'`` unsigned integer 8
``'uint16'`` unsigned integer 16
``'uint32'`` unsigned integer 32
``'uint64'`` unsigned integer 64
``'float32'`` floating point 32
``'float64'`` floating point 64
``'complex64'`` complex 64 (two float32)
``'complex128'`` complex 128 (two float64)
================= =================== =================
The broadcastable pattern indicates both the number of dimensions and
whether a particular dimension must have length 1.
Here is a table mapping the :ref:`broadcastable <libdoc_tensor_broadcastable>` pattern to what kind of tensor it encodes:
===================== =================================
pattern interpretation
===================== =================================
[] scalar
[True] 1D scalar (vector of length 1)
[True, True] 2D scalar (1x1 matrix)
[False] vector
[False, False] matrix
[False] * n nD tensor
[True, False] row (1xN matrix)
[False, True] column (Mx1 matrix)
[False, True, False] A Mx1xP tensor (a)
[True, False, False] A 1xNxP tensor (b)
[False, False, False] A MxNxP tensor (pattern of a + b)
===================== =================================
For dimensions in which broadcasting is False, the length of this
dimension can be 1 or more. For dimensions in which broadcasting is True,
the length of this dimension must be 1.
When two tensors have a different number of dimensions, the broadcastable
pattern is *expanded to the left*, by padding with ``True``. For example,
a vector's pattern, ``[False]``, could be expanded to ``[True, False]``, and
would behave like a row (1xN matrix). In the same way, a matrix (``[False,
False]``) would behave like a 1xNxP tensor (``[True, False, False]``).
If we wanted to create a type representing a 3D array of unsigned
bytes, we would do:
If you would like to construct a tensor variable with a non-standard
broadcasting pattern, or a larger number of dimensions you'll need to create
your own :class:`TensorType` instance. You create such an instance by passing
the dtype and broadcasting pattern to the constructor. For example, you
can create your own 5-dimensional tensor type
.. code-block:: python
>>> dtensor5 = TensorType('float64', (False,)*5)
>>> x = dtensor5()
>>> z = dtensor5('z')
You can also redefine some of the provided types and they will interact
correctly:
# 3D tensor of signed bytes
mytype = theano.tensor.TensorType('uint8', [False]*3)
>>> my_dmatrix = TensorType('float64', (False,)*2)
>>> x = my_dmatrix() # allocate a matrix variable
>>> my_dmatrix == dmatrix # this compares True
# complex types (based on complex64)
my_cscalar = theano.tensor.TensorType('complex64', [])
my_cmatrix = theano.tensor.TensorType('complex64', [False, False])
See :class:`TensorType` for more information about creating new types of
Tensor.
Shared Variable
---------------
Converting from Python Objects
---------------
----------------
Another way of creating a TensorVariable (a TensorSharedVariable to be
precise) is by calling :func:`shared()`
.. code-block:: python
x = shared(numpy.random.randn(3,4))
x = shared(numpy.random.randn(3,4))
This will return a :term:`shared variable <shared variable>` whose ``.value`` is
a numpy ndarray. The number of dimensions and dtype of the Variable are
inferred from the ndarray argument.
inferred from the ndarray argument. The argument to `shared` *will not be
copied*, and subsequent changes will be reflected in ``x.value``.
For additional information, see the :func:`shared() <shared.shared>` documentation.
.. _libdoc_tensor_autocasting:
Finally, when you use a numpy ndarry or a Python number together with
:class:`TensorVariable` instances in arithmetic expressions, the result is a
:class:`TensorVariable`. What happens to the ndarray or the number?
Theano requires that the inputs to all expressions be Variable instances, so
Theano automatically wraps them in a :class:`TensorConstant`.
.. note::
For additional information, see the :func:`shared` documentation.
Theano makes a copy of any ndarray that you use in an expression, so
subsequent
changes to that ndarray will not have any effect on the Theano expression.
For numpy ndarrays the dtype is given, but the broadcastable pattern must be
inferred. The TensorConstant is given a type with a matching dtype,
and a broadcastable pattern with a ``True`` for every shape dimension that is 1.
Autocasting
-----------
For python numbers, the broadcastable pattern is ``()`` but the dtype must be
inferred. Python integers are stored in the smallest dtype that can hold
them, so small constants like ``1`` are stored in a ``bscalar``.
Likewise, Python floats are stored in an fscalar if fscalar suffices to hold
them perfectly, but a dscalar otherwise.
Theano does autocasting of python floats/ints into Theano constants.
.. note::
When floatX=float32 is given in :envvar:`THEANO_FLAGS`, then Python floats
are stored instead as single-precision floats.
For fine control of this rounding policy, see
theano.tensor.basic.autocast_float.
.. function:: as_tensor_variable(x, name=None, ndim=None)
.. TODO: What does (or compatible) mean? Talk about casting rules, refer .
.. TODO: link to floatX (?)
Turn an argument `x` into a TensorVariable or TensorConstant.
.. function:: as_tensor_variable(x, ...)
Many tensor Ops run their arguments through this function as
pre-processing. It passes through TensorVariable instances, and tries to
wrap other objects into TensorConstant.
When `x` is a Python number, the dtype is inferred as described above.
When `x` is a `list` or `tuple` it is passed through numpy.asarray
If the `ndim` argument is not None, it must be an integer and the output
will be broadcasted if necessary in order to have this many dimensions.
:rtype: :class:`TensorVariable` or :class:`TensorConstant`
TensorType and TensorVariable
=============================
.. class:: TensorType(Type)
The Type class used to mark Variables that stand for `numpy.ndarray`
values. Recalling to the tutorial, the purple box in
:ref:`the tutorial's graph-structure figure <tutorial-graphfigure>` is an instance of this class.
.. attribute:: broadcastable
A tuple of True/False values, one for each dimension. True in
position 'i' indicates that at evaluation-time, the ndarray will have
size 1 in that 'i'-th dimension. Such a dimension is called a
*broadcastable dimension* (see :ref:`libdoc_tensor_broadcastable`).
The broadcastable pattern indicates both the number of dimensions and
whether a particular dimension must have length 1.
Here is a table mapping some `broadcastable` patterns to what they
mean:
===================== =================================
pattern interpretation
===================== =================================
[] scalar
[True] 1D scalar (vector of length 1)
[True, True] 2D scalar (1x1 matrix)
[False] vector
[False, False] matrix
[False] * n nD tensor
[True, False] row (1xN matrix)
[False, True] column (Mx1 matrix)
[False, True, False] A Mx1xP tensor (a)
[True, False, False] A 1xNxP tensor (b)
[False, False, False] A MxNxP tensor (pattern of a + b)
===================== =================================
For dimensions in which broadcasting is False, the length of this
dimension can be 1 or more. For dimensions in which broadcasting is True,
the length of this dimension must be 1.
When two arguments to an element-wise operation (like addition or
subtraction) have a different
number of dimensions, the broadcastable
pattern is *expanded to the left*, by padding with ``True``. For example,
a vector's pattern, ``[False]``, could be expanded to ``[True, False]``, and
would behave like a row (1xN matrix). In the same way, a matrix (``[False,
False]``) would behave like a 1xNxP tensor (``[True, False, False]``).
If we wanted to create a type representing a matrix that would
broadcast over the middle dimension of a 3-dimensional tensor when
adding them together, we would define it like this:
>>> middle_broadcaster = TensorType('complex64', [False, True, False])
.. attribute:: ndim
The number of dimensions that a Variable's value will have at
evaluation-time. This must be known when we are building the
expression graph.
.. attribute:: dtype
A string indicating
the numerical type of the ndarray for which a Variable of this Type
is standing.
.. _dtype_list:
The dtype attribute of a TensorType instance can be any of the
following strings.
================= =================== =================
dtype domain bits
================= =================== =================
``'int8'`` signed integer 8
``'int16'`` signed integer 16
``'int32'`` signed integer 32
``'int64'`` signed integer 64
``'uint8'`` unsigned integer 8
``'uint16'`` unsigned integer 16
``'uint32'`` unsigned integer 32
``'uint64'`` unsigned integer 64
``'float32'`` floating point 32
``'float64'`` floating point 64
``'complex64'`` complex 64 (two float32)
``'complex128'`` complex 128 (two float64)
================= =================== =================
.. method:: __init__(self, dtype, broadcastable)
If you wish to use a type of tensor which is not already available
(for example, a 5D tensor) you can build an appropriate type by instantiating
:class:`TensorType`.
TensorVariable
----------------
.. class:: TensorVariable(Variable, _tensor_py_operators)
The result of symbolic operations typically have this type.
See :class:`_tensor_py_operators` for most of the attributes and methods
you'll want to call.
.. class:: TensorConstant(Variable, _tensor_py_operators)
Python and numpy numbers are wrapped in this type.
See :class:`_tensor_py_operators` for most of the attributes and methods
you'll want to call.
.. class:: TensorSharedVariable(Variable, _tensor_py_operators)
This type is returned by :func:`shared` when the value to share is a numpy
ndarray.
See :class:`_tensor_py_operators` for most of the attributes and methods
you'll want to call.
.. class:: _tensor_py_operators(object)
This mix-in class adds convenient attributes, methods, and support for Python operators (see :ref:`tensor_operator_support`).
.. attribute:: type
A reference to the :class:`TensorType` instance describing the sort of
values that might be associated with this variable.
.. attribute:: ndim
The number of dimensions of this tensor. Aliased to
:attr:`TensorType.ndim`.
.. attribute:: dtype
The numeric type of this tensor. Aliased to
:attr:`TensorType.dtype`.
.. method:: reshape(shape, ndim=None)
Returns a view of this tensor that has been reshaped as in
numpy.reshape. If the shape is a Variable argument, then you might
need to use the optional `ndim` parameter to declare how many elements
the shape has, and therefore how many dimensions the reshaped Variable
will have.
See :func:`reshape`.
.. method:: dimshuffle(*pattern)
Returns a view of this tensor with permuted dimensions. Typically the
pattern will include the integers 0, 1, ... ndim-1, and any number of
'x' characters in dimensions where this tensor should be broadcasted.
See :func:`dimshuffle`.
.. method:: flatten(ndim=1)
Returns a view of this tensor with `ndim` dimensions, whose shape for the first
`ndim-1` dimensions will be the same as `self`, and shape in the
remaining dimension will be expanded to fit in all the data from self.
See :func:`flatten`.
.. attribute:: T
Transpose of this tensor.
>>> x = T.zmatrix()
>>> y = 3+.2j * x.T
.. note::
In numpy and in Theano, the transpose of a vector is exactly the
same vector! Use `reshape` or `dimshuffle` to turn your vector
into a row or column matrix.
...
...
@@ -254,44 +474,76 @@ Theano does autocasting of python floats/ints into Theano constants.
Shaping and Shuffling
=====================
To re-order the dimensions of a variable, to insert or remove broadcastable
dimensions, see :meth:`_tensor_py_operators.dimshuffle`
.. function:: shape(x)
:param x: symbolic Tensor (or compatible)
Returns lvector representing shape of `x`
.. function:: reshape(x, newshape, ndim=None)
:type x: any TensorVariable (or compatible)
:param x: variable to be reshaped
:type newshape: lvector (or compatible)
:param newshape: the new shape for `x`
:param ndim: optional - the length that `newshape`'s value will have.
If this is ``None``, then `reshape()` will infer it from `newshape`.
:rtype: variable with x's dtype, but ndim dimensions
.. note::
This function can infer the length of a symbolic newshape in some
cases, but if it cannot and you do not provide the `ndim`, then this
function will raise an Exception.
.. function:: flatten(x, outdim=1)
Similar to :func:`reshape`, but the shape is inferred from the shape of `x`.
:param x: variable to be flattened
:type x: any TensorVariable (or compatible)
Returns the symbolic shape vector of `x`
:type outdim: int
:param outdim: the number of dimensions in the returned variable
.. function:: reshape()
:rtype: variable with same dtype as `x` and `outdim` dimensions
:returns: variable with the same shape as `x` in the leading `outdim-1`
dimensions, but with all remaining dimensions of `x` collapsed into
the last dimension.
.. function:: dimshuffle()
For example, if we flatten a tensor of shape (2,3,4,5) with flatten(x,
outdim=2), then we'll have the same (2-1=1) leading dimensions (2,), and the
remaining dimensions are collapsed. So the output in this example would
have shape (2, 60).
.. function:: flatten()
Reductions
==========
.. function:: max(x,axis=None)
.. function:: max(x,
axis=None)
:Parameter: *x* - symbolic Tensor (or compatible)
:Parameter: *axis* - axis along which to compute the maximum
:Returns: the maximum value along a given axis
.. note::
If axis=None, then axis is assumed to be ndim(x)-1
.. function:: min(x,axis=None)
.. function:: min(x,
axis=None)
:Parameter: *x* - symbolic Tensor (or compatible)
:Parameter: *axis* - axis along which to compute the minimum
:Returns: the minimum value along a given axis
.. note::
if axis=None, then axis is assumed to be ndim(x)-1
.. function:: sum(x,axis=None)
.. function:: sum(x,
axis=None)
:Parameter: *x* - symbolic Tensor (or compatible)
:Parameter: *axis* - axis or axes along which to compute the sum
...
...
@@ -302,8 +554,18 @@ Reductions
* an *int* - computed along this axis
* a *list of ints* - computed along these axes
.. function:: prod(x, axis=None)
:Parameter: *x* - symbolic Tensor (or compatible)
:Parameter: *axis* - axis or axes along which to compute the product
:Returns: product of every term in *x* along *axis*
axis can be:
* *None* - in which case the sum is computed along all axes (like numpy)
* an *int* - computed along this axis
* a *list of ints* - computed along these axes
.. function:: mean(x,axis=None)
.. function:: mean(x,
axis=None)
:Parameter: *x* - symbolic Tensor (or compatible)
:Parameter: *axis* - axis or axes along which to compute the mean
...
...
@@ -314,7 +576,7 @@ Reductions
* an *int* - computed along this axis
* a *list of ints* - computed along these axes
.. function:: var(x,axis=None)
.. function:: var(x,
axis=None)
:Parameter: *x* - symbolic Tensor (or compatible)
:Parameter: *axis* - axis or axes along which to compute the variance
...
...
@@ -329,20 +591,24 @@ Reductions
Indexing
========
Basic indexing.
Like numpy, Theano distinguishes between *basic* and *advanced* indexing.
Theano fully supports basic indexing
(see `numpy's basic indexing <http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_).
Mirrors numpy's `basic indexing <http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_. Read that page first.
Advanced indexing is almost entirely unsupported (for now).
The one sort of advanced indexing that is supported is the retrieval of the c[i]'th element of each
row of a matrix x:
Advanced indexing.
>>> x = T.fmatrix()
>>> c = T.lvector()
>>> x[T.arange(c.shape[0]), c]
.. _libdoc_tensor_elementwise:
.. note::
Index-assignment is *not* supported.
Index-assignment is *not* supported.
If you want to do something like ``a[5] = b`` or ``a[5]+=b``, see :func:`setsubtensor`.
If you want to do something like ``a[5] = b`` or ``a[5]+=b``, see :func:`setsubtensor`.
.. _tensor_operator_support:
Operator Support
================
...
...
@@ -356,7 +622,7 @@ Arithmetic
>>> a + 3 # T.add(a, 3) -> itensor3
>>> 3 - a # T.sub(3, a)
>>> a * 3.5 # T.mul(a, 3.5) -> ftensor3 or dtensor3 (depending on
auto
casting)
>>> a * 3.5 # T.mul(a, 3.5) -> ftensor3 or dtensor3 (depending on casting)
>>> 2.2 / a # T.truediv(2.2, a)
>>> 2.2 // a # T.intdiv(2.2, a)
>>> 2.2**a # T.pow(2.2, a)
...
...
@@ -379,9 +645,52 @@ computations. If you would like to update the value of a
:term:`shared variable`, consider using the ``updates`` argument to
:func:`theano.function`.
.. _libdoc_tensor_elementwise:
Elementwise
===========
.. _libdoc_tensor_broadcastable:
Broadcasting in Theano vs. Numpy
--------------------------------
Broadcasting is a mechanism which allows tensors with
different numbers of dimensions to be added or multiplied
together by (virtually) replicating the smaller tensor along
the dimensions that it is lacking.
Broadcasting is the mechanism by which a scalar
may be added to a matrix, a vector to a matrix or a scalar to
a vector.
.. figure:: bcast.png
Broadcasting a row matrix. T and F respectively stand for
True and False and indicate along which dimensions we allow
broadcasting.
If the second argument were a vector, its shape would be
``(2,)`` and its broadcastable pattern ``(F,)``. They would
be automatically expanded to the **left** to match the
dimensions of the matrix (adding ``1`` to the shape and ``T``
to the pattern), resulting in ``(1, 2)`` and ``(T, F)``.
It would then behave just like the example above.
Unlike numpy which does broadcasting dynamically, Theano needs
to know, for any operation which supports broadcasting, which
dimensions will need to be broadcasted. When applicable, this
information is given in the :ref:`type` of a *Variable*.
See also:
* `SciPy documentation about numpy's broadcasting <http://www.scipy.org/EricsBroadcastingDoc>`_
* `OnLamp article about numpy's broadcasting <http://www.onlamp.com/pub/a/python/2000/09/27/numerically.html>`_
Casting
-------
...
...
@@ -515,7 +824,7 @@ Mathematical
.. function:: neg(a)
Returns a variable representing the
opposite of a, ie -a
.
Returns a variable representing the
negation of `a` (also ``-a``)
.
.. function:: inv(a)
...
...
@@ -558,47 +867,6 @@ Mathematical
Returns a variable representing the hyperbolic trigonometric functions of a (hyperbolic cosine, sine and tangent).
.. _libdoc_tensor_broadcastable:
Broadcasting in Theano vs. Numpy
--------------------------------
Broadcasting is a mechanism which allows tensors with
different numbers of dimensions to be added or multiplied
together by (virtually) replicating the smaller tensor along
the dimensions that it is lacking.
In a nutshell, broadcasting is the mechanism by which a scalar
may be added to a matrix, a vector to a matrix or a scalar to
a vector.
.. figure:: bcast.png
Broadcasting a row matrix. T and F respectively stand for
True and False and indicate along which dimensions we allow
broadcasting.
If the second argument were a vector, its shape would be
``(2,)`` and its broadcastable pattern ``(F,)``. They would
be automatically expanded to the **left** to match the
dimensions of the matrix (adding ``1`` to the shape and ``T``
to the pattern), resulting in ``(1, 2)`` and ``(T, F)``.
It would then behave just like the example above.
Unlike numpy which does broadcasting dynamically, Theano needs
to know, for any operation which supports broadcasting, which
dimensions will need to be broadcasted. When applicable, this
information is given in the :ref:`type` of a *Variable*.
See also:
* `SciPy documentation about numpy's broadcasting <http://www.scipy.org/EricsBroadcastingDoc>`_
* `OnLamp article about numpy's broadcasting <http://www.onlamp.com/pub/a/python/2000/09/27/numerically.html>`_
Linear Algebra
==============
...
...
@@ -664,3 +932,8 @@ Gradient / Differentiation
:returns: gradients with respect to cost for each of the `wrt` terms
Full Constructor List
======================
doc/library/tensor/index.txt
浏览文件 @
596f9bca
...
...
@@ -10,8 +10,8 @@
.. moduleauthor:: LISA
Theano's strength is in expressing symbolic calculations involving tensors.
There are many types of symbolic expressions for tensors.
For everyone's
sanity, t
hey are grouped into the following sections:
There are many types of symbolic expressions for tensors.
T
hey are grouped into the following sections:
.. toctree::
...
...
doc/tutorial/adding.txt
浏览文件 @
596f9bca
...
...
@@ -65,7 +65,7 @@ is the type we assign to "0-dimensional arrays (`scalar`) of doubles
``dscalar`` is not a class. Therefore, neither ``x`` nor ``y``
are actually instances of ``dscalar``. They are instances of
:
ref:`TensorVariable <libdoc_tensor_type>
`. ``x`` and ``y``
:
class:`TensorVariable
`. ``x`` and ``y``
are, however, assigned the theano Type ``dscalar`` in their ``type``
field, as you can see here:
...
...
doc/tutorial/symbolic_graphs.txt
浏览文件 @
596f9bca
...
...
@@ -38,11 +38,20 @@ details about these building blocks see :ref:`variable`, :ref:`op`,
**Diagram**
.. _tutorial-graphfigure:
.. figure:: apply.png
:align: center
Interaction between instances of Apply (blue), Variable (red), Op (green),
and Type (purple).
.. # COMMENT
WARNING: hyper-links and ref's seem to break the PDF build when placed
into this figure caption.
Arrows represent references to the Python objects pointed at. The blue
Arrows in this :ref:`figure <tutorial-graphfigure>` represent references to the
Python objects pointed at. The blue
box is an :ref:`apply` node. Red boxes are :ref:`variable` nodes. Green
circles are :ref:`Ops <op>`. Purple boxes are :ref:`Types <type>`.
...
...
theano/tensor/basic.py
浏览文件 @
596f9bca
...
...
@@ -737,6 +737,11 @@ btensor3 = TensorType('int8', (False,)*3)
wtensor3
=
TensorType
(
'int16'
,
(
False
,)
*
3
)
itensor3
=
TensorType
(
'int32'
,
(
False
,)
*
3
)
ltensor3
=
TensorType
(
'int64'
,
(
False
,)
*
3
)
def
tensor3
(
name
=
None
,
dtype
=
'float64'
):
type
=
TensorType
(
dtype
,
(
False
,
False
,
False
))
return
type
(
name
)
tensor3s
,
ftensor3s
,
dtensor3s
,
itensor3s
,
ltensor3s
=
_multi
(
tensor3
,
ftensor3
,
dtensor3
,
itensor3
,
ltensor3
)
ctensor4
=
TensorType
(
'complex64'
,
(
False
,)
*
4
)
ztensor4
=
TensorType
(
'complex128'
,
(
False
,)
*
4
)
...
...
@@ -746,6 +751,11 @@ btensor4 = TensorType('int8', (False,)*4)
wtensor4
=
TensorType
(
'int16'
,
(
False
,)
*
4
)
itensor4
=
TensorType
(
'int32'
,
(
False
,)
*
4
)
ltensor4
=
TensorType
(
'int64'
,
(
False
,)
*
4
)
def
tensor4
(
name
=
None
,
dtype
=
'float64'
):
type
=
TensorType
(
dtype
,
(
False
,
False
,
False
,
False
))
return
type
(
name
)
tensor4s
,
ftensor4s
,
dtensor4s
,
itensor4s
,
ltensor4s
=
_multi
(
tensor4
,
ftensor4
,
dtensor4
,
itensor4
,
ltensor4
)
class
_tensor_py_operators
:
#UNARY
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论