提交 9ae243d3 authored 作者: hantek's avatar hantek

remove all docstrings in test_basic.py

上级 569ac30f
...@@ -103,13 +103,11 @@ def eval_outputs(outputs): ...@@ -103,13 +103,11 @@ def eval_outputs(outputs):
def get_numeric_subclasses(cls=numpy.number, ignore=None): def get_numeric_subclasses(cls=numpy.number, ignore=None):
""" # Return subclasses of `cls` in the numpy scalar hierarchy.
Return subclasses of `cls` in the numpy scalar hierarchy. #
# We only return subclasses that correspond to unique data types.
We only return subclasses that correspond to unique data types. # The hierarchy can be seen here:
The hierarchy can be seen here: # http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
"""
if ignore is None: if ignore is None:
ignore = [] ignore = []
rval = [] rval = []
...@@ -127,28 +125,26 @@ def get_numeric_subclasses(cls=numpy.number, ignore=None): ...@@ -127,28 +125,26 @@ def get_numeric_subclasses(cls=numpy.number, ignore=None):
def get_numeric_types(with_int=True, with_float=True, with_complex=False, def get_numeric_types(with_int=True, with_float=True, with_complex=False,
only_theano_types=True): only_theano_types=True):
""" # Return numpy numeric data types.
Return numpy numeric data types. #
# :param with_int: Whether to include integer types.
:param with_int: Whether to include integer types. #
# :param with_float: Whether to include floating point types.
:param with_float: Whether to include floating point types. #
# :param with_complex: Whether to include complex types.
:param with_complex: Whether to include complex types. #
# :param only_theano_types: If True, then numpy numeric data types that are
:param only_theano_types: If True, then numpy numeric data types that are # not supported by Theano are ignored (i.e. those that are not declared in
not supported by Theano are ignored (i.e. those that are not declared in # scalar/basic.py).
scalar/basic.py). #
# :returns: A list of unique data type objects. Note that multiple data types
:returns: A list of unique data type objects. Note that multiple data types # may share the same string representation, but can be differentiated through
may share the same string representation, but can be differentiated through # their `num` attribute.
their `num` attribute. #
# Note that when `only_theano_types` is True we could simply return the list
Note that when `only_theano_types` is True we could simply return the list # of types defined in the `scalar` module. However with this function we can
of types defined in the `scalar` module. However with this function we can # test more unique dtype objects, and in the future we may use it to
test more unique dtype objects, and in the future we may use it to # automatically detect new data types introduced in numpy.
automatically detect new data types introduced in numpy.
"""
if only_theano_types: if only_theano_types:
theano_types = [d.dtype for d in theano.scalar.all_types] theano_types = [d.dtype for d in theano.scalar.all_types]
rval = [] rval = []
...@@ -178,10 +174,8 @@ def get_numeric_types(with_int=True, with_float=True, with_complex=False, ...@@ -178,10 +174,8 @@ def get_numeric_types(with_int=True, with_float=True, with_complex=False,
def _numpy_checker(x, y): def _numpy_checker(x, y):
""" # Checks if x.data and y.data have the same contents.
Checks if x.data and y.data have the same contents. # Used in DualLinker to compare C version with Python version.
Used in DualLinker to compare C version with Python version.
"""
x, y = x[0], y[0] x, y = x[0], y[0]
if (x.dtype != y.dtype or x.shape != y.shape if (x.dtype != y.dtype or x.shape != y.shape
or numpy.any(numpy.abs(x - y) > 1e-10)): or numpy.any(numpy.abs(x - y) > 1e-10)):
...@@ -189,10 +183,9 @@ def _numpy_checker(x, y): ...@@ -189,10 +183,9 @@ def _numpy_checker(x, y):
def safe_make_node(op, *inputs): def safe_make_node(op, *inputs):
""" Emulate the behaviour of make_node when op is a function. # Emulate the behaviour of make_node when op is a function.
#
Normally op in an instead of the Op class. # Normally op in an instead of the Op class.
"""
node = op(*inputs) node = op(*inputs)
if isinstance(node, list): if isinstance(node, list):
return node[0].owner return node[0].owner
...@@ -201,16 +194,15 @@ def safe_make_node(op, *inputs): ...@@ -201,16 +194,15 @@ def safe_make_node(op, *inputs):
def upcast_float16_ufunc(fn): def upcast_float16_ufunc(fn):
"""Decorator that enforces computation is not done in float16 by NumPy. # Decorator that enforces computation is not done in float16 by NumPy.
#
Some ufuncs in NumPy will compute float values on int8 and uint8 # Some ufuncs in NumPy will compute float values on int8 and uint8
in half-precision (float16), which is not enough, and not compatible # in half-precision (float16), which is not enough, and not compatible
with the C code. # with the C code.
#
:param fn: numpy ufunc # :param fn: numpy ufunc
:returns: function similar to fn.__call__, computing the same # :returns: function similar to fn.__call__, computing the same
value with a minimum floating-point precision of float32 # value with a minimum floating-point precision of float32
"""
def ret(*args, **kwargs): def ret(*args, **kwargs):
out_dtype = numpy.find_common_type( out_dtype = numpy.find_common_type(
[a.dtype for a in args], [numpy.float16]) [a.dtype for a in args], [numpy.float16])
...@@ -224,15 +216,14 @@ def upcast_float16_ufunc(fn): ...@@ -224,15 +216,14 @@ def upcast_float16_ufunc(fn):
def upcast_int8_nfunc(fn): def upcast_int8_nfunc(fn):
"""Decorator that upcasts input of dtype int8 to float32. # Decorator that upcasts input of dtype int8 to float32.
#
This is so that floating-point computation is not carried using # This is so that floating-point computation is not carried using
half-precision (float16), as some NumPy functions do. # half-precision (float16), as some NumPy functions do.
#
:param fn: function computing a floating-point value from inputs # :param fn: function computing a floating-point value from inputs
:returns: function similar to fn, but upcasting its uint8 and int8 # :returns: function similar to fn, but upcasting its uint8 and int8
inputs before carrying out the computation. # inputs before carrying out the computation.
"""
def ret(*args, **kwargs): def ret(*args, **kwargs):
args = list(args) args = list(args)
for i, a in enumerate(args): for i, a in enumerate(args):
...@@ -248,10 +239,8 @@ def makeTester(name, op, expected, checks=None, good=None, bad_build=None, ...@@ -248,10 +239,8 @@ def makeTester(name, op, expected, checks=None, good=None, bad_build=None,
bad_runtime=None, grad=None, mode=None, grad_rtol=None, bad_runtime=None, grad=None, mode=None, grad_rtol=None,
eps=1e-10, skip=False, test_memmap=True, check_name=True, eps=1e-10, skip=False, test_memmap=True, check_name=True,
grad_eps=None): grad_eps=None):
""" # :param check_name:
:param check_name: # Use only for tester that aren't in Theano.
Use only for tester that aren't in Theano.
"""
if checks is None: if checks is None:
checks = {} checks = {}
if good is None: if good is None:
...@@ -551,7 +540,7 @@ def rand(*shape): ...@@ -551,7 +540,7 @@ def rand(*shape):
def rand_nonzero(shape, eps=3e-4): def rand_nonzero(shape, eps=3e-4):
"""Like rand, but the absolute value has to be at least eps""" # Like rand, but the absolute value has to be at least eps
# covers [0, 1) # covers [0, 1)
r = numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX) r = numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX)
# covers [0, (1 - eps) / 2) U [(1 + eps) / 2, 1) # covers [0, (1 - eps) / 2) U [(1 + eps) / 2, 1)
...@@ -693,17 +682,15 @@ _grad_broadcast_binary_normal = dict( ...@@ -693,17 +682,15 @@ _grad_broadcast_binary_normal = dict(
def check_floatX(inputs, rval): def check_floatX(inputs, rval):
""" # :param inputs: Inputs to a function that returned `rval` with these inputs.
:param inputs: Inputs to a function that returned `rval` with these inputs. #
# :param rval: Value returned by a function with inputs set to `inputs`.
:param rval: Value returned by a function with inputs set to `inputs`. #
# :returns: Either `rval` unchanged, or `rval` cast in float32. The idea is
:returns: Either `rval` unchanged, or `rval` cast in float32. The idea is # that when a numpy function would have returned a float64, Theano may prefer
that when a numpy function would have returned a float64, Theano may prefer # to return a float32 instead when `config.cast_policy` is set to
to return a float32 instead when `config.cast_policy` is set to # 'numpy+floatX' and config.floatX to 'float32', and there was no float64
'numpy+floatX' and config.floatX to 'float32', and there was no float64 # input.
input.
"""
if (isinstance(rval, numpy.ndarray) and if (isinstance(rval, numpy.ndarray) and
rval.dtype == 'float64' and rval.dtype == 'float64' and
config.cast_policy == 'numpy+floatX' config.cast_policy == 'numpy+floatX'
...@@ -839,9 +826,8 @@ MulInplaceTester = makeBroadcastTester(op=inplace.mul_inplace, ...@@ -839,9 +826,8 @@ MulInplaceTester = makeBroadcastTester(op=inplace.mul_inplace,
def copymod(dct, without=None, **kwargs): def copymod(dct, without=None, **kwargs):
"""Return dct but with the keys named by args removed, and with # Return dct but with the keys named by args removed, and with
kwargs added. # kwargs added.
"""
if without is None: if without is None:
without = [] without = []
rval = copy(dct) rval = copy(dct)
...@@ -919,11 +905,10 @@ if config.floatX == 'float32': ...@@ -919,11 +905,10 @@ if config.floatX == 'float32':
def _numpy_true_div(x, y): def _numpy_true_div(x, y):
"""Performs true division, and cast the result in the type we expect. # Performs true division, and cast the result in the type we expect.
#
We define that function so we can use it in TrueDivTester.expected, # We define that function so we can use it in TrueDivTester.expected,
because simply calling numpy.true_divide could cause a dtype mismatch. # because simply calling numpy.true_divide could cause a dtype mismatch.
"""
out = numpy.true_divide(x, y) out = numpy.true_divide(x, y)
# Use floatX as the result of int / int # Use floatX as the result of int / int
if x.dtype in tensor.discrete_dtypes and y.dtype in tensor.discrete_dtypes: if x.dtype in tensor.discrete_dtypes and y.dtype in tensor.discrete_dtypes:
...@@ -2297,10 +2282,8 @@ class ApplyDefaultTestOp(theano.Op): ...@@ -2297,10 +2282,8 @@ class ApplyDefaultTestOp(theano.Op):
class TestAsTensorVariable(unittest.TestCase): class TestAsTensorVariable(unittest.TestCase):
""" # Unit test for ensuring that as_tensor_variable handles Apply objects
Unit test for ensuring that as_tensor_variable handles Apply objects # correctly and removes leading broadcastable dimensions when possible.
correctly and removes leading broadcastable dimensions when possible.
"""
def setUp(self): def setUp(self):
self.x = tensor.scalar('x') self.x = tensor.scalar('x')
...@@ -3067,9 +3050,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -3067,9 +3050,7 @@ class T_max_and_argmax(unittest.TestCase):
assert tuple(v) == numpy.max(data, np_axis).shape assert tuple(v) == numpy.max(data, np_axis).shape
def test_arg_grad(self): def test_arg_grad(self):
""" # The test checks that the gradient of argmax(x).sum() is 0
The test checks that the gradient of argmax(x).sum() is 0
"""
x = matrix() x = matrix()
cost = argmax(x, axis=0).sum() cost = argmax(x, axis=0).sum()
...@@ -3082,15 +3063,14 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -3082,15 +3063,14 @@ class T_max_and_argmax(unittest.TestCase):
n = as_tensor_variable(data) n = as_tensor_variable(data)
def safe_verify_grad(func, data): def safe_verify_grad(func, data):
""" # Wrapper around 'verify_grad' that picks a proper value for epsilon.
Wrapper around 'verify_grad' that picks a proper value for epsilon. #
# This is needed because 'verify_grad' may fail when its epsilon is
This is needed because 'verify_grad' may fail when its epsilon is # too large, due to the fact the argmax is not continuous.
too large, due to the fact the argmax is not continuous. # We make sure epsilon is less than the minimum absolute value found
We make sure epsilon is less than the minimum absolute value found # in the matrix of pairwise differences between all elements in the
in the matrix of pairwise differences between all elements in the # data. This way, the argmax will not change when adding epsilon.
data. This way, the argmax will not change when adding epsilon.
"""
# 'data' is a one-element list. # 'data' is a one-element list.
data_tensor, = data data_tensor, = data
# Flatten it into a 1D vector. # Flatten it into a 1D vector.
...@@ -3107,9 +3087,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -3107,9 +3087,7 @@ class T_max_and_argmax(unittest.TestCase):
utt.verify_grad(func, data, eps=eps) utt.verify_grad(func, data, eps=eps)
def check_grad_max(data, max_grad_data, axis=None): def check_grad_max(data, max_grad_data, axis=None):
""" # Why this is needed? verify_grad is not enough?
Why this is needed? verify_grad is not enough?
"""
# This works only for axis in [0, None]. # This works only for axis in [0, None].
assert axis in [0, None] assert axis in [0, None]
z = numpy.zeros_like(data) z = numpy.zeros_like(data)
...@@ -3159,9 +3137,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -3159,9 +3137,7 @@ class T_max_and_argmax(unittest.TestCase):
safe_verify_grad(lambda v: max_and_argmax(v, axis=i)[1], [data]) safe_verify_grad(lambda v: max_and_argmax(v, axis=i)[1], [data])
def test_preserve_broadcastable(self): def test_preserve_broadcastable(self):
""" # Ensure the original broadcastable flags are preserved by Max/Argmax.
Ensure the original broadcastable flags are preserved by Max/Argmax.
"""
x = tensor.matrix().dimshuffle('x', 0, 'x', 1, 'x') x = tensor.matrix().dimshuffle('x', 0, 'x', 1, 'x')
y = x.max(axis=1) y = x.max(axis=1)
assert y.type.broadcastable == (True, True, False, True) assert y.type.broadcastable == (True, True, False, True)
...@@ -3501,12 +3477,10 @@ class T_min_max(unittest.TestCase): ...@@ -3501,12 +3477,10 @@ class T_min_max(unittest.TestCase):
check_grad_min(data, eval_outputs(grad(min(n.flatten()), n))) check_grad_min(data, eval_outputs(grad(min(n.flatten()), n)))
def _grad_list(self): def _grad_list(self):
""" # Test the gradient when we have multiple axis at the same time.
Test the gradient when we have multiple axis at the same time. #
# This not implemented, so we disable the test. See ticket:
This not implemented, so we disable the test. See ticket: # http://www.assembla.com/spaces/theano/tickets/511
http://www.assembla.com/spaces/theano/tickets/511
"""
data = rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for fct in [max_and_argmax, max, min]: for fct in [max_and_argmax, max, min]:
...@@ -3534,10 +3508,8 @@ class T_outer(unittest.TestCase): ...@@ -3534,10 +3508,8 @@ class T_outer(unittest.TestCase):
assert_allclose(o, numpy.outer(v1, v2)) assert_allclose(o, numpy.outer(v1, v2))
def test_grad(self): def test_grad(self):
""" # Test the combined graph of the graph of outer
Test the combined graph of the graph of outer # with broadcastable dimensions, just in case.
with broadcastable dimensions, just in case.
"""
for shp0, shp1 in [((1,), (2,)), for shp0, shp1 in [((1,), (2,)),
((3,), (1,)), ((3,), (1,)),
((1,), (1,)), ((1,), (1,)),
...@@ -3577,9 +3549,7 @@ class T_GetVectorLength(unittest.TestCase): ...@@ -3577,9 +3549,7 @@ class T_GetVectorLength(unittest.TestCase):
class T_Join_and_Split(unittest.TestCase): class T_Join_and_Split(unittest.TestCase):
""" # Split is tested by each verify_grad method.
Split is tested by each verify_grad method.
"""
def setUp(self): def setUp(self):
Join.debug = False Join.debug = False
utt.seed_rng() utt.seed_rng()
...@@ -3648,9 +3618,9 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3648,9 +3618,9 @@ class T_Join_and_Split(unittest.TestCase):
self.assertTrue((out == want).all()) self.assertTrue((out == want).all())
def test_stack_scalar_make_vector(self): def test_stack_scalar_make_vector(self):
"""Test that calling stack() on scalars instantiates MakeVector, # Test that calling stack() on scalars instantiates MakeVector,
not Join. Test that the floatX dtype stay floatX, not downcasted # not Join. Test that the floatX dtype stay floatX, not downcasted
to int64""" # to int64
a = tensor.scalar('a', dtype=self.floatX) a = tensor.scalar('a', dtype=self.floatX)
b = tensor.scalar('b', dtype=self.floatX) b = tensor.scalar('b', dtype=self.floatX)
s = stack([a, b, a, b]) s = stack([a, b, a, b])
...@@ -3664,8 +3634,8 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3664,8 +3634,8 @@ class T_Join_and_Split(unittest.TestCase):
assert f.maker.fgraph.outputs[0].dtype == self.floatX assert f.maker.fgraph.outputs[0].dtype == self.floatX
def test_stack_scalar_make_vector_dtype(self): def test_stack_scalar_make_vector_dtype(self):
'''Test that calling stack() on scalars instantiates MakeVector, # Test that calling stack() on scalars instantiates MakeVector,
event when the scalar don't have the same dtype.''' # event when the scalar don't have the same dtype.
a = tensor.iscalar('a') a = tensor.iscalar('a')
b = tensor.lscalar('b') b = tensor.lscalar('b')
s = stack([a, b, a, b]) s = stack([a, b, a, b])
...@@ -3678,8 +3648,8 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3678,8 +3648,8 @@ class T_Join_and_Split(unittest.TestCase):
assert f.maker.fgraph.outputs[0].dtype == 'int64' assert f.maker.fgraph.outputs[0].dtype == 'int64'
def test_stack_scalar_make_vector_constant(self): def test_stack_scalar_make_vector_constant(self):
'''Test that calling stack() on scalars instantiates MakeVector, # Test that calling stack() on scalars instantiates MakeVector,
event when the scalar are simple int type.''' # event when the scalar are simple int type.
a = tensor.iscalar('a') a = tensor.iscalar('a')
b = tensor.lscalar('b') b = tensor.lscalar('b')
# test when the constant is the first element. # test when the constant is the first element.
...@@ -3694,7 +3664,8 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3694,7 +3664,8 @@ class T_Join_and_Split(unittest.TestCase):
assert f.maker.fgraph.outputs[0].dtype == 'int64' assert f.maker.fgraph.outputs[0].dtype == 'int64'
def test_stack_new_interface(self): def test_stack_new_interface(self):
"""Test the new numpy-like interface: stack(tensors, axis=0).""" # Test the new numpy-like interface: stack(tensors, axis=0).
# Testing against old interface # Testing against old interface
warnings.simplefilter('always', DeprecationWarning) warnings.simplefilter('always', DeprecationWarning)
a = tensor.imatrix('a') a = tensor.imatrix('a')
...@@ -3786,8 +3757,8 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3786,8 +3757,8 @@ class T_Join_and_Split(unittest.TestCase):
assert numpy.allclose(Hb_v, 0.) assert numpy.allclose(Hb_v, 0.)
def test_join_concatenate_one_element(self): def test_join_concatenate_one_element(self):
''' Fast test of concatenate as this is an alias for join. # Fast test of concatenate as this is an alias for join.
also test that we remove the Join op if there is only 1 input''' # also test that we remove the Join op if there is only 1 input
m = tensor.fmatrix() m = tensor.fmatrix()
c = tensor.concatenate([m]) c = tensor.concatenate([m])
f = theano.function(inputs=[m], outputs=[c], f = theano.function(inputs=[m], outputs=[c],
...@@ -3970,7 +3941,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3970,7 +3941,7 @@ class T_Join_and_Split(unittest.TestCase):
mode=self.mode) mode=self.mode)
def test_join_matrixV(self): def test_join_matrixV(self):
"""variable join axis""" # variable join axis
v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX) v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX)
a = self.shared(v) a = self.shared(v)
b = as_tensor_variable(v) b = as_tensor_variable(v)
...@@ -3996,7 +3967,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -3996,7 +3967,7 @@ class T_Join_and_Split(unittest.TestCase):
utt.verify_grad(lambda a, b: join(1, a, b), [v, 2 * v], mode=self.mode) utt.verify_grad(lambda a, b: join(1, a, b), [v, 2 * v], mode=self.mode)
def test_join_matrixV_negative_axis(self): def test_join_matrixV_negative_axis(self):
"""variable join negative axis""" # variable join negative axis
v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX) v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX)
a = self.shared(v) a = self.shared(v)
b = as_tensor_variable(v) b = as_tensor_variable(v)
...@@ -4022,7 +3993,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -4022,7 +3993,7 @@ class T_Join_and_Split(unittest.TestCase):
self.assertRaises(IndexError, f, -3) self.assertRaises(IndexError, f, -3)
def test_join_matrixC_negative_axis(self): def test_join_matrixC_negative_axis(self):
"""constant join negative axis""" # constant join negative axis
v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX) v = numpy.array([[.1, .2, .3], [.4, .5, .6]], dtype=self.floatX)
a = self.shared(v) a = self.shared(v)
b = as_tensor_variable(v) b = as_tensor_variable(v)
...@@ -4071,11 +4042,9 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -4071,11 +4042,9 @@ class T_Join_and_Split(unittest.TestCase):
assert numpy.allclose(f(4, 5), [5, 9, 4]) assert numpy.allclose(f(4, 5), [5, 9, 4])
def test_broadcastable_flag_assignment_mixed_otheraxes(self): def test_broadcastable_flag_assignment_mixed_otheraxes(self):
""" # Test that the broadcastable flags for the output of
Test that the broadcastable flags for the output of # a join operation on non-join axes are True if one or
a join operation on non-join axes are True if one or # more inputs is broadcastable on that dimension.
more inputs is broadcastable on that dimension.
"""
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = numpy.random.RandomState(seed=utt.fetch_seed())
a_val = rng.rand(1, 4, 1).astype(self.floatX) a_val = rng.rand(1, 4, 1).astype(self.floatX)
b_val = rng.rand(1, 3, 1).astype(self.floatX) b_val = rng.rand(1, 3, 1).astype(self.floatX)
...@@ -4112,11 +4081,9 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -4112,11 +4081,9 @@ class T_Join_and_Split(unittest.TestCase):
self.assertRaises(ValueError, f) self.assertRaises(ValueError, f)
def test_broadcastable_flag_assignment_mixed_thisaxes(self): def test_broadcastable_flag_assignment_mixed_thisaxes(self):
""" # Test that the broadcastable flag of the join axis
Test that the broadcastable flag of the join axis # is False when some inputs are broadcastable on that
is False when some inputs are broadcastable on that # dimension.
dimension.
"""
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = numpy.random.RandomState(seed=utt.fetch_seed())
a_val = rng.rand(2, 4, 1).astype(self.floatX) a_val = rng.rand(2, 4, 1).astype(self.floatX)
b_val = rng.rand(1, 4, 1).astype(self.floatX) b_val = rng.rand(1, 4, 1).astype(self.floatX)
...@@ -4146,11 +4113,9 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -4146,11 +4113,9 @@ class T_Join_and_Split(unittest.TestCase):
self.assertRaises(TypeError, f, a_val, bad_b_val) self.assertRaises(TypeError, f, a_val, bad_b_val)
def test_broadcastable_flags_all_broadcastable_on_joinaxis(self): def test_broadcastable_flags_all_broadcastable_on_joinaxis(self):
""" # Test that joining together several inputs which are all
Test that joining together several inputs which are all # broadcastable on the join dimension results in the output
broadcastable on the join dimension results in the output # being non-broadcastable on the join dimension.
being non-broadcastable on the join dimension.
"""
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = numpy.random.RandomState(seed=utt.fetch_seed())
a_val = rng.rand(1, 4, 1).astype(self.floatX) a_val = rng.rand(1, 4, 1).astype(self.floatX)
b_val = rng.rand(1, 4, 1).astype(self.floatX) b_val = rng.rand(1, 4, 1).astype(self.floatX)
...@@ -4340,13 +4305,12 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -4340,13 +4305,12 @@ class T_Join_and_Split(unittest.TestCase):
self.assertRaises(ValueError, f) self.assertRaises(ValueError, f)
def test_join_inplace(): def test_join_inplace():
"""Test join to work inplace. # Test join to work inplace.
#
This function tests the case when several elements are passed to the # This function tests the case when several elements are passed to the
join function but all except one of them are empty. In this case join # join function but all except one of them are empty. In this case join
should work inplace and the output should be the view of the non-empty # should work inplace and the output should be the view of the non-empty
element. # element.
"""
s = tensor.lscalar() s = tensor.lscalar()
x = tensor.vector('x') x = tensor.vector('x')
z = tensor.zeros((s,)) z = tensor.zeros((s,))
...@@ -4365,13 +4329,12 @@ def test_join_inplace(): ...@@ -4365,13 +4329,12 @@ def test_join_inplace():
def test_join_oneInput(): def test_join_oneInput():
"""Test join when only 1 input is given. # Test join when only 1 input is given.
#
This functions tests the case when concatenate is called # This functions tests the case when concatenate is called
on an array of tensors but the array has only one element. # on an array of tensors but the array has only one element.
In this case, we would like to avoid the computational # In this case, we would like to avoid the computational
overhead of concatenation of one element. # overhead of concatenation of one element.
"""
x_0 = theano.tensor.fmatrix() x_0 = theano.tensor.fmatrix()
x_1 = theano.tensor.fmatrix() x_1 = theano.tensor.fmatrix()
x_2 = theano.tensor.fvector() x_2 = theano.tensor.fvector()
...@@ -4384,15 +4347,13 @@ def test_join_oneInput(): ...@@ -4384,15 +4347,13 @@ def test_join_oneInput():
class test_comparison(unittest.TestCase): class test_comparison(unittest.TestCase):
"""Test <, >, <=, >=, == and != # Test <, >, <=, >=, == and !=
#
Test that we can do the comparison with different # Test that we can do the comparison with different
combination of tensor(shared and constant variable) with # combination of tensor(shared and constant variable) with
ndarray. ndarray cmp tensor was crashing. In a NumPy PR (should # ndarray. ndarray cmp tensor was crashing. In a NumPy PR (should
be in the release 1.8 of NumPy), it will work. So we assert it # be in the release 1.8 of NumPy), it will work. So we assert it
work(futur behavior) or raise an error(current NumPy release). # work(futur behavior) or raise an error(current NumPy release).
"""
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
self.mode = None self.mode = None
...@@ -4810,7 +4771,7 @@ class test_matinv(unittest.TestCase): ...@@ -4810,7 +4771,7 @@ class test_matinv(unittest.TestCase):
return ssd0, ssd return ssd0, ssd
def test_reciprocal(self): def test_reciprocal(self):
"""Matrix reciprocal by gradient descent""" # Matrix reciprocal by gradient descent
ssd0, ssd = self.mat_reciprocal(3) ssd0, ssd = self.mat_reciprocal(3)
utt.seed_rng() utt.seed_rng()
...@@ -5196,13 +5157,13 @@ class test_grad(unittest.TestCase): ...@@ -5196,13 +5157,13 @@ class test_grad(unittest.TestCase):
return self.gval0, self.gval1 return self.gval0, self.gval1
def test_1param(self): def test_1param(self):
"""grad: Test passing a single variable param""" # grad: Test passing a single variable param
o = test_grad.O() o = test_grad.O()
a1 = o.make_node() a1 = o.make_node()
self.assertTrue(o.gval0 is tensor.grad(a1.outputs[0], a1.inputs[0])) self.assertTrue(o.gval0 is tensor.grad(a1.outputs[0], a1.inputs[0]))
def test_Nparam(self): def test_Nparam(self):
"""grad: Test passing multiple variable params""" # grad: Test passing multiple variable params
o = test_grad.O() o = test_grad.O()
a1 = o.make_node() a1 = o.make_node()
g0, g1 = grad(a1.outputs[0], a1.inputs) g0, g1 = grad(a1.outputs[0], a1.inputs)
...@@ -5211,12 +5172,12 @@ class test_grad(unittest.TestCase): ...@@ -5211,12 +5172,12 @@ class test_grad(unittest.TestCase):
self.assertTrue(o.gval1 is g1) self.assertTrue(o.gval1 is g1)
def test_grad_keep_type(self): def test_grad_keep_type(self):
"""Tests that the theano grad method returns a list if it is passed a list # Tests that the theano grad method returns a list if it is passed a list
and a single variable if it is passed a single variable. # and a single variable if it is passed a single variable.
pylearn2 depends on theano behaving this way. This functionality has been # pylearn2 depends on theano behaving this way. This functionality has been
added three times and erroneously removed twice. If you do anything that # added three times and erroneously removed twice. If you do anything that
requires changing this test or making it fail you are almost certainly # requires changing this test or making it fail you are almost certainly
making a common mistake, NOT fixing something. """ # making a common mistake, NOT fixing something.
X = tensor.matrix() X = tensor.matrix()
y = X.sum() y = X.sum()
...@@ -5230,7 +5191,7 @@ class test_grad(unittest.TestCase): ...@@ -5230,7 +5191,7 @@ class test_grad(unittest.TestCase):
assert not isinstance(G, list) assert not isinstance(G, list)
def test_1None_rval(self): def test_1None_rval(self):
"""grad: Test returning a single zero value from grad""" # grad: Test returning a single zero value from grad
o = test_grad.O() o = test_grad.O()
a1 = o.make_node() a1 = o.make_node()
g = grad(a1.outputs[0], a1.outputs[1], g = grad(a1.outputs[0], a1.outputs[1],
...@@ -5240,7 +5201,7 @@ class test_grad(unittest.TestCase): ...@@ -5240,7 +5201,7 @@ class test_grad(unittest.TestCase):
self.assertRaises(TypeError, grad, a1.outputs[0], 'wtf') self.assertRaises(TypeError, grad, a1.outputs[0], 'wtf')
def test_NNone_rval(self): def test_NNone_rval(self):
"""grad: Test returning some zero value from grad""" # grad: Test returning some zero value from grad
o = test_grad.O() o = test_grad.O()
a1 = o.make_node() a1 = o.make_node()
g0, g1, g2 = grad(a1.outputs[0], a1.inputs + [scalar('z')], g0, g1, g2 = grad(a1.outputs[0], a1.inputs + [scalar('z')],
...@@ -5251,7 +5212,7 @@ class test_grad(unittest.TestCase): ...@@ -5251,7 +5212,7 @@ class test_grad(unittest.TestCase):
self.assertTrue(g2.owner.inputs[1].data == 0) self.assertTrue(g2.owner.inputs[1].data == 0)
def test_zero_gradient_shape(self): def test_zero_gradient_shape(self):
"""Ensure that a zero gradient has the proper shape.""" # Ensure that a zero gradient has the proper shape.
x = dmatrix() x = dmatrix()
f = theano.function([x], grad(dscalar(), x, f = theano.function([x], grad(dscalar(), x,
disconnected_inputs='ignore')) disconnected_inputs='ignore'))
...@@ -5260,7 +5221,7 @@ class test_grad(unittest.TestCase): ...@@ -5260,7 +5221,7 @@ class test_grad(unittest.TestCase):
self.assertTrue(a.shape == f(a).shape) # With proper shape. self.assertTrue(a.shape == f(a).shape) # With proper shape.
def test_cost_is_scalar(self): def test_cost_is_scalar(self):
'''grad: Test that a non-scalar cost raises a TypeError''' # grad: Test that a non-scalar cost raises a TypeError
s = scalar() s = scalar()
v = vector() v = vector()
m = matrix() m = matrix()
...@@ -5274,8 +5235,7 @@ class T_op_cache(unittest.TestCase): ...@@ -5274,8 +5235,7 @@ class T_op_cache(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
"""trigger bug in ticket #162 # trigger bug in ticket #162
"""
lr = constant(0.011) lr = constant(0.011)
v = matrix() v = matrix()
v.name = 'v' v.name = 'v'
...@@ -5565,11 +5525,10 @@ def test_flatten_outdim_invalid(): ...@@ -5565,11 +5525,10 @@ def test_flatten_outdim_invalid():
def test_is_flat(): def test_is_flat():
""" # tests is_flat method for constant and symbolic variables,
tests is_flat method for constant and symbolic variables, # as well as reshaped constant and symbolic variables on the
as well as reshaped constant and symbolic variables on the # given outdim
given outdim
"""
# Constant variable # Constant variable
assert tensor.is_flat(tensor.as_tensor_variable(numpy.zeros((10)))) assert tensor.is_flat(tensor.as_tensor_variable(numpy.zeros((10))))
assert tensor.is_flat(tensor.as_tensor_variable(numpy.zeros((10, 10, 10))), assert tensor.is_flat(tensor.as_tensor_variable(numpy.zeros((10, 10, 10))),
...@@ -5760,7 +5719,7 @@ class TestARange(unittest.TestCase): ...@@ -5760,7 +5719,7 @@ class TestARange(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def test_Op_integers(self): def test_Op_integers(self):
"""Test behaviour of ARange Op on integer inputs""" # Test behaviour of ARange Op on integer inputs
start, stop, step = iscalars('start', 'stop', 'step') start, stop, step = iscalars('start', 'stop', 'step')
out = ARange(start.type.dtype)(start, stop, step) out = ARange(start.type.dtype)(start, stop, step)
f = function([start, stop, step], out) f = function([start, stop, step], out)
...@@ -5773,7 +5732,7 @@ class TestARange(unittest.TestCase): ...@@ -5773,7 +5732,7 @@ class TestARange(unittest.TestCase):
assert numpy.all(f(0, 0, 1) == numpy.arange(0, 0, 1)) assert numpy.all(f(0, 0, 1) == numpy.arange(0, 0, 1))
def test_integers(self): def test_integers(self):
"""Test arange constructor, on integer outputs""" # Test arange constructor, on integer outputs
start, stop, step = iscalars('start', 'stop', 'step') start, stop, step = iscalars('start', 'stop', 'step')
out = arange(start, stop, step) out = arange(start, stop, step)
f = function([start, stop, step], out) f = function([start, stop, step], out)
...@@ -5793,7 +5752,7 @@ class TestARange(unittest.TestCase): ...@@ -5793,7 +5752,7 @@ class TestARange(unittest.TestCase):
assert numpy.all(f(0, 0, 1) == numpy.arange(0, 0, 1)) assert numpy.all(f(0, 0, 1) == numpy.arange(0, 0, 1))
def test_float32(self): def test_float32(self):
"""Test arange constructor, on float32 outputs""" # Test arange constructor, on float32 outputs
start, stop, step = fscalars('start', 'stop', 'step') start, stop, step = fscalars('start', 'stop', 'step')
out = arange(start, stop, step) out = arange(start, stop, step)
f = function([start, stop, step], out) f = function([start, stop, step], out)
...@@ -5827,7 +5786,7 @@ class TestARange(unittest.TestCase): ...@@ -5827,7 +5786,7 @@ class TestARange(unittest.TestCase):
assert numpy.all(f_val == expected_val) assert numpy.all(f_val == expected_val)
def test_float64(self): def test_float64(self):
"""Test arange constructor, on float64 outputs""" # Test arange constructor, on float64 outputs
start, stop, step = dscalars('start', 'stop', 'step') start, stop, step = dscalars('start', 'stop', 'step')
out = arange(start, stop, step) out = arange(start, stop, step)
f = function([start, stop, step], out) f = function([start, stop, step], out)
...@@ -5850,7 +5809,7 @@ class TestARange(unittest.TestCase): ...@@ -5850,7 +5809,7 @@ class TestARange(unittest.TestCase):
assert numpy.all(f_val == expected_val) assert numpy.all(f_val == expected_val)
def test_default_step(self): def test_default_step(self):
"""Test that arange constructor uses the correct default step""" # Test that arange constructor uses the correct default step
start, stop = iscalars('start', 'stop') start, stop = iscalars('start', 'stop')
out = arange(start, stop) out = arange(start, stop)
f = function([start, stop], out) f = function([start, stop], out)
...@@ -5878,7 +5837,7 @@ class TestARange(unittest.TestCase): ...@@ -5878,7 +5837,7 @@ class TestARange(unittest.TestCase):
assert numpy.all(df(-0.7, 5.3) == numpy.arange(-0.7, 5.3)) assert numpy.all(df(-0.7, 5.3) == numpy.arange(-0.7, 5.3))
def test_default_start(self): def test_default_start(self):
"""Test that arange constructor uses the correct default start""" # Test that arange constructor uses the correct default start
stop = iscalar('stop') stop = iscalar('stop')
out = arange(stop) out = arange(stop)
f = function([stop], out) f = function([stop], out)
...@@ -5914,7 +5873,7 @@ class TestARange(unittest.TestCase): ...@@ -5914,7 +5873,7 @@ class TestARange(unittest.TestCase):
assert numpy.all(ff(fstop_v32) == numpy.arange(fstop_v)) assert numpy.all(ff(fstop_v32) == numpy.arange(fstop_v))
def test_upcast(self): def test_upcast(self):
"""Test that arange computes output type adequately""" # Test that arange computes output type adequately
if config.cast_policy == 'custom': if config.cast_policy == 'custom':
assert arange(iscalar()).dtype == 'int64' assert arange(iscalar()).dtype == 'int64'
assert arange(fscalar()).dtype == fscalar().dtype assert arange(fscalar()).dtype == fscalar().dtype
...@@ -5986,8 +5945,8 @@ class TestARange(unittest.TestCase): ...@@ -5986,8 +5945,8 @@ class TestARange(unittest.TestCase):
raise NotImplementedError(config.cast_policy) raise NotImplementedError(config.cast_policy)
def test_dtype_cache(self): def test_dtype_cache(self):
"""Checks that the same Op is returned on repeated calls to arange # Checks that the same Op is returned on repeated calls to arange
using the same dtype, but not for different dtypes.""" # using the same dtype, but not for different dtypes.
start, stop, step = iscalars('start', 'stop', 'step') start, stop, step = iscalars('start', 'stop', 'step')
out1 = arange(start, stop, step) out1 = arange(start, stop, step)
...@@ -6125,7 +6084,7 @@ class TestInversePermutation(unittest.TestCase): ...@@ -6125,7 +6084,7 @@ class TestInversePermutation(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def test_dim1(self): def test_dim1(self):
"""Test the inversion of one permutation (int vector)""" # Test the inversion of one permutation (int vector)
p = ivector() p = ivector()
inv = inverse_permutation(p) inv = inverse_permutation(p)
assert inv.dtype == p.dtype assert inv.dtype == p.dtype
...@@ -6143,7 +6102,7 @@ class TestInversePermutation(unittest.TestCase): ...@@ -6143,7 +6102,7 @@ class TestInversePermutation(unittest.TestCase):
assert numpy.all(inv_val[p_val] == numpy.arange(10)) assert numpy.all(inv_val[p_val] == numpy.arange(10))
def test_dim2(self): def test_dim2(self):
"""Test the inversion of several permutations at a time""" # Test the inversion of several permutations at a time
# Each row of p is a different permutation to inverse # Each row of p is a different permutation to inverse
p = imatrix() p = imatrix()
inv = inverse_permutation(p) inv = inverse_permutation(p)
...@@ -6169,7 +6128,7 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -6169,7 +6128,7 @@ class TestPermuteRowElements(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def test_1_1(self): def test_1_1(self):
"""Test PermuteRowElements(vector, vector)""" # Test PermuteRowElements(vector, vector)
input = dvector() input = dvector()
p = ivector() p = ivector()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
...@@ -6186,12 +6145,12 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -6186,12 +6145,12 @@ class TestPermuteRowElements(unittest.TestCase):
# Verify gradient # Verify gradient
def permute_fixed(s_input): def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val""" # Auxiliary op defined to get rid of gradient wrt p_val
return permute_row_elements(s_input, p_val) return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val]) utt.verify_grad(permute_fixed, [input_val])
def test_2_1(self): def test_2_1(self):
"""Test broadcasting in PermuteRowElements(matrix, vector)""" # Test broadcasting in PermuteRowElements(matrix, vector)
input = matrix() input = matrix()
p = ivector() p = ivector()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
...@@ -6208,12 +6167,12 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -6208,12 +6167,12 @@ class TestPermuteRowElements(unittest.TestCase):
# Verify gradient # Verify gradient
def permute_fixed(s_input): def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val""" # Auxiliary op defined to get rid of gradient wrt p_val
return permute_row_elements(s_input, p_val) return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val]) utt.verify_grad(permute_fixed, [input_val])
def test_2_2(self): def test_2_2(self):
"""Test PermuteRowElements(matrix, matrix)""" # Test PermuteRowElements(matrix, matrix)
input = matrix() input = matrix()
p = imatrix() p = imatrix()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
...@@ -6233,13 +6192,13 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -6233,13 +6192,13 @@ class TestPermuteRowElements(unittest.TestCase):
# Verify gradient # Verify gradient
def permute_fixed(s_input): def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val""" # Auxiliary op defined to get rid of gradient wrt p_val
return permute_row_elements(s_input, p_val) return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val]) utt.verify_grad(permute_fixed, [input_val])
def test_1_2(self): def test_1_2(self):
"""Test PermuteRowElements(vector, matrix) # Test PermuteRowElements(vector, matrix)
Different permutations will be applied to the same input vector""" # Different permutations will be applied to the same input vector
input = vector() input = vector()
p = imatrix() p = imatrix()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
...@@ -6257,14 +6216,14 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -6257,14 +6216,14 @@ class TestPermuteRowElements(unittest.TestCase):
# Verify gradient # Verify gradient
def permute_fixed(s_input): def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val""" # Auxiliary op defined to get rid of gradient wrt p_val
return permute_row_elements(s_input, p_val) return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val]) utt.verify_grad(permute_fixed, [input_val])
def test_3b_2(self): def test_3b_2(self):
"""Test permute_row_elements on a more complex broadcasting pattern: # Test permute_row_elements on a more complex broadcasting pattern:
input.type.broadcastable = (False, True, False), # input.type.broadcastable = (False, True, False),
p.type.broadcastable = (False, False).""" # p.type.broadcastable = (False, False).
input = TensorType('floatX', (False, True, False))() input = TensorType('floatX', (False, True, False))()
p = imatrix() p = imatrix()
...@@ -6285,17 +6244,15 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -6285,17 +6244,15 @@ class TestPermuteRowElements(unittest.TestCase):
# Verify gradient # Verify gradient
def permute_fixed(s_input): def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val""" # Auxiliary op defined to get rid of gradient wrt p_val
return permute_row_elements(s_input, p_val) return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val]) utt.verify_grad(permute_fixed, [input_val])
class test_tensordot(unittest.TestCase): class test_tensordot(unittest.TestCase):
def TensorDot(self, axes): def TensorDot(self, axes):
""" # Since tensordot is no longer an op, mimic the old op signature
Since tensordot is no longer an op, mimic the old op signature # to allow easy use of verify_grad.
to allow easy use of verify_grad.
"""
return lambda a, b: tensordot(a, b, axes) return lambda a, b: tensordot(a, b, axes)
def setUp(self): def setUp(self):
...@@ -6563,7 +6520,7 @@ def test_var(): ...@@ -6563,7 +6520,7 @@ def test_var():
class T_sum(unittest.TestCase): class T_sum(unittest.TestCase):
def test_sum_overflow(self): def test_sum_overflow(self):
"""Ensure that overflow errors are a little bit harder to get""" # Ensure that overflow errors are a little bit harder to get
a = Tensor(dtype='int8', broadcastable=[False])() a = Tensor(dtype='int8', broadcastable=[False])()
f = function([a], sum(a)) f = function([a], sum(a))
assert f([1] * 300) == 300 assert f([1] * 300) == 300
...@@ -6620,7 +6577,7 @@ def test_autocast(): ...@@ -6620,7 +6577,7 @@ def test_autocast():
def _test_autocast_custom(): def _test_autocast_custom():
"""Called from `test_autocast`.""" # Called from `test_autocast`.
assert config.cast_policy == 'custom' assert config.cast_policy == 'custom'
orig_autocast = autocast_float.dtypes orig_autocast = autocast_float.dtypes
...@@ -6689,7 +6646,7 @@ def _test_autocast_custom(): ...@@ -6689,7 +6646,7 @@ def _test_autocast_custom():
def _test_autocast_numpy(): def _test_autocast_numpy():
"""Called from `test_autocast`.""" # Called from `test_autocast`.
assert config.cast_policy == 'numpy' assert config.cast_policy == 'numpy'
# Go through some typical scalar values. # Go through some typical scalar values.
...@@ -6708,7 +6665,7 @@ def _test_autocast_numpy(): ...@@ -6708,7 +6665,7 @@ def _test_autocast_numpy():
def _test_autocast_numpy_floatX(): def _test_autocast_numpy_floatX():
"""Called from `test_autocast`.""" # Called from `test_autocast`.
assert config.cast_policy == 'numpy+floatX' assert config.cast_policy == 'numpy+floatX'
backup_floatX = config.floatX backup_floatX = config.floatX
...@@ -6742,15 +6699,11 @@ def _test_autocast_numpy_floatX(): ...@@ -6742,15 +6699,11 @@ def _test_autocast_numpy_floatX():
class test_arithmetic_cast(unittest.TestCase): class test_arithmetic_cast(unittest.TestCase):
# Test output types of basic arithmeric operations (* / + - //).
""" #
Test output types of basic arithmeric operations (* / + - //). # We only test the behavior for `config.cast_policy` set to either 'numpy' or
# 'numpy+floatX': the 'custom' behavior is (at least partially) tested in
We only test the behavior for `config.cast_policy` set to either 'numpy' or # `_test_autocast_custom`.
'numpy+floatX': the 'custom' behavior is (at least partially) tested in
`_test_autocast_custom`.
"""
def test_arithmetic_cast(self): def test_arithmetic_cast(self):
backup_config = config.cast_policy backup_config = config.cast_policy
dtypes = get_numeric_types(with_complex=True) dtypes = get_numeric_types(with_complex=True)
...@@ -6920,10 +6873,8 @@ class test_broadcast(unittest.TestCase): ...@@ -6920,10 +6873,8 @@ class test_broadcast(unittest.TestCase):
self.assertRaises(ValueError, f) self.assertRaises(ValueError, f)
def test_unbroadcast_addbroadcast(self): def test_unbroadcast_addbroadcast(self):
""" # test that the unbroadcast fct don't insert not needed broadcast
test that the unbroadcast fct don't insert not needed broadcast # and fuse consecutive Rebroadcast op
and fuse consecutive Rebroadcast op
"""
x = matrix() x = matrix()
assert unbroadcast(x, 0) is x assert unbroadcast(x, 0) is x
...@@ -7014,11 +6965,9 @@ def test_len(): ...@@ -7014,11 +6965,9 @@ def test_len():
def test_mod(): def test_mod():
""" # We add this test as not all language and C implementation give the same
We add this test as not all language and C implementation give the same # sign to the result. This check that the c_code of `Mod` is implemented
sign to the result. This check that the c_code of `Mod` is implemented # as Python. That is what we want.
as Python. That is what we want.
"""
x, y = fscalars('xy') x, y = fscalars('xy')
fn = gof.DualLinker().accept( fn = gof.DualLinker().accept(
gof.FunctionGraph([x, y], [x % y])).make_function() gof.FunctionGraph([x, y], [x % y])).make_function()
...@@ -7030,9 +6979,7 @@ def test_mod(): ...@@ -7030,9 +6979,7 @@ def test_mod():
def test_divmod(): def test_divmod():
""" # Confirm that divmod is equivalent to the python version.
Confirm that divmod is equivalent to the python version.
"""
x, y = fscalars('xy') x, y = fscalars('xy')
d, r = divmod(x, y) d, r = divmod(x, y)
fn = gof.DualLinker().accept( fn = gof.DualLinker().accept(
...@@ -7047,21 +6994,18 @@ def test_divmod(): ...@@ -7047,21 +6994,18 @@ def test_divmod():
def test_mod_compile(): def test_mod_compile():
""" # This test generate an Elemwise of Composite as:
This test generate an Elemwise of Composite as: # Elemwise{
Elemwise{ # Composite{
Composite{ # Composite{
Composite{ # Composite{
Composite{ # Composite{mod,EQ},
Composite{mod,EQ}, # Switch},
Switch}, # mul},
mul}, # add}}
add}} #
# The c_code generated is not compiling as of 30 June 2010. I fix the
The c_code generated is not compiling as of 30 June 2010. I fix the # compilation in the same commit.
compilation in the same commit.
"""
x = tensor.vector() x = tensor.vector()
y = tensor.vector() y = tensor.vector()
shape = x.shape shape = x.shape
...@@ -7260,12 +7204,9 @@ class T_get_scalar_constant_value(unittest.TestCase): ...@@ -7260,12 +7204,9 @@ class T_get_scalar_constant_value(unittest.TestCase):
class T_as_tensor_variable(unittest.TestCase): class T_as_tensor_variable(unittest.TestCase):
""" # We test that ticket #649 stay fixed.
We test that ticket #649 stay fixed. # We should not allow as_tensor_variable to accept True or False
We should not allow as_tensor_variable to accept True or False # But it should upcast an ndarray of bool to uint8
But it should upcast an ndarray of bool to uint8
"""
def test_bool(self): def test_bool(self):
self.assertRaises(TypeError, as_tensor_variable, True) self.assertRaises(TypeError, as_tensor_variable, True)
self.assertRaises(TypeError, as_tensor_variable, False) self.assertRaises(TypeError, as_tensor_variable, False)
...@@ -7294,8 +7235,7 @@ class T_as_tensor_variable(unittest.TestCase): ...@@ -7294,8 +7235,7 @@ class T_as_tensor_variable(unittest.TestCase):
class test_complex_mod(unittest.TestCase): class test_complex_mod(unittest.TestCase):
"""Make sure % fails on complex numbers.""" # Make sure % fails on complex numbers.
def test_fail(self): def test_fail(self):
x = vector(dtype='complex64') x = vector(dtype='complex64')
try: try:
...@@ -7306,10 +7246,7 @@ class test_complex_mod(unittest.TestCase): ...@@ -7306,10 +7246,7 @@ class test_complex_mod(unittest.TestCase):
class test_size(unittest.TestCase): class test_size(unittest.TestCase):
""" # Ensure the `size` attribute of tensors behaves as in numpy.
Ensure the `size` attribute of tensors behaves as in numpy.
"""
def test_matrix(self): def test_matrix(self):
x = tensor.matrix() x = tensor.matrix()
y = numpy.zeros((5, 7), dtype=config.floatX) y = numpy.zeros((5, 7), dtype=config.floatX)
...@@ -7333,22 +7270,19 @@ class test_size(unittest.TestCase): ...@@ -7333,22 +7270,19 @@ class test_size(unittest.TestCase):
class test_diag(unittest.TestCase): class test_diag(unittest.TestCase):
""" # Test that tensor.diag has the same behavior as numpy.diag.
Test that tensor.diag has the same behavior as numpy.diag. # numpy.diag has two behaviors:
#
numpy.diag has two behaviors: # (1) when given a vector, it returns a matrix with that vector as the
(1) when given a vector, it returns a matrix with that vector as the # diagonal.
diagonal. # (2) when given a matrix, returns a vector which is the diagonal of the
(2) when given a matrix, returns a vector which is the diagonal of the # matrix.
matrix. #
# (1) and (2) are tested by test_alloc_diag and test_extract_diag
(1) and (2) are tested by test_alloc_diag and test_extract_diag # respectively.
respectively. #
# test_diag test makes sure that linalg.diag instantiates
test_diag test makes sure that linalg.diag instantiates # the right op based on the dimension of the input.
the right op based on the dimension of the input.
"""
def __init__(self, name, mode=None, shared=tensor._shared, def __init__(self, name, mode=None, shared=tensor._shared,
floatX=None, type=tensor.TensorType): floatX=None, type=tensor.TensorType):
self.mode = mode self.mode = mode
...@@ -7427,28 +7361,21 @@ class test_diag(unittest.TestCase): ...@@ -7427,28 +7361,21 @@ class test_diag(unittest.TestCase):
class test_numpy_assumptions(unittest.TestCase): class test_numpy_assumptions(unittest.TestCase):
""" # Verify that some assumptions Theano makes on Numpy's behavior still hold.
Verify that some assumptions Theano makes on Numpy's behavior still hold.
"""
def test_ndarray_copy(self): def test_ndarray_copy(self):
""" # A copy or deepcopy of the ndarray type should not create a new object.
A copy or deepcopy of the ndarray type should not create a new object. #
# This is because Theano makes some comparisons of the form:
This is because Theano makes some comparisons of the form: # if type(x) is numpy.ndarray
if type(x) is numpy.ndarray
"""
assert copy(numpy.ndarray) is numpy.ndarray assert copy(numpy.ndarray) is numpy.ndarray
assert deepcopy(numpy.ndarray) is numpy.ndarray assert deepcopy(numpy.ndarray) is numpy.ndarray
def test_dtype_equality(self): def test_dtype_equality(self):
""" # Ensure dtype string comparisons are consistent.
Ensure dtype string comparisons are consistent. #
# Theano often uses string representations of dtypes (e.g. 'float32'). We
Theano often uses string representations of dtypes (e.g. 'float32'). We # need to make sure that comparing the string representations is the same
need to make sure that comparing the string representations is the same # as comparing the dtype objects themselves.
as comparing the dtype objects themselves.
"""
dtypes = get_numeric_types(with_complex=True) dtypes = get_numeric_types(with_complex=True)
# Perform all pairwise comparisons of dtypes, making sure comparing # Perform all pairwise comparisons of dtypes, making sure comparing
# their string representation yields the same result. # their string representation yields the same result.
...@@ -7541,8 +7468,8 @@ class TestSpecifyShape(unittest.TestCase): ...@@ -7541,8 +7468,8 @@ class TestSpecifyShape(unittest.TestCase):
return None return None
def test_bad_shape(self): def test_bad_shape(self):
""" Test that at run time we raise an exception when the shape # Test that at run time we raise an exception when the shape
is not the one specified""" # is not the one specified
specify_shape = SpecifyShape() specify_shape = SpecifyShape()
x = vector() x = vector()
...@@ -7568,7 +7495,7 @@ class TestSpecifyShape(unittest.TestCase): ...@@ -7568,7 +7495,7 @@ class TestSpecifyShape(unittest.TestCase):
self.assertRaises(AssertionError, f, xval) self.assertRaises(AssertionError, f, xval)
def test_bad_number_of_shape(self): def test_bad_number_of_shape(self):
""" Test that the number of dimensions provided is good""" # Test that the number of dimensions provided is good
specify_shape = SpecifyShape() specify_shape = SpecifyShape()
x = vector() x = vector()
...@@ -8147,9 +8074,7 @@ def test_norm(): ...@@ -8147,9 +8074,7 @@ def test_norm():
class test_ptp(unittest.TestCase): class test_ptp(unittest.TestCase):
def test_scalar(self): def test_scalar(self):
""" # Should return 0 for all scalar
Should return 0 for all scalar
"""
x = scalar('x') x = scalar('x')
p = ptp(x) p = ptp(x)
f = theano.function([x], p) f = theano.function([x], p)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论