提交 5b6e5783 authored 作者: Pierre-Antoine Manzagol's avatar Pierre-Antoine Manzagol

Merge.

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%-10s %-10s %-4s %-10s %-20s' %(
letter[0], shape[0], letter[1], len(shape[1]), s, shape[1]
)
print hdr
.. 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 autocasting)
>>> 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
======================
......@@ -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, they are grouped into the following sections:
There are many types of symbolic expressions for tensors.
They are grouped into the following sections:
.. toctree::
......
......@@ -23,79 +23,8 @@ put random variables in your graph. Theano will allocate a numpy RandomState
object for each such variable, and draw from it as necessary. I'll call this sort of sequence of
random numbers a *random stream*.
Brief example
-------------
Here's a brief example. The setup code is:
.. code-block:: python
from theano.tensor.shared_randomstreams import RandomStreams
srng = RandomStreams(seed=234)
rv_u = srng.uniform((2,2))
rv_n = srng.normal((2,2))
f = function([], rv_u, updates=[rv_u.update])
g = function([], rv_n) #omitting rv_n.update
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u, updates=[rv_u.update])
Here, 'rv_u' represents a random stream of 2x2 matrices of draws from a uniform
distribution. Likewise, 'rv_n' represenents a random stream of 2x2 matrices of
draws from a normal distribution. The distributions that are implemented are
defined in :class:`RandomStreams`.
Now let's use these things. If we call f(), we get random uniform numbers.
Since we are updating the internal state of the random number generator (via
the ``updates`` argument, we get different random numbers every time.
>>> f_val0 = f()
>>> f_val1 = f() #different numbers from f_val0
When we omit the updates argument (as in ``g``) to ``function``, then the
random number generator state is not affected by calling the returned function. So for example,
calling ``g`` multiple times will return the same numbers.
>>> g_val0 = g() # different numbers from f_val0 and f_val1
>>> g_val0 = g() # same numbers as g_val0 !!!
An important remark is that a random variable is drawn at most once during any
single function execution. So the ``nearly_zeros`` function is guaranteed to
return approximately 0 (except for rounding error) even though the ``rv_u``
random variable appears three times in the output expression.
>>> nearly_zeros = function([], rv_u + rv_u - 2 * rv_u, updates=[rv_u.update])
Seedings Streams
----------------
Random variables can be seeded individually or collectively.
You can seed just one random variable by seeding or assigning to the
``.rng.value`` attribute.
>>> rv_u.rng.value.seed(89234) # seeds the generator for rv_u
You can also seed *all* of the random variables allocated by a :class:`RandomStreams`
object by that object's ``seed`` method. This seed will be used to seed a
temporary random number generator, that will in turn generate seeds for each
of the random variables.
>>> srng.seed(902340) # seeds rv_u and rv_n with different seeds each
Sharing Streams between Functions
---------------------------------
As usual for shared variables, the random number generators used for random
variables are common between functions. So our ``nearly_zeros`` function will
update the state of the generators used in function ``f`` above.
For example:
>>> state_after_v0 = rv_u.rng.value.get_state()
>>> nearly_zeros() # this affects rv_u's generator
>>> v1 = f()
>>> rv_u.rng.value.set_state(state_after_v0)
>>> v2 = f() # v2 != v1
For an example of how to use random numbers, see
:ref:`using_random_numbers`.
Reference
......
......@@ -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:
......
......@@ -289,6 +289,7 @@ careful though, not to allow the expressions introduced by a givens
substitution to be co-dependent, the order of substitution is not defined, so
the substitutions have to work in any order.
.. _using_random_numbers:
Using Random Numbers
====================
......@@ -376,7 +377,7 @@ For example:
>>> state_after_v0 = rv_u.rng.value.get_state()
>>> nearly_zeros() # this affects rv_u's generator
>>> v1 = f()
>>> v1 = f()
>>> rv_u.rng.value.set_state(state_after_v0)
>>> v2 = f() # v2 != v1
......
......@@ -5,29 +5,148 @@
Loading and Saving
==================
Many Theano objects can be serialized. However, you will want to consider different mechanisms
depending on the amount of time you anticipate between saving and reloading. For short-term
(such as temp files and network transfers) pickling is possible. For longer-term (such as
saving models from an experiment) you should not rely on pickled theano objects; we recommend
loading and saving the underlying shared objects as you would in the course of any other python
program.
Python's standard way of saving class instances and reloading them
is the pickle_ mechanism. Many Theano objects can be serialized (and
deserialized) by ``pickle``, however, a limitation of ``pickle`` is that
it does not save the code or data of a class along with the instance of
the class being serialized. As a result, reloading objects created by a
previous version of a class can be really problematic.
pickling -- Short-term serialization
=====================================
Thus, you will want to consider different mechanisms depending on
the amount of time you anticipate between saving and reloading. For
short-term (such as temp files and network transfers), pickling of
the Theano objects or classes is possible. For longer-term (such as
saving models from an experiment) you should not rely on pickled Theano
objects; we recommend loading and saving the underlying shared objects
as you would in the course of any other Python program.
Pickling and unpickling of functions. Caveats... basically don't do this for long-term storage.
***TODO***
.. _pickle: http://docs.python.org/library/pickle.html
not-pickling -- Long-term serialization
=======================================
***TODO***
The basics of pickling
======================
Give a short example of how to add a __getstate__ and __setstate__ to a class. Point out to
use protocol=-1 for numpy ndarrays.
The two modules ``pickle`` and ``cPickle`` have the same functionalities, but
``cPickle``, coded in C, is much faster.
Point to the python docs for further reading.
>>> import cPickle
You can serialize (or *save*, or *pickle*) objects to a file with
``cPickle.dump``:
>>> f = file('obj.save', 'wb')
>>> cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
>>> f.close()
.. note::
If you want your saved object to be stored efficiently, don't forget
to use ``cPickle.HIGHEST_PROTOCOL``, the resulting file can be
dozens of times smaller than with the default protocol.
.. note::
Opening your file in binary mode (``'b'``) is required for portability
(especially between Unix and Windows).
To de-serialize (or *load*, or *unpickle*) a pickled file, use
``cPickle.load``:
>>> f = file('obj.save', 'rb')
>>> loaded_obj = cPickle.load(f)
>>> f.close()
You can pickle several objects into the same file, and load them all (in the
same order):
>>> f = file('objects.save', 'wb')
>>> for obj in [obj1, obj2, obj3]:
>>> cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
>>> f.close()
Then:
>>> f = file('objects.save', 'rb')
>>> loaded_objects = []
>>> for i in range(3):
>>> loaded_objects.append(cPickle.load(f))
>>> f.close()
For more details about pickle's usage, see
`Python documentation <http://docs.python.org/library/pickle.html#usage>`_.
Short-term serialization
========================
If you are confident that the class instance you are serializing will be
deserialized by a compatible version of the code, pickling the whole model is
an adequate solution. It would be the cas, for instance, if you are saving
models and reloading them during the same execution of your program, or if the
class you're saving has been really stable for a while.
You can control what pickle will save from your object, by defining a
`__getstate__
<http://docs.python.org/library/pickle.html#object.__getstate__>`_ method,
and similarly `__setstate__
<http://docs.python.org/library/pickle.html#object.__getstate__>`_.
This will be especially useful if, for instance, your model class contains a
link to the data set currently in use, that you probably don't want to pickle
along every instance of your model.
For instance, you can define functions along the lines of:
.. code-block:: python
def __getstate__(self):
state = dict(self.__dict__)
del state['training_set']
return state
def __setstate__(self, d):
self.__dict__.update(d)
self.training_set = cPickle.load(file(self.training_set_file, 'rb'))
Long-term serialization
=======================
If the implementation of the class you want to save is quite unstable, for
instance if functions are created or removed, class members are renamed, you
should save and load only the immutable (and necessary) part of your class.
You can do that by defining __getstate__ and __setstate__ functions as above,
maybe defining the attributes you want to save, rather than the ones you
don't.
For instance, if the only parameters you want to save are a weight
matrix ``W`` and a bias ``b``, you can define:
.. code-block:: python
def __getstate__(self):
return (W, b)
def __setstate__(self, (W,b)):
self.W = W
self.b = b
If, at some point in time, ``W`` is renamed to ``weights`` and ``b`` to
``bias``, the older pickled files will still be usable, if you update these
functions to reflect the change in name:
.. code-block:: python
def __getstate__(self):
return (weights, bias)
def __setstate__(self, (W,b)):
self.weights = W
self.bias = b
For more information on advanced use of pickle and its internals, see Python's
pickle_ documentation.
......@@ -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>`.
......
......@@ -149,9 +149,6 @@ def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
clone_d.setdefault(old_o, new_o)
return clone_d[a]
#def v_clone(v):
# return _v_clone(v, clone_d)
# initialize the clone_d mapping with the `givens` argument
try:
givens = givens.items() # converts a dictionary to the sort of list that we want.
......@@ -173,8 +170,6 @@ def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
for i, iv in zip(inputs, input_variables):
i.variable = iv
#set_of_param_variables = set(input_variables)
# It was decided, as a first step, to prevent shared variables from being
# used as function inputs. Although it is technically possible, it is also not clear
# when/how to use the value of that shared variable (is it a default? ignored?, if the
......@@ -200,10 +195,6 @@ def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
update_d[store_into] = update_val
update_expr.append((store_into, update_val))
# computed_list is a list of output variables (which will be extended later)
#computed_list = []
# Elements of "outputs" are here cloned to "cloned_outputs"
if isinstance(outputs, list):
cloned_outputs = []
......@@ -246,7 +237,6 @@ def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
shared_inputs.append(v)
i += 1
#updates = update_d #?
for sv in shared_inputs:
if sv in update_d:
si = In(variable=sv, value=sv.container, mutable=True,
......@@ -258,64 +248,6 @@ def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
return orig_function(inputs, cloned_outputs, mode,
accept_inplace=accept_inplace, name=name)
if 0:
# Add update values as quantities that must be computed.
# Here, we
# - extend the computed_list
# - replace some update expressions (but update keys remain)
new_updates = {}
for (store_into, update_val) in iter_over_pairs(updates):
if not isinstance(store_into, SharedVariable):
raise TypeError('update target must be a SharedVariable', store_into)
if store_into in new_updates:
raise ValueError('this shared variable already has an update expression',
(store_into, new_updates[store_into]))
update_val = v_clone(store_into.filter_update(update_val))
if update_val.type != store_into.type:
raise TypeError('an update must have the same type as the original shared variable',
(store_into, store_into.type,
update_val, update_val.type))
computed_list.append(update_val)
new_updates[store_into] = update_val
updates = new_updates
# Obtain all inputs we need to compute what we want.
graph_inputs = graph.inputs(computed_list,
blockers=set_of_param_variables)
shared_inputs = [i for i in graph_inputs if isinstance(i, SharedVariable)]
# Add shared variables (from shared_inputs) that were not already present in the list of
# params.
inputs += [In(variable=si, value=si.container, mutable=False)
for si in shared_inputs
if si not in set_of_param_variables]
del shared_inputs
# Iterate over the updates, which are either pairs
# (shared_var, expressionvariable), or a similar dictionary.
# For each shared_variable, find the In instance that we created for it in the inputs list.
# Give that In instance (in_sv) an update expression.
#
# I think we usually want to set these Inputs to be mutable,
# ... are there exceptions?
for (sv, new_val) in iter_over_pairs(updates):
in_sv = None
for in_sv_i in inputs:
if in_sv_i.variable is sv:
assert in_sv is None
in_sv = in_sv_i
if in_sv is None:
# This variable was not used anywhere and thus is not in the input
# list yet.
inputs.append(In(variable=sv, value=sv.container, mutable=True,
update=new_val))
else:
in_sv.update = new_val
in_sv.mutable = True
return orig_function(inputs, cloned_outputs, mode, accept_inplace=accept_inplace,name=name)
def _pfunc_param_to_in(param):
if isinstance(param, Constant):
......@@ -354,22 +286,3 @@ def iter_over_pairs(pairs):
else:
return pairs
#TODO: Make these non-recursive so they can deal with larger graphs
def _a_clone(a, dct):
if a is None:
return None
if a not in dct:
for i in a.inputs:
_v_clone(i, dct)
dct[a] = a.clone_with_new_inputs([dct[i] for i in a.inputs])
for old_o, new_o in zip(a.outputs, dct[a].outputs):
dct.setdefault(old_o, new_o)
return dct[a]
def _v_clone(v, dct):
assert v is not None
if v.owner:
_a_clone(v.owner, dct)
return dct.setdefault(v, v)
......@@ -18,6 +18,7 @@ except ImportError:
import theano.sandbox.cuda as tcn
import cuda_ndarray as cuda
import theano.compile.mode
from theano.tests import unittest_tools as utt
mode_with_gpu = theano.compile.mode.get_default_mode().including('gpu')
......@@ -81,6 +82,41 @@ def test_sum():
assert numpy.allclose(f2(val2),f(val))
def test_reshape():
a = tcn.CudaNdarrayType((False,))()
b = tcn.CudaNdarrayType((False,False))()
c = T.reshape(a, [2,3])
#basic
f = theano.function([a], c)
fv = f(cuda_ndarray.CudaNdarray(numpy.asarray([0,1,2,3,4,5],dtype='float32')))
import pdb;pdb.set_trace()
assert numpy.all(fv == numpy.asarray([[0,1,2], [3,4,5]]))
#test that it works without inplace operations
a_val = cuda_ndarray.CudaNdarray(numpy.asarray([0,1,2,3,4,5],dtype='float32'))
a_val_copy = cuda_ndarray.CudaNdarray(numpy.asarray([0,1,2,3,4,5],dtype='float32'))
b_val = cuda_ndarray.CudaNdarray(numpy.asarray([[0,1,2],[3,4,5]],dtype='float32'))
f_sub = theano.function([a,b], c-b)
assert numpy.all(f_sub(a_val, b_val) == 0.0)
assert numpy.all(numpy.asarray(a_val) == numpy.asarray(a_val_copy))
#test that it works with inplace operations
a_val = numpy.asarray([0,1,2,3,4,5], dtype='float32')
a_val_copy = numpy.asarray([0,1,2,3,4,5], dtype='float32')
b_val = numpy.asarray([[0,1,2],[3,4,5]], dtype='float32')
f_sub = theano.function([a,b], c-b)
assert numpy.all(f_sub(a_val, b_val) == 0.0)
assert numpy.all(numpy.asarray(a_val) == numpy.asarray(a_val_copy))
# verify gradient
def just_vals(v):
return T.Reshape(2)(v, numpy.asarray([2,3], dtype='int32'))
utt.verify_grad(just_vals, [a_val])
def test_elemwise0():
a = tcn.shared_constructor(numpy.random.rand(4,4), 'a')
......
......@@ -12,6 +12,7 @@ import numpy
import __builtin__
class DownsampleFactorMaxGrad(Op):
def __init__(self, ds, ignore_border):
self.ds = tuple(ds)
self.ignore_border = ignore_border
......@@ -147,10 +148,48 @@ class DownsampleFactorMaxGrad(Op):
return ()
def max_pool2D(input, ds, ignore_border=False):
"""
Takes as input a N-D tensor, where N >= 2. It downscales the input image by
the specified factor, by keeping only the maximum value of non-overlapping
patches of size (ds[0],ds[1])
:type input: N-D theano tensor of input images.
:param input: input images. Max pooling will be done over the 2 last dimensions.
:type ds: tuple of length 2
:param ds: factor by which to downscale. (2,2) will halve the image in each
dimension.
:param ignore_border: boolean value. When True, (5,5) input with ds=(2,2)
will generate a (2,2) output. (3,3) otherwise.
"""
if input.ndim < 2:
raise NotImplementedError('max_pool2D requires a dimension >= 2')
# extract image dimensions
img_shape = input.shape[-2:]
# count the number of "leading" dimensions, store as dmatrix
batch_size = tensor.prod(input.shape[:-2])
batch_size = tensor.shape_padright(batch_size,1)
# store as 4D tensor with shape: (batch_size,1,height,width)
new_shape = tensor.cast(tensor.join(0, batch_size,
tensor.as_tensor([1,]), img_shape), 'int64')
input_4D = tensor.reshape(input, new_shape, ndim=4)
# downsample mini-batch of images
op = DownsampleFactorMax(ds, ignore_border)
output = op(input_4D)
# restore to original shape
outshp = tensor.join(0, input.shape[:-2], output.shape[-2:])
return tensor.reshape(output, outshp, ndim=input.ndim)
class DownsampleFactorMax(Op):
"""
For N-dimensional tensors, consider that the last two dimensions span images.
This Op downsamples these images by taking the max over non-overlapping rectangular regions.
For N-dimensional tensors, consider that the last two dimensions span images.
This Op downsamples these images by a factor ds, by taking the max over non-
overlapping rectangular regions.
"""
@staticmethod
......@@ -192,6 +231,8 @@ class DownsampleFactorMax(Op):
:param ignore_border: if ds doesn't divide imgshape, do we include an extra row/col of
partial downsampling (False) or ignore it (True).
:type ignore_border: bool
TODO: why is poolsize an op parameter here?
"""
self.ds = tuple(ds)
self.ignore_border = ignore_border
......
......@@ -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
......@@ -1666,6 +1676,10 @@ def sum(input, axis = None):
pprint.assign(Sum(), printing.FunctionPrinter('sum'))
@constructor
def prod(input, axis = None):
"""WRITEME"""
return elemwise.Prod(axis)(input)
@constructor
def mean(input, axis = None):
......
......@@ -933,6 +933,9 @@ class Sum(CAReduce):
int8='int32',
int16='int32',
int32='int64',
uint8='uint32',
uint16='uint32',
uint32='uint64',
).get(idtype, idtype)
def grad(self, (x, ), (gz, )):
......@@ -958,4 +961,38 @@ class Sum(CAReduce):
else:
return "Sum{%s}" % ", ".join(map(str, self.axis))
class Prod(CAReduce):
"""
Multiplies all the values of a tensor along the specified axis(es).
Equivalent to CAReduce(scalar.prod, axis = axis), with the
difference that this defines the gradient of prod wrt its tensor
input.
"""
def __init__(self, axis = None):
CAReduce.__init__(self, scalar.mul, axis)
def _output_dtype(self, idtype):
# we want to protect against overflow
return dict(
int8='int64',
int16='int64',
int32='int64',
uint8='uint64',
uint16='uint64',
uint32='uint64',
).get(idtype, idtype)
def grad(self, (x, ), (gz, )):
if x.dtype[0:3] in ('int','uin'):
return [None]
else:
raise NotImplementedError('Will be implemented shortly')
def __str__(self):
if self.axis is None:
return "Prod"
else:
return "Prod{%s}" % ", ".join(map(str, self.axis))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论