提交 a96d5716 authored 作者: lamblin's avatar lamblin

Merge pull request #611 from dwf/default_argument_fix

Default argument fix
......@@ -15,7 +15,13 @@ Bug fixes
Note: set_subtensor(x[slice[,...]], new_value) was working correctly
in all case as well as inc_subtensor(*, *).
Note2: If your code have this behavior, we print a warning by default.
(Frederic B.)
(Frederic B.)
* Fixed an issue whereby config values were used as default arguments,
with those defaults then stuck at old values if the config variables were
changed during program execution. (David W-F)
* Fixed many subtle bugs involving mutable default arguments which may have
led to unexpected behaviour, such as objects sharing instance variables
they were not supposed to share. (David W-F)
Documentation
* Added in the tutorial documentation on how to extend Theano.
......
......@@ -46,7 +46,7 @@ Reference
.. method:: __init__(variable, borrow=False)
Initialize attributes from arguments.
Initialize attributes from arguments.
.. class:: Param
......@@ -57,21 +57,21 @@ Reference
A variable in an expression graph to use as a compiled-function parameter
.. attribute:: default
The default value to use at call-time (can also be a Container where
the function will find a value at call-time.)
.. attribute:: name
.. attribute:: name
A string to identify an argument for this parameter in keyword arguments.
.. attribute:: mutable
``True`` means the compiled-function is allowed to modify this
argument. ``False`` means it is not allowed.
.. attribute:: strict
If ``False``, a function argument may be copied or cast to match the type
required by the parameter `variable`. If ``True``, a function argument
must exactly match the type required by `variable`.
......@@ -81,7 +81,7 @@ Reference
Initialize object attributes.
.. function:: function(inputs, outputs, mode=None, updates=[], givens=[], accept_inplace=False, name=None)
.. function:: function(inputs, outputs, mode=None, updates=None, givens=None, accept_inplace=False, name=None)
Return a callable object that will calculate `outputs` from `inputs`.
......
......@@ -11,7 +11,7 @@ symbolic matrix expressions. When you type,
>>> x = T.fmatrix()
the ``x`` is a :class:`TensorVariable` instance.
The ``T.fmatrix`` object itself is an instance of :class:`TensorType`.
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``.
......@@ -98,71 +98,71 @@ They are all callable, and accept an optional ``name`` argument. So for example
xyz = dmatrix('xyz') # creates one Variable with name 'xyz'
.. #COMMENT
table generated by
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)
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)
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)
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)
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)
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)
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)
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)
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
Plural Constructors
--------------------------
There are several constructors that can produce multiple variables at once.
......@@ -188,7 +188,7 @@ These are not frequently used in practice, but often used in tutorial examples t
Return one or more matrix variables.
Each of these plural constructors accepts
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
......@@ -251,7 +251,7 @@ Finally, when you use a numpy ndarry or a Python number together with
Theano requires that the inputs to all expressions be Variable instances, so
Theano automatically wraps them in a :class:`TensorConstant`.
.. note::
.. note::
Theano makes a copy of any ndarray that you use in an expression, so
subsequent
......@@ -288,7 +288,7 @@ them perfectly, but a dscalar otherwise.
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.
will be broadcasted if necessary in order to have this many dimensions.
:rtype: :class:`TensorVariable` or :class:`TensorConstant`
......@@ -304,7 +304,7 @@ TensorType and TensorVariable
: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
......@@ -349,7 +349,7 @@ TensorType and TensorVariable
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
......@@ -362,7 +362,7 @@ TensorType and TensorVariable
the numerical type of the ndarray for which a Variable of this Type
is standing.
.. _dtype_list:
.. _dtype_list:
The dtype attribute of a TensorType instance can be any of the
following strings.
......@@ -386,7 +386,7 @@ TensorType and TensorVariable
.. method:: __init__(self, dtype, broadcastable)
If you wish to use a type of tensor which is not already available
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`.
......@@ -433,7 +433,7 @@ TensorVariable
.. attribute:: dtype
The numeric type of this tensor. Aliased to
The numeric type of this tensor. Aliased to
:attr:`TensorType.dtype`.
.. method:: reshape(shape, ndim=None)
......@@ -476,10 +476,10 @@ TensorVariable
Transpose of this tensor.
>>> x = T.zmatrix()
>>> y = 3+.2j * x.T
>>> 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.
......@@ -510,20 +510,20 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
:rtype: variable with x's dtype, but ndim dimensions
.. note::
.. 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:: shape_padleft(x, n_ones=1)
Reshape `x` by left padding the shape with `n_ones` 1s. Note that all
this new dimension will be broadcastable. To make them non-broadcastable
Reshape `x` by left padding the shape with `n_ones` 1s. Note that all
this new dimension will be broadcastable. To make them non-broadcastable
see the :func:`unbroadcast`.
:param x: variable to be reshaped
:param x: variable to be reshaped
:type x: any TensorVariable (or compatible)
:type n_ones: int
......@@ -533,11 +533,11 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
.. function:: shape_padright(x,n_ones = 1)
Reshape `x` by right padding the shape with `n_ones` 1s. Note that all
this new dimension will be broadcastable. To make them non-broadcastable
Reshape `x` by right padding the shape with `n_ones` 1s. Note that all
this new dimension will be broadcastable. To make them non-broadcastable
see the :func:`unbroadcast`.
:param x: variable to be reshaped
:param x: variable to be reshaped
:type x: any TensorVariable (or compatible)
:type n_ones: int
......@@ -557,7 +557,7 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
along that dimension is not 1, a ``ValueError`` will be raised.
.. 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
......@@ -591,7 +591,7 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
.. function:: fill(a,b)
:param a: tensor that has same shape as output
:param a: tensor that has same shape as output
:param b: theano scalar or value with which you want to fill the output
Create a matrix by filling the shape of `a` with `b`
......@@ -600,9 +600,9 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
:param n: number of rows in output (value or theano scalar)
:param m: number of columns in output (value or theano scalar)
:param k: Index of the diagonal: 0 refers to the main diagonal,
a positive value refers to an upper diagonal, and a
negative value to a lower diagonal. It can be a theano
:param k: Index of the diagonal: 0 refers to the main diagonal,
a positive value refers to an upper diagonal, and a
negative value to a lower diagonal. It can be a theano
scalar.
:returns: An array where all elements are equal to zero, except for the `k`-th
diagonal, whose values are equal to one.
......@@ -644,7 +644,7 @@ dimensions, see :meth:`_tensor_py_operators.dimshuffle`.
>>> # x.ndim == 2
Reductions
Reductions
==========
......@@ -654,7 +654,7 @@ Reductions
:Parameter: *axis* - axis along which to compute the maximum
:Returns: the maximum value along a given axis
:note: see maximum for elemwise max
if axis=None, Theano 0.5rc1 or later: max over the flattened tensor (like numpy)
older: then axis is assumed to be ndim(x)-1
......@@ -834,7 +834,7 @@ Casting
Cast any tensor `x` to a Tensor of the same shape, but with a different
numerical type `dtype`.
This is not a reinterpret cast, but a coersion cast, similar to
``numpy.asarray(x, dtype=dtype)``.
......@@ -850,12 +850,12 @@ Casting
.. function:: real(x)
Return the real (not imaginary) components of Tensor x.
Return the real (not imaginary) components of Tensor x.
For non-complex `x` this function returns x.
.. function:: imag(x)
Return the imaginary components of Tensor x.
Return the imaginary components of Tensor x.
For non-complex `x` this function returns zeros_like(x).
......@@ -875,7 +875,7 @@ The six usual equality and inequality operators share the same interface.
Here is an example with the less-than operator.
.. code-block:: python
.. code-block:: python
import theano.tensor as T
x,y = T.dmatrices('x','y')
......@@ -927,7 +927,7 @@ Condition
:Parameter: *iff* - symbolic Tensor (or compatible)
:Return type: symbolic Tensor
.. code-block:: python
.. code-block:: python
import theano.tensor as T
a,b = T.dmatrices('a','b')
......@@ -945,14 +945,14 @@ Bit-wise
--------
The bitwise operators possess this interface:
The bitwise operators possess this interface:
:Parameter: *a* - symbolic Tensor of integer type.
:Parameter: *b* - symbolic Tensor of integer type.
.. note::
.. note::
The bitwise operators must have an integer type as input.
The bit-wise not (invert) takes only one parameter.
:Return type: symbolic Tensor with corresponding dtype.
......@@ -991,7 +991,7 @@ The bitwise operators possess this interface:
Here is an example using the bit-wise ``and_`` via the ``&`` operator:
.. code-block:: python
.. code-block:: python
import theano.tensor as T
x,y = T.imatrices('x','y')
......@@ -1137,7 +1137,7 @@ Linear Algebra
:param Y: right term
:type X: symbolic vector
:type Y: symbolic vector
:rtype: symbolic matrix
:rtype: symbolic matrix
:return: vector-vector outer product
......@@ -1150,7 +1150,7 @@ Linear Algebra
:param axes: sum out these axes from X and Y.
:type X: symbolic tensor
:type Y: symbolic tensor
:rtype: symbolic tensor
:rtype: symbolic tensor
:type axes: see numpy.tensordot
:return: tensor product
......@@ -1160,7 +1160,7 @@ Linear Algebra
Gradient / Differentiation
==========================
.. function:: grad(cost, wrt, g_cost=None, consider_constant=[], warn_type=False)
.. function:: grad(cost, wrt, g_cost=None, consider_constant=None, warn_type=False)
Return symbolic gradients for one or more variables with respect to some
cost.
......
......@@ -37,7 +37,7 @@ In this way, we could express something like Logistic Regression like this:
def sample(self, n):
"""[Symbolically] draw a sample of size n"""
def density(self, pt, givens={}):
def density(self, pt, givens=None):
"""Conditional Density/Probability of P(self=pt)
Implicitly conditioned on knowing the values of all variables
......@@ -48,7 +48,7 @@ In this way, we could express something like Logistic Regression like this:
def mode(self):
"""Return expression of the most likely value of this distribution"""
We would really like to integrate out certain variables sometimes...
We would really like to integrate out certain variables sometimes...
An RBM could be expressed like this:
......@@ -71,7 +71,7 @@ An RBM could be expressed like this:
RBM.hidden.energy(h) # an expression for the free energy
v_given_h = RBM.visible.conditional(h) # a random variable
Rather than program all the training algorithms into an RBM module,
Rather than program all the training algorithms into an RBM module,
the idea would be to express the relationship between RBM variables so that we
could automatically recognize how to do Gibbs sampling, gradient descent on Free
Energy, etc.
......
......@@ -13,7 +13,7 @@ changes are proposed to make function-construction calls more
readable and intuitive, and to make it easier to share values between
functions.
The strategy is to
The strategy is to
- introduce a new kind of ``Variable`` (``SharedVariable``) that has a container
associated with it, and can allow multiple functions to share a value.
......@@ -59,17 +59,17 @@ The proposal is for two new ways of creating a *shared* variable:
def __init__(self, name, type, value, strict):
"""
:param name: The name for this variable (see `Variable`).
:param type: The type for this variable (see `Variable`).
:param value: A value to associate with this variable (a new container will be created).
:param strict: True -> assignments to .value will not be cast or copied, so they must
have the correct type.
:param container: The container to use for this variable. Illegal to pass this as well
as a value.
For more user-friendly constructor, see `shared`
"""
......@@ -79,23 +79,23 @@ The proposal is for two new ways of creating a *shared* variable:
value = property(...)
"""Read/write the non-symbolic value associated with this SharedVariable.
If the SharedVariable is shared, changes to this value will be visible to all functions using
this SharedVariable. If this SharedVariable is not shared, a change will not be visible to
functions that were created before the change.
"""
def shared(value, name=None, strict=False, **kwargs):
"""Return a SharedVariable Variable, initialized with a copy or reference of `value`.
This function iterates over constructor functions (see `shared_constructor`) to find a
suitable SharedVariable subclass.
:note:
:note:
By passing kwargs, you effectively limit the set of potential constructors to those that
can accept those kwargs.
"""
...
......@@ -149,25 +149,25 @@ Corner cases and exotic examples can be found in the tests.
.. code-block:: python
def pfunc(params, outputs, mode=None, givens={}, updates=[])
def pfunc(params, outputs, mode=None, givens=None, updates=None)
"""Function-constructor for graphs with shared variables.
:type params: list of either Variable or Param instances.
:param params: function parameters, these are not allowed to be shared
variables
:type outputs: list of Variables or Out instances
:param outputs: expressions to compute
:param mode: compilation mode
:type updates: iterable over pairs (shared_variable, new_expression). List, tuple or dict.
:param updates: update the values for SharedVariable inputs according to these expressions
:rtype: theano.compile.Function
:returns: a callable object that will compute the outputs (given the inputs)
and update the implicit function arguments according to the `updates`.
"""
...
......@@ -177,20 +177,20 @@ Corner cases and exotic examples can be found in the tests.
def __init__(self, variable, default=None, mutable=False, strict=False):
"""
:param variable: A node in an expression graph to set with each function call.
:param default: The default value to use at call-time (can also be a Container where
the function will find a value at call-time.)
:param name: A string to identify this parameter from function kwargs.
:param mutable: True -> function is allowed to modify this argument.
:param strict: False -> function arguments may be copied or cast to match the
type required by the parameter `variable`. True -> function arguments must exactly match the type
required by `variable`.
:param implicit: see help(theano.io.In)
"""
Note that if some update value is not a variable, it will be cast into
......@@ -210,40 +210,40 @@ simple one.
import numpy, theano
from pfunc import pfunc
from sharedvalue import shared
from theano import tensor
from theano.tensor.nnet import sigmoid
class NNet(object):
def __init__(self,
def __init__(self,
input = tensor.dvector('input'),
target = tensor.dvector('target'),
n_input=1, n_hidden=1, n_output=1, lr=1e-3, **kw):
super(NNet, self).__init__(**kw)
self.input = input
self.target = target
self.lr = shared(lr, 'learning_rate')
self.w1 = shared(numpy.zeros((n_hidden, n_input)), 'w1')
self.w2 = shared(numpy.zeros((n_output, n_hidden)), 'w2')
self.hidden = sigmoid(tensor.dot(self.w1, self.input))
self.output = tensor.dot(self.w2, self.hidden)
self.cost = tensor.sum((self.output - self.target)**2)
self.sgd_updates = {
self.w1: self.w1 - self.lr * tensor.grad(self.cost, self.w1),
self.w2: self.w2 - self.lr * tensor.grad(self.cost, self.w2)}
self.sgd_step = pfunc(
params = [self.input, self.target],
outputs = [self.output, self.cost],
updates = self.sgd_updates)
self.compute_output = pfunc([self.input], self.output)
self.output_from_hidden = pfunc([self.hidden], self.output)
......@@ -17,8 +17,11 @@ purpose of it is to hack it to investigate what your own particular program is d
predefined_optimizers)
class StepMode(Mode):
def __init__(self, linker=config.linker, optimizer=config.optimizer):
def __init__(self, linker=None, optimizer=None):
if linker is None:
linker = config.linker
if optimizer is None:
optimizer = config.optimizer
def blah(i, node, th):
# This function will be run for each node in your compiled program.
# here you can inspect all the values as they are computed,
......@@ -43,14 +46,14 @@ purpose of it is to hack it to investigate what your own particular program is d
if i == 39:
print 'this node is weird...', th.outputs[0][0]
self.provided_linker = linker
self.provided_optimizer = optimizer
if isinstance(linker, basestring) or linker is None:
linker = predefined_linkers[linker]
self.linker = WrapLinkerMany([linker], [blah])
if isinstance(optimizer, basestring) or optimizer is None:
optimizer = predefined_optimizers[optimizer]
self._optimizer = optimizer
......
......@@ -504,9 +504,9 @@ def char_from_number(number):
def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
file=sys.stdout, print_destroy_map=False, print_view_map=False,
order=[], ids='CHAR', stop_on_name=False,
prefix_child=None):
file=sys.stdout, print_destroy_map=False,
print_view_map=False, order=None, ids='CHAR',
stop_on_name=False, prefix_child=None):
"""Print the graph leading to `r` to given depth.
:param r: Variable instance
......@@ -531,6 +531,9 @@ def debugprint(r, prefix='', depth=-1, done=None, print_type=False,
if depth == 0:
return
if order is None:
order = []
if done is None:
done = dict()
......@@ -1417,7 +1420,9 @@ class _Linker(gof.link.LocalLinker):
self.env = None
self.maker = maker
def accept(self, env, no_recycling=[]):
def accept(self, env, no_recycling=None):
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
assert type(self) is _Linker
return type(self)(self.env, self.maker).accept(env, no_recycling)
......
......@@ -11,7 +11,7 @@ from profiling import ProfileStats
from pfunc import pfunc
from numpy import any #for to work in python 2.4
def function(inputs, outputs=None, mode=None, updates=[], givens=[],
def function(inputs, outputs=None, mode=None, updates=None, givens=None,
no_default_updates=False, accept_inplace=False, name=None,
rebuild_strict=True, allow_input_downcast=None, profile=None,
on_unused_input='raise'):
......@@ -80,7 +80,11 @@ def function(inputs, outputs=None, mode=None, updates=[], givens=[],
"""
#tuple are used in some tests, as we accepted them in the past
#I prefer to allow it as they act the same as list for what they are used.
if not isinstance(inputs,(list,tuple)):
if updates is None:
updates = []
if givens is None:
givens = []
if not isinstance(inputs, (list, tuple)):
raise Exception("Inputs variable of a Theano function should be contained in a list, even when there is a single input.")
# compute some features of the arguments:
......
......@@ -245,7 +245,11 @@ class Mode(object):
predefined_modes.
"""
def __init__(self, linker = config.linker, optimizer = config.optimizer):
def __init__(self, linker=None, optimizer=None):
if linker is None:
linker = config.linker
if optimizer is None:
optimizer = config.optimizer
self.__setstate__((linker, optimizer))
#self.provided_optimizer - typically the `optimizer` arg. But if the `optimizer` arg is
# keyword corresponding to a predefined Query, then this stores the query
......
......@@ -241,7 +241,7 @@ class Method(Component):
function call.
"""
outputs=None
outputs = None
"""function outputs (see `compile.function`)"""
updates = {}
......@@ -260,10 +260,10 @@ class Method(Component):
"""
mode=None
mode = None
"""This will override the Module compilation mode for this Method"""
def __init__(self, inputs, outputs, updates = {}, mode=None):
def __init__(self, inputs, outputs, updates=None, mode=None):
"""Initialize attributes
:param inputs: value for `Method.inputs`
......@@ -283,6 +283,8 @@ class Method(Component):
:type mode: None or any mode accepted by `compile.function`
"""
if updates is None:
updates = {}
super(Method, self).__init__()
self.inputs = inputs
self.outputs = outputs
......@@ -339,7 +341,7 @@ class Method(Component):
"""
return None
def build(self, mode, memo, allocate_all = False):
def build(self, mode, memo, allocate_all=False):
"""Compile a function for this Method.
:param allocate_all: if True, storage will be
......@@ -573,7 +575,7 @@ class Composite(Component):
"""
raise NotImplementedError
def flat_components(self, include_self = False):
def flat_components(self, include_self=False):
"""
Generator that yields each component in a flattened hierarchy
of composites and components. If include_self is True, the
......@@ -589,7 +591,7 @@ class Composite(Component):
else:
yield component
def flat_components_map(self, include_self = False, path = []):
def flat_components_map(self, include_self=False, path=None):
"""
Generator that yields (path, component) pairs in a flattened
hierarchy of composites and components, where path is a
......@@ -600,6 +602,8 @@ class Composite(Component):
If include_self is True, the list will include the Composite
instances, else it will only yield the list of leaves.
"""
if path is None:
path = []
if include_self:
yield path, self
for name, component in self.components_map():
......@@ -758,7 +762,9 @@ class ComponentList(Composite):
member.name = '%s.%i' % (name, i)
def default_initialize(self, init = {}, **kwinit):
def default_initialize(self, init=None, **kwinit):
if init is None:
init = {}
for k, initv in dict(init, **kwinit).iteritems():
self[k] = initv
......@@ -788,7 +794,9 @@ class ComponentDictInstance(ComponentDictInstanceNoInit):
ComponentDictInstance is meant to be instantiated by ComponentDict.
"""
def initialize(self, init={}, **kwinit):
def initialize(self, init=None, **kwinit):
if init is None:
init = {}
for k, initv in dict(init, **kwinit).iteritems():
self[k] = initv
......@@ -797,7 +805,9 @@ class ComponentDictInstance(ComponentDictInstanceNoInit):
class ComponentDict(Composite):
InstanceType = ComponentDictInstance # Type used by build() to make the instance
def __init__(self, components = {}, **kwcomponents):
def __init__(self, components=None, **kwcomponents):
if components is None:
components = {}
super(ComponentDict, self).__init__()
components = dict(components, **kwcomponents)
for val in components.itervalues():
......@@ -1077,10 +1087,12 @@ class Module(ComponentDict):
memo[self] = inst
return inst
def _instance_initialize(self, inst, init = {}, **kwinit):
def _instance_initialize(self, inst, init=None, **kwinit):
"""
Default initialization method.
"""
if init is None:
init = {}
for name, value in chain(init.iteritems(), kwinit.iteritems()):
inst[name] = value
......
......@@ -322,7 +322,7 @@ class Param(object):
self.implicit = implicit
def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
def pfunc(params, outputs=None, mode=None, updates=None, givens=None,
no_default_updates=False, accept_inplace=False, name=None,
rebuild_strict=True, allow_input_downcast=None,
profile=None, on_unused_input='raise'):
......@@ -405,6 +405,10 @@ def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
# Then it clones the outputs and the update expressions. This rebuilds a computation graph
# from the inputs and the givens.
#
if updates is None:
updates = []
if givens is None:
givens = []
if profile is None:
profile = config.profile
# profile -> True or False
......
......@@ -82,9 +82,13 @@ class Profile_Maker(FunctionMaker):
return ret
class ProfileMode(Mode):
def __init__(self, linker=config.linker, optimizer=config.optimizer):
message=""
profile_stats={}
def __init__(self, linker=None, optimizer=None):
if linker is None:
linker = config.linker
if optimizer is None:
optimizer = config.optimizer
message = ""
profile_stats = {}
self.__setstate__((linker,
optimizer,
message,
......
......@@ -402,8 +402,10 @@ class CLinker(link.Linker):
def __init__(self):
self.env = None
def accept(self, env, no_recycling=[]):
def accept(self, env, no_recycling=None):
"""WRITEME"""
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
return type(self)().accept(env, no_recycling)
#raise Exception("Cannot accept from a Linker that is already"
......@@ -987,12 +989,18 @@ class CLinker(link.Linker):
)
@staticmethod
def cmodule_key_(env, no_recycling, compile_args=[], libraries=[],
header_dirs=[], insert_config_md5=True):
def cmodule_key_(env, no_recycling, compile_args=None, libraries=None,
header_dirs=None, insert_config_md5=True):
"""
Do the actual computation of cmodule_key in a static method
to allow it to be reused in scalar.Composite.__eq__
"""
if compile_args is None:
compile_args = []
if libraries is None:
libraries = []
if header_dirs is None:
header_dirs = []
order = list(env.toposort())
#set of variables that have been computed by nodes we have
# seen 'so far' in the loop below
......@@ -1381,7 +1389,9 @@ class OpWiseCLinker(link.LocalLinker):
self.nice_errors = nice_errors
self.allow_gc = allow_gc
def accept(self, env, no_recycling=[]):
def accept(self, env, no_recycling=None):
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
return type(self)(self.fallback_on_perform).accept(env,
no_recycling)
......@@ -1519,7 +1529,9 @@ class DualLinker(link.Linker):
self.env = None
self.checker = checker
def accept(self, env, no_recycling=[]):
def accept(self, env, no_recycling=None):
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
return type(self)(self.checker).accept(env, no_recycling)
# raise Exception("Cannot accept from a Linker that is already "
......
......@@ -1411,7 +1411,8 @@ class GCC_compiler(object):
@staticmethod
def compile_str(module_name, src_code, location=None,
include_dirs=[], lib_dirs=[], libs=[], preargs=[]):
include_dirs=None, lib_dirs=None, libs=None,
preargs=None):
"""
:param module_name: string (this has been embedded in the src_code
......@@ -1435,6 +1436,12 @@ class GCC_compiler(object):
"""
#TODO: Do not do the dlimport in this function
if include_dirs is None:
preargs = []
if lib_dirs is None:
lib_dirs = []
if libs is None:
libs = []
if preargs is None:
preargs = []
else:
......
......@@ -80,18 +80,24 @@ class Env(utils.object2):
### Special ###
# TODO: document which things that features can do to the env
def __init__(self, inputs, outputs, features=[]):
def __init__(self, inputs, outputs, features=None):
"""
Create an Env which operates on the subgraph bound by the inputs and outputs
sets.
Create an Env which operates on the subgraph bound by the inputs and
outputs sets.
This class keeps a pointer to the inputs and outputs, and also modifies them.
This class keeps a pointer to the inputs and outputs, and also modifies
them.
#TODO: document what variables are[not] set in the env when a feature is added via the
constructor. How constructed is the env?
#TODO: document what variables are[not] set in the env when a feature
is added via the constructor. How constructed is the env?
"""
if features is None:
features = []
# XXX: Unless I'm missing something (but there's no documentation,
# so I probably am) this should be a set.
self._features = []
# All nodes in the subgraph defined by inputs and outputs are cached in nodes
......@@ -109,8 +115,10 @@ class Env(utils.object2):
for input in self.inputs:
if input.owner is not None:
raise ValueError("One of the provided inputs is the output of an already existing node. " \
"If that is okay, either discard that input's owner or use graph.clone.")
raise ValueError("One of the provided inputs is the output of"
"an already existing node. "
"If that is okay, either discard that "
"input's owner or use graph.clone.")
self.__setup_r__(input)
self.variables.add(input)
......@@ -432,6 +440,9 @@ class Env(utils.object2):
### features ###
# XXX: This is terribly named. The "extend" method of a list
# takes a sequence, and since this is a kind of container you
# would expect it to do similarly.
def extend(self, feature):
"""WRITEME
Adds a feature to this env. The feature may define one
......
......@@ -675,9 +675,11 @@ def general_toposort(r_out, deps, debug_print = False):
return rlist
def io_toposort(i, o, orderings = {}):
def io_toposort(i, o, orderings=None):
"""WRITEME
"""
if orderings is None:
orderings = {}
#the inputs are used only here in the function that decides what 'predecessors' to explore
iset = set(i)
def deps(obj):
......@@ -701,7 +703,7 @@ default_node_formatter = lambda op, argstrings: "%s(%s)" % (op.op,
", ".join(argstrings))
def is_same_graph(var1, var2, givens={}, debug=False):
def is_same_graph(var1, var2, givens=None, debug=False):
"""
Return True iff Variables `var1` and `var2` perform the same computation.
......@@ -740,6 +742,8 @@ def is_same_graph(var1, var2, givens={}, debug=False):
====== ====== ====== ======
"""
# Lazy import.
if givens is None:
givens = {}
global equal_computations, is_same_graph_with_merge
if equal_computations is None:
from theano.gof.opt import is_same_graph_with_merge
......
......@@ -299,7 +299,8 @@ def map_storage(env, order, input_storage, output_storage):
return input_storage, output_storage, storage_map
def streamline(env, thunks, order, post_thunk_old_storage = None, no_recycling = [], profiler = None, nice_errors = True):
def streamline(env, thunks, order, post_thunk_old_storage=None,
no_recycling=None, profiler=None, nice_errors=True):
"""WRITEME
:param env:
......@@ -320,6 +321,8 @@ def streamline(env, thunks, order, post_thunk_old_storage = None, no_recycling =
:param nice_errors: run in such a way that the double-traceback is printed. This costs a
bit of performance in the inner python loop.
"""
if no_recycling is None:
no_recycling = []
if profiler is not None:
raise NotImplementedError()
......@@ -419,7 +422,7 @@ class PerformLinker(LocalLinker):
self.env = None
self.allow_gc = allow_gc
def accept(self, env, no_recycling = []):
def accept(self, env, no_recycling=None):
"""
:param env: a PerformLinker can have accepted one Env instance at a time.
......@@ -427,6 +430,8 @@ class PerformLinker(LocalLinker):
:returns: self (TODO: WHY? Who calls this function?)
"""
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
return type(self)().accept(env, no_recycling)
#raise Exception("Cannot accept from a Linker that is already tied to another Env.")
......@@ -548,7 +553,7 @@ class WrapLinker(Linker):
self.linkers = linkers
self.wrapper = wrapper
def accept(self, env, no_recycling = []):
def accept(self, env, no_recycling=None):
"""
@type env: gof.Env
@param env: the env which we will link
......@@ -560,6 +565,8 @@ class WrapLinker(Linker):
the computation to avoid reusing it.
"""
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
return type(self)(self.linkers, self.wrapper).accept(env, no_recycling)
......
......@@ -356,12 +356,14 @@ class MergeOptimizer(Optimizer):
merge_optimizer = MergeOptimizer()
def is_same_graph_with_merge(var1, var2, givens={}):
def is_same_graph_with_merge(var1, var2, givens=None):
"""
Merge-based implementation of `theano.gof.graph.is_same_graph`.
See help on `theano.gof.graph.is_same_graph` for additional documentation.
"""
if givens is None:
givens = {}
# Copy variables since the MergeOptimizer will modify them.
copied = copy.deepcopy([var1, var2, givens])
vars = copied[0:2]
......@@ -483,7 +485,9 @@ class LocalOptimizer(object):
class FromFunctionLocalOptimizer(LocalOptimizer):
"""WRITEME"""
def __init__(self, fn, tracks = []):
def __init__(self, fn, tracks=None):
if tracks is None:
tracks = []
self.transform = fn
self._tracks = tracks
def tracks(self):
......
......@@ -40,9 +40,18 @@ def MyValue(data):
class MyOp(Op):
def __init__(self, nin, name, vmap = {}, dmap = {}, nout = 1,
destroyhandler_tolerate_same = [],
destroyhandler_tolerate_aliased = []):
def __init__(self, nin, name, vmap=None, dmap=None, nout=1,
destroyhandler_tolerate_same=None,
destroyhandler_tolerate_aliased=None):
if vmap is None:
vmap = {}
if dmap is None:
dmap = {}
if destroyhandler_tolerate_same is None:
destroyhandler_tolerate_same = []
if destroyhandler_tolerate_aliased is None:
destroyhandler_tolerate_aliased = []
self.nin = nin
self.nout = nout
self.name = name
......@@ -50,7 +59,7 @@ class MyOp(Op):
self.view_map = vmap
self.destroyhandler_tolerate_same = destroyhandler_tolerate_same
self.destroyhandler_tolerate_aliased = destroyhandler_tolerate_aliased
def make_node(self, *inputs):
assert len(inputs) == self.nin
inputs = map(as_variable, inputs)
......
......@@ -31,8 +31,10 @@ def MyVariable(name):
class MyOp(Op):
def __init__(self, name, dmap = {}, x = None):
def __init__(self, name, dmap=None, x=None):
self.name = name
if dmap is None:
dmap = {}
self.destroy_map = dmap
self.x = x
......
......@@ -429,7 +429,7 @@ class VM_Linker(link.LocalLinker):
self.callback = callback
self.updated_vars = {}
def accept(self, env, no_recycling=[]):
def accept(self, env, no_recycling=None):
"""
:param env: a PerformLinker can have accepted one Env instance
at a time.
......@@ -438,6 +438,8 @@ class VM_Linker(link.LocalLinker):
:returns: self (TODO: WHY? Who calls this function?)
"""
if no_recycling is None:
no_recycling = []
if self.env is not None and self.env is not env:
return type(self)().accept(env, no_recycling)
self.env = env
......
......@@ -54,7 +54,9 @@ def theano_parse_c_arg(c_arg):
"""
class TheanoElementwiseKernel(pycuda.elementwise.ElementwiseKernel):
def __init__(self, arguments, operation,
name="kernel", keep=False, options=[], **kwargs):
name="kernel", keep=False, options=None, **kwargs):
if options is None:
options = []
if isinstance(arguments, basestring):
arguments = [theano_parse_c_arg(arg)
for arg in arguments.split(",")]
......@@ -88,10 +90,12 @@ class PycudaElemwiseKernelOp(GpuOp):
nin = property(lambda self: self.scalar_op.nin)
nout = property(lambda self: self.scalar_op.nout)
def __init__(self, scalar_op, inplace_pattern={}, name=None):
def __init__(self, scalar_op, inplace_pattern=None, name=None):
if inplace_pattern is None:
inplace_pattern = {}
self.name = name
self.scalar_op = scalar_op
self.inplace_pattern = None
self.inplace_pattern = inplace_pattern
def __str__(self):
if self.name is None:
......@@ -172,10 +176,12 @@ class PycudaElemwiseSourceModuleOp(GpuOp):
nin = property(lambda self: self.scalar_op.nin)
nout = property(lambda self: self.scalar_op.nout)
def __init__(self, scalar_op, inplace_pattern={}, name=None):
def __init__(self, scalar_op, inplace_pattern=None, name=None):
if inplace_pattern is None:
inplace_pattern = {}
self.name = name
self.scalar_op = scalar_op
self.inplace_pattern = None
self.inplace_pattern = inplace_pattern
def __str__(self):
if self.name is None:
......@@ -264,10 +270,12 @@ class PycudaElemwiseSourceModuleMakeThunkOp(Op):
nin = property(lambda self: self.scalar_op.nin)
nout = property(lambda self: self.scalar_op.nout)
def __init__(self, scalar_op, inplace_pattern={}, name=None):
def __init__(self, scalar_op, inplace_pattern=None, name=None):
if inplace_pattern is None:
inplace_pattern = {}
self.name = name
self.scalar_op = scalar_op
self.inplace_pattern = None
self.inplace_pattern = inplace_pattern
def __str__(self):
if self.name is None:
......
......@@ -170,14 +170,18 @@ class Print(Op):
class PrinterState(gof.utils.scratchpad):
def __init__(self, props={}, **more_props):
def __init__(self, props=None, **more_props):
if props is None:
props = {}
if isinstance(props, gof.utils.scratchpad):
self.__update__(props)
else:
self.__dict__.update(props)
self.__dict__.update(more_props)
def clone(self, props={}, **more_props):
def clone(self, props=None, **more_props):
if props is None:
props = {}
return PrinterState(self, **dict(props, **more_props))
......@@ -359,8 +363,10 @@ class PPrinter:
cp.assign(condition, printer)
return cp
def process_graph(self, inputs, outputs, updates={},
def process_graph(self, inputs, outputs, updates=None,
display_inputs=False):
if updates is None:
updates = {}
if not isinstance(inputs, (list, tuple)):
inputs = [inputs]
if not isinstance(outputs, (list, tuple)):
......
......@@ -130,10 +130,12 @@ class GpuElemwise(GpuOp):
nin = property(lambda self: self.scalar_op.nin)
nout = property(lambda self: self.scalar_op.nout)
def __init__(self, scalar_op, inplace_pattern={}, sync=None):
def __init__(self, scalar_op, inplace_pattern=None, sync=None):
#TODO-- this looks like a bug-- either we should use the sync argument
# or get rid of it, we shouldn't let the client think they can control
#sync when they can't
if inplace_pattern is None:
inplace_pattern = {}
sync = config.gpuelemwise.sync
self.scalar_op = scalar_op
......
......@@ -39,11 +39,13 @@ class NaiveAlgo(object):
#cache_version = ()
cache_version = (15, verbose)
def __init__(self, scalar_op, sync=True, inplace_pattern={}):
def __init__(self, scalar_op, sync=True, inplace_pattern=None):
"""
:param scalar_op: the scalar operation to execute on each element.
:param sync: if True, will wait after the kernel launch and check for error call.
"""
if inplace_pattern is None:
inplace_pattern = {}
try:
code = scalar_op.c_support_code_apply(None, "nodename")
if code:
......
......@@ -54,9 +54,14 @@ class Kouh2008(object):
_logger.debug('output dtype %s' % output.dtype)
@classmethod
def new_expbounds(cls, rng, x_list, n_out, dtype=None, params=[], updates=[], exponent_range=(1.0, 3.0)):
def new_expbounds(cls, rng, x_list, n_out, dtype=None, params=None,
updates=None, exponent_range=(1.0, 3.0)):
"""
"""
if params is None:
params = []
if updates is None:
updates = []
if dtype is None:
dtype = x_list[0].dtype
n_terms = len(x_list)
......
......@@ -10,12 +10,16 @@ class DebugLinker(gof.WrapLinker):
def __init__(self,
linkers,
debug_pre = [],
debug_post = [],
copy_originals = False,
check_types = True,
compare_variables = True,
compare_fn = (lambda x, y: x == y)):
debug_pre=None,
debug_post=None,
copy_originals=False,
check_types=True,
compare_variables=True,
compare_fn=(lambda x, y: x == y)):
if debug_pre is None:
debug_pre = []
if debug_post is None:
debug_post = []
gof.WrapLinker.__init__(self,
linkers = linkers,
wrapper = self.wrapper)
......@@ -23,7 +27,7 @@ class DebugLinker(gof.WrapLinker):
self.env = None
self.compare_fn = compare_fn
self.copy_originals = copy_originals
if check_types not in [None, True]:
self.check_types = check_types
......@@ -42,10 +46,12 @@ class DebugLinker(gof.WrapLinker):
if compare_variables is not None:
self.debug_post.append(self.compare_variables)
def accept(self, env, no_recycling = []):
def accept(self, env, no_recycling=None):
if no_recycling is None:
no_recycling = []
return gof.WrapLinker.accept(self,
env = env,
no_recycling = no_recycling)
env=env,
no_recycling=no_recycling)
def store_value(self, i, node, *thunks):
th1 = thunks[0]
......@@ -165,7 +171,9 @@ def numpy_compare(a, b, tolerance = 1e-6):
return a == b
def numpy_debug_linker(pre, post = []):
def numpy_debug_linker(pre, post=None):
if post is None:
post = []
return DebugLinker([gof.OpWiseCLinker],
pre,
post,
......
......@@ -96,10 +96,12 @@ def compile_fn(f, path_locals, common_inputs):
updated = []
return compiled_f, updated
def compile(smod, initial_values={}):
def compile(smod, initial_values=None):
"""
:type values: dictionary Variable -> value
"""
if initial_values is None:
initial_values = {}
def sym_items(mod):
for k in mod.__dict__:
if k in ['__module__', 'build_graph', '__doc__']:
......
......@@ -281,8 +281,10 @@ def test_consistency_GPU_parallel():
samples = numpy.array(samples).flatten()
assert(numpy.allclose(samples, java_samples))
def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=[],
def basictest(f, steps, sample_size, prefix="", allow_01=False, inputs=None,
target_avg=0.5, target_std=None, mean_rtol=0.01):
if inputs is None:
inputs = []
dt = 0.0
avg_std = 0.0
......
......@@ -25,7 +25,7 @@ class symbolic_fn_callable(object):
class.
.. code-block:: python
class T(TheanoObject):
@symbolic_fn
def add(self, x):
......@@ -33,7 +33,7 @@ class symbolic_fn_callable(object):
add_outputs = ...
add_updates = ...
return RVal(add_outputs, add_updates)
t = T()
t = T()
t.add.outputs(5) # returns `add_outputs` from when `x=theano_type(5)`
t.add.updates(5) # returns `add_updates` from when `x=theano_type(5)`
t.add.theano_function(5) # returns the `Function` compiled when `x=theano_type(5)`
......@@ -48,7 +48,7 @@ class symbolic_fn_callable(object):
"""Silly method to work with symbolic_fn.__get__"""
self.o_self = o_self
return self
def run_symbolic(self, *args, **kwargs):
return self.o_self._get_method_impl(self.fn, self.o_self, args, kwargs, mode=self.mode)
......@@ -70,7 +70,7 @@ class symbolic_fn(object):
def __init__(self, fn, mode=None):
self.fn = fn
self.callable = symbolic_fn_callable(fn, mode)
def __get__(self, o_self, o_cls):
return self.callable.on(o_self)
......@@ -91,16 +91,18 @@ class RVal(object):
"""A Return-Value object for a `symbolic_fn` """
outputs = []
"""The method will compute values for the variables in this list"""
updates = {}
"""The method will update module variables in this dictionary
For items ``(k,v)`` in this dictionary, ``k`` must be a `symbolic_member` of some module.
On each call to this compiled function, the value of ``k`` will be replaced with the
computed value of the Variable ``v``.
"""
def __init__(self, outputs, updates={}):
def __init__(self, outputs, updates=None):
if updates is None:
updates = {}
self.outputs = outputs
assert type(updates) is dict
self.updates = updates
......@@ -111,19 +113,19 @@ class TheanoObject(object):
This class provides support for symbolic_fn class attributes.
These will be compiled on demand so that they can be used just like normal (non-symbolic)
methods.
The symbolic functions in a TheanoObject can share member variables that have been created
using the `symbolic_member` method.
:note: Other variables (ones not created using ``self.symbolic_member``) referred to in the
body of a symbolic function will *not* be shared between symbolic functions, or between
symbolic functions and this class. These other variables will be locked away in the
closure of a symbolic function when that function is compiled.
closure of a symbolic function when that function is compiled.
:warning: It is not recommended for code to interleave
(a) changes to non-symbolic instance variables with
(b) calls to symbolic functions that use those instance variables.
(b) calls to symbolic functions that use those instance variables.
A symbolic function may be
compiled multiple times because it must be compiled for each set of argument types.
Each time the function is compiled, the values of non-symbolic variables will be locked
......@@ -179,7 +181,7 @@ class TheanoObject(object):
# construct In instances for the symbolic_member instances that can automatically be
# included here.
module_inputs = [theano.compile.io.In(
variable=v,
variable=v,
value=v._theanoclass_container,
mutable=(v in rval.updates),
update=rval.updates.get(v, None))
......@@ -210,7 +212,7 @@ class TheanoObject(object):
v = tensor.lscalar(name)
v._theanoclass_container = \
theano.gof.Container(v,
theano.gof.Container(v,
storage = [theano._asarray(ival, dtype='int64')],
readonly=False)
assert not hasattr(v, 'set')
......@@ -222,5 +224,5 @@ class TheanoObject(object):
return v
......@@ -454,7 +454,7 @@ def infer_shape(outs, inputs, input_shapes):
class Validator(object):
def __init__(self, valid=[], invalid=[], valid_equivalent={}):
def __init__(self, valid=None, invalid=None, valid_equivalent=None):
'''
Check if variables can be expressed without using variables in invalid.
......@@ -462,6 +462,13 @@ class Validator(object):
variables to valid ones that can be used instead.
'''
if valid is None:
valid = []
if invalid is None:
invalid = []
if valid_equivalent is None:
valid_equivalent = {}
# Nodes that are valid to have in the graph computing outputs
self.valid = set(valid)
......
......@@ -2662,22 +2662,28 @@ def zeros_like(model, dtype=None):
return fill(model, constant(0.0, dtype=dtype))
def zeros(shape, dtype=config.floatX):
def zeros(shape, dtype=None):
"""
Create a Tensor filled with zeros, closer to Numpy's syntax than ``alloc``.
"""
if dtype is None:
dtype = config.floatX
return alloc(numpy.array(0, dtype=dtype), *shape)
def ones(shape, dtype=config.floatX):
def ones(shape, dtype=None):
"""
Create a Tensor filled with ones, closer to Numpy's syntax than ``alloc``.
"""
if dtype is None:
dtype = config.floatX
return alloc(numpy.array(1, dtype=dtype), *shape)
class Eye(gof.Op):
def __init__(self, dtype=config.floatX):
def __init__(self, dtype=None):
if dtype is None:
dtype = config.floatX
self.dtype = dtype
def make_node(self, n, m, k):
......@@ -2702,8 +2708,10 @@ class Eye(gof.Op):
return hash(self.dtype) ^ hash(type(self))
def eye(n, m=None, k=0, dtype=config.floatX):
if m == None:
def eye(n, m=None, k=0, dtype=None):
if dtype is None:
dtype = config.floatX
if m is None:
m = n
localop = Eye(dtype)
return localop(n, m, k)
......@@ -3080,7 +3088,7 @@ def var(input, axis=None):
"""
input_ndim = input.type.ndim
if axis == None:
if axis is None:
axis = range(input_ndim)
if isinstance(axis, int):
axis = [axis]
......@@ -4081,7 +4089,9 @@ class IncSubtensor(Op):
"""
def __init__(self, idx_list, inplace=False, set_instead_of_inc=False,
destroyhandler_tolerate_aliased=[]):
destroyhandler_tolerate_aliased=None):
if destroyhandler_tolerate_aliased is None:
destroyhandler_tolerate_aliased = []
self.idx_list = map(Subtensor.convert, idx_list)
self.inplace = inplace
if inplace:
......
......@@ -9,7 +9,7 @@ else:
import numpy
from copy import copy
from theano.compile import (SymbolicInputKit, SymbolicInput,
from theano.compile import (SymbolicInputKit, SymbolicInput,
Module, module, Method, Member, In, Component)
from theano.gof import Container
from theano.gof.python25 import deque
......@@ -20,7 +20,7 @@ class KitComponent(Component):
"""
Represents a SymbolicInputKit (see io.py).
"""
def __init__(self, kit):
super(KitComponent, self).__init__()
self.kit = kit
......@@ -88,7 +88,9 @@ rk = RandomKit('rk', 0xBAD5EED)
class RModule(Module):
"""Module providing random number streams in Theano graphs."""
def __init__(self, components = {}, **kwcomponents):
def __init__(self, components=None, **kwcomponents):
if components is None:
components = {}
super(RModule, self).__init__(components, **kwcomponents)
self.random = RandomKit('rkit')
self._rkit = KitComponent(self.random)
......@@ -104,8 +106,8 @@ class RModule(Module):
if recursive:
#Here, we recurse through all the components (inst2) contained in (inst)
#and seeds each subcomponent that is an RModule
for path, c in self.flat_components_map(True):
if isinstance(c, RModule):
inst2 = inst
......
......@@ -430,7 +430,7 @@ class Elemwise(Op):
Elemwise(log)(rand(3, 4, 5))
"""
def __init__(self, scalar_op, inplace_pattern={}, name=None,
def __init__(self, scalar_op, inplace_pattern=None, name=None,
nfunc_spec=None):
"""
Usage: Elemwise(scalar_op, inplace_pattern = {})
......@@ -451,6 +451,8 @@ class Elemwise(Op):
NOTE: as of now, the sign of the nout field is ignored (some work
needs to be done to resize the destinations when needed).
"""
if inplace_pattern is None:
inplace_pattern = {}
self.name = name
self.scalar_op = scalar_op
self.inplace_pattern = inplace_pattern
......
......@@ -31,7 +31,7 @@ class T_softplus(unittest.TestCase):
class T_sigmoid_opts(unittest.TestCase):
def get_mode(self, excluding=[]):
def get_mode(self, excluding=None):
"""
Return appropriate mode for the tests.
......@@ -41,6 +41,8 @@ class T_sigmoid_opts(unittest.TestCase):
set to 'FAST_COMPILE' (in which case it is replaced by the 'FAST_RUN'
mode), without the optimizations specified in `excluding`.
"""
if excluding is None:
excluding = []
m = theano.config.mode
if m == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN')
......
......@@ -173,9 +173,19 @@ def safe_make_node(op, *inputs):
return node.owner
def makeTester(name, op, expected, checks={}, good={}, bad_build={},
bad_runtime={}, grad={}, mode=None, grad_rtol=None,
def makeTester(name, op, expected, checks=None, good=None, bad_build=None,
bad_runtime=None, grad=None, mode=None, grad_rtol=None,
eps=1e-10, skip=False):
if checks is None:
checks = {}
if good is None:
good = {}
if bad_build is None:
bad_build = {}
if bad_runtime is None:
bad_runtime = {}
if grad is None:
grad = {}
if grad is True:
grad = good
......@@ -400,7 +410,9 @@ def rand_of_dtype(shape, dtype):
raise TypeError()
def makeBroadcastTester(op, expected, checks={}, name=None, **kwargs):
def makeBroadcastTester(op, expected, checks=None, name=None, **kwargs):
if checks is None:
checks = {}
if name is None:
name = str(op)
# Here we ensure the test name matches the name of the variable defined in
......@@ -575,10 +587,12 @@ MulInplaceTester = makeBroadcastTester(op = inplace.mul_inplace,
inplace = True)
def copymod(dct, without=[], **kwargs):
def copymod(dct, without=None, **kwargs):
"""Return dct but with the keys named by args removed, and with
kwargs added.
"""
if without is None:
without = []
rval = copy(dct)
for a in without:
if a in rval:
......
......@@ -1427,7 +1427,9 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
self.ger_destructive = ger_destructive
self.gemm = gemm_no_inplace
def function(self, inputs, outputs, updates={}):
def function(self, inputs, outputs, updates=None):
if updates is None:
updates = {}
return theano.function(inputs, outputs, self.mode, updates=updates)
def b(self, bval):
......
......@@ -21,11 +21,13 @@ class MyType(Type):
class MyOp(Op):
def __init__(self, name, dmap = {}, x = None):
def __init__(self, name, dmap=None, x=None):
if dmap is None:
dmap = {}
self.name = name
self.destroy_map = dmap
self.x = x
def make_node(self, *inputs):
inputs = map(as_variable, inputs)
for input in inputs:
......@@ -41,7 +43,8 @@ class MyOp(Op):
return self.name
def __eq__(self, other):
return self is other or isinstance(other, MyOp) and self.x is not None and self.x == other.x
return (self is other or isinstance(other, MyOp) and self.x is not None
and self.x == other.x)
def __hash__(self):
if self.x is not None:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论