提交 4a167629 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merge pull request #289 from nouiz/fix_import_floatX

Fix an import case and made more test run in floatX
...@@ -502,6 +502,26 @@ Final version ...@@ -502,6 +502,26 @@ Final version
double = Double() double = Double()
DeepCopyOp
==========
We have an internal Op called DeepCopyOp. It is used to make sure we
respect the user vs Theano memory region as described in the :ref:`tutorial
<aliasing>`. Theano has a Python implementation that calls the object's
``copy()`` or ``deepcopy()`` method for Theano types for which it does not
know how to generate C code.
You can implement c_code for this op. You register it like this:
.. code-block:: python
theano.compile.function_module.register_DeepCopyOp_c_code(YOUR_TYPE_CLASS, THE_C_CODE)
In your C code, you should use %(iname)s and %(oname)s to represent
the C variable names of the DeepCopyOp input and output
respectively. See an example for the type ``CudaNdarrayType`` (GPU array)
in the file `theano/sandbox/cuda/type.py`.
Output Guard Output Guard
============ ============
......
...@@ -129,7 +129,21 @@ class AliasedMemoryError(Exception): ...@@ -129,7 +129,21 @@ class AliasedMemoryError(Exception):
### Function ### Function
### ###
def register_DeepCopyOp_c_code(typ, code):
""" Tell DeepCopyOp how to generate C code for a Theano Type
:param typ: A Theano type. It must be the Theano class itself and not an
instance of the class.
:param code: C code that deep copies the Theano type 'typ'.
Use %(iname)s and %(oname)s for the input and output C
variable names respectively.
"""
DeepCopyOp.c_codes[typ] = code
class DeepCopyOp(theano.gof.Op): class DeepCopyOp(theano.gof.Op):
c_codes = {} # Theano Type, code
def __init__(self): def __init__(self):
pass pass
...@@ -175,19 +189,8 @@ class DeepCopyOp(theano.gof.Op): ...@@ -175,19 +189,8 @@ class DeepCopyOp(theano.gof.Op):
} }
"""%locals() """%locals()
elif isinstance(node.inputs[0].type, theano.sandbox.cuda.CudaNdarrayType): elif node.inputs[0].type.__class__ in self.c_codes:
return """ return self.c_codes[node.inputs[0].type.__class__] % locals()
Py_XDECREF(%(oname)s);
%(oname)s = (CudaNdarray*)CudaNdarray_Copy(%(iname)s);
if (!%(oname)s)
{
PyErr_SetString(PyExc_ValueError, "DeepCopyOp: the copy failed!");
%(fail)s;
}
"""%locals()
else: else:
super(DeepCopyOp, self).c_code(node, name, inames, onames, sub) super(DeepCopyOp, self).c_code(node, name, inames, onames, sub)
......
""" """
This file test tensor op that should also operate on CudaNdaray. This file test tensor op that should also operate on CudaNdaray.
""" """
import numpy import copy
from nose.plugins.skip import SkipTest
from theano import tensor import numpy
import theano import theano
from theano import tensor
import theano.tensor as T import theano.tensor as T
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
if cuda.cuda_available == False: if cuda.cuda_available == False:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
...@@ -105,3 +106,24 @@ def test_may_share_memory_cuda(): ...@@ -105,3 +106,24 @@ def test_may_share_memory_cuda():
raise Exception("An error was expected") raise Exception("An error was expected")
except TypeError: except TypeError:
pass pass
def test_deepcopy():
a = cuda.fmatrix()
a_v = cuda.CudaNdarray(numpy.zeros((3, 4), dtype='float32'))
# We force the c code to check that we generate c code
mode = theano.Mode("c", mode_with_gpu.optimizer)
f = theano.function([a], a, mode=mode)
theano.printing.debugprint(f)
out = f(a_v)
assert out is not a_v
assert numpy.allclose(numpy.asarray(a_v), numpy.asarray(out))
# We force the python linker as the default code should work for this op
mode = theano.Mode("py", mode_with_gpu.optimizer)
f = theano.function([a], a, mode=mode)
theano.printing.debugprint(f)
out = f(a_v)
assert out is not a_v
assert numpy.allclose(numpy.asarray(a_v), numpy.asarray(out))
...@@ -356,6 +356,19 @@ class CudaNdarrayType(Type): ...@@ -356,6 +356,19 @@ class CudaNdarrayType(Type):
# to have OutputGuard generate C code for this type. # to have OutputGuard generate C code for this type.
theano.compile.mode.register_OutputGuard_c_code(CudaNdarrayType) theano.compile.mode.register_OutputGuard_c_code(CudaNdarrayType)
# Register CudaNdarrayType to the DeepCopyOp list of types with c code.
theano.compile.function_module.register_DeepCopyOp_c_code(CudaNdarrayType, """
Py_XDECREF(%(oname)s);
%(oname)s = (CudaNdarray*)CudaNdarray_Copy(%(iname)s);
if (!%(oname)s)
{
PyErr_SetString(PyExc_ValueError, "DeepCopyOp: the copy failed!");
%(fail)s;
}
""")
# THIS WORKS # THIS WORKS
# But CudaNdarray instances don't compare equal to one another, and what about __hash__ ? # But CudaNdarray instances don't compare equal to one another, and what about __hash__ ?
......
...@@ -1488,7 +1488,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1488,7 +1488,7 @@ class T_max_and_argmax(unittest.TestCase):
assert len(v) == 0 assert len(v) == 0
def test2(self): def test2(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None), for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None),
([0, 1], None), ([1, 0], None)]: ([0, 1], None), ([1, 0], None)]:
...@@ -1500,7 +1500,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1500,7 +1500,7 @@ class T_max_and_argmax(unittest.TestCase):
assert tuple(v_shape) == numpy.max(data, np_axis).shape assert tuple(v_shape) == numpy.max(data, np_axis).shape
def test2_invalid(self): def test2_invalid(self):
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
# Silence expected error messages # Silence expected error messages
_logger = logging.getLogger('theano.gof.opt') _logger = logging.getLogger('theano.gof.opt')
oldlevel = _logger.level oldlevel = _logger.level
...@@ -1515,7 +1515,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1515,7 +1515,7 @@ class T_max_and_argmax(unittest.TestCase):
_logger.setLevel(oldlevel) _logger.setLevel(oldlevel)
def test2_invalid_neg(self): def test2_invalid_neg(self):
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
old_stderr = sys.stderr old_stderr = sys.stderr
sys.stderr = StringIO.StringIO() sys.stderr = StringIO.StringIO()
try: try:
...@@ -1528,7 +1528,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1528,7 +1528,7 @@ class T_max_and_argmax(unittest.TestCase):
sys.stderr = old_stderr sys.stderr = old_stderr
def test2_valid_neg(self): def test2_valid_neg(self):
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
v, i = eval_outputs(max_and_argmax(n, -1)) v, i = eval_outputs(max_and_argmax(n, -1))
assert i.dtype == 'int64' assert i.dtype == 'int64'
self.assertTrue(v.shape == (2,)) self.assertTrue(v.shape == (2,))
...@@ -1547,7 +1547,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1547,7 +1547,7 @@ class T_max_and_argmax(unittest.TestCase):
assert v == (3) assert v == (3)
def test3(self): def test3(self):
data = numpy.random.rand(2, 3, 4) data = rand(2, 3, 4)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None), for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None),
([0, 1, 2], None), ([1, 2, 0], None)]: ([0, 1, 2], None), ([1, 2, 0], None)]:
...@@ -1559,7 +1559,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1559,7 +1559,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_grad(self): def test_grad(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
def check_grad_max(data, max_grad_data, axis=None): def check_grad_max(data, max_grad_data, axis=None):
...@@ -1595,8 +1595,17 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -1595,8 +1595,17 @@ class T_max_and_argmax(unittest.TestCase):
check_grad_max(data, eval_outputs(grad( check_grad_max(data, eval_outputs(grad(
max_and_argmax(n.flatten())[0], n))) max_and_argmax(n.flatten())[0], n)))
# Test 3d inner dimensions
data = rand(3, 4, 5)
for i in [0, 1, 2]:
utt.verify_grad(lambda v: max_and_argmax(v, axis=[i])[0], [data])
utt.verify_grad(lambda v: max_and_argmax(v, axis=[i])[1], [data])
# Test 4d inner dimensions # Test 4d inner dimensions
data = numpy.random.rand(2, 3, 4, 5) # Use float64 as otherwise the test don't pass.
data = rand(2, 3, 4, 5).astype("float64")
for i in [0, 1, 2, 3]: for i in [0, 1, 2, 3]:
utt.verify_grad(lambda v: max_and_argmax(v, axis=[i])[0], [data]) utt.verify_grad(lambda v: max_and_argmax(v, axis=[i])[0], [data])
utt.verify_grad(lambda v: max_and_argmax(v, axis=[i])[1], [data]) utt.verify_grad(lambda v: max_and_argmax(v, axis=[i])[1], [data])
...@@ -1629,7 +1638,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1629,7 +1638,7 @@ class T_argmin_argmax(unittest.TestCase):
assert len(v) == 0 assert len(v) == 0
def test2(self): def test2(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]: for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]:
for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None), for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None),
...@@ -1641,7 +1650,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1641,7 +1650,7 @@ class T_argmin_argmax(unittest.TestCase):
def test2_invalid(self): def test2_invalid(self):
for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]: for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]:
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
# Silence expected error messages # Silence expected error messages
_logger = logging.getLogger('theano.gof.opt') _logger = logging.getLogger('theano.gof.opt')
oldlevel = _logger.level oldlevel = _logger.level
...@@ -1657,7 +1666,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1657,7 +1666,7 @@ class T_argmin_argmax(unittest.TestCase):
def test2_invalid_neg(self): def test2_invalid_neg(self):
for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]: for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]:
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
old_stderr = sys.stderr old_stderr = sys.stderr
sys.stderr = StringIO.StringIO() sys.stderr = StringIO.StringIO()
try: try:
...@@ -1671,7 +1680,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1671,7 +1680,7 @@ class T_argmin_argmax(unittest.TestCase):
def test2_valid_neg(self): def test2_valid_neg(self):
for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]: for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]:
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
i = eval_outputs(fct(n, -1)) i = eval_outputs(fct(n, -1))
self.assertTrue(i.shape == (2,)) self.assertTrue(i.shape == (2,))
self.assertTrue(numpy.all(i == nfct(n.value, -1))) self.assertTrue(numpy.all(i == nfct(n.value, -1)))
...@@ -1685,7 +1694,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1685,7 +1694,7 @@ class T_argmin_argmax(unittest.TestCase):
assert v == (3) assert v == (3)
def test3(self): def test3(self):
data = numpy.random.rand(2, 3, 4) data = rand(2, 3, 4)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]: for fct, nfct in [(argmax, numpy.argmax), (argmin, numpy.argmin)]:
for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (2, 2), for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (2, 2),
...@@ -1697,7 +1706,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1697,7 +1706,7 @@ class T_argmin_argmax(unittest.TestCase):
assert tuple(v_shape) == nfct(data, np_axis).shape assert tuple(v_shape) == nfct(data, np_axis).shape
def test_grad_argmin(self): def test_grad_argmin(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
#test grad of argmin #test grad of argmin
...@@ -1716,7 +1725,7 @@ class T_argmin_argmax(unittest.TestCase): ...@@ -1716,7 +1725,7 @@ class T_argmin_argmax(unittest.TestCase):
pass pass
def test_grad_argmax(self): def test_grad_argmax(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
#test grad of argmax #test grad of argmax
...@@ -1759,7 +1768,7 @@ class T_min_max(unittest.TestCase): ...@@ -1759,7 +1768,7 @@ class T_min_max(unittest.TestCase):
assert len(v) == 0 assert len(v) == 0
def test2(self): def test2(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for fct, nfct in [(max, numpy.max), (min, numpy.min)]: for fct, nfct in [(max, numpy.max), (min, numpy.min)]:
for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None), for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (None, None),
...@@ -1771,7 +1780,7 @@ class T_min_max(unittest.TestCase): ...@@ -1771,7 +1780,7 @@ class T_min_max(unittest.TestCase):
def test2_invalid(self): def test2_invalid(self):
for fct in [max, min]: for fct in [max, min]:
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
# Silence expected error messages # Silence expected error messages
_logger = logging.getLogger('theano.gof.opt') _logger = logging.getLogger('theano.gof.opt')
oldlevel = _logger.level oldlevel = _logger.level
...@@ -1787,7 +1796,7 @@ class T_min_max(unittest.TestCase): ...@@ -1787,7 +1796,7 @@ class T_min_max(unittest.TestCase):
def test2_invalid_neg(self): def test2_invalid_neg(self):
for fct in [max, min]: for fct in [max, min]:
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
old_stderr = sys.stderr old_stderr = sys.stderr
sys.stderr = StringIO.StringIO() sys.stderr = StringIO.StringIO()
try: try:
...@@ -1801,7 +1810,7 @@ class T_min_max(unittest.TestCase): ...@@ -1801,7 +1810,7 @@ class T_min_max(unittest.TestCase):
def test2_valid_neg(self): def test2_valid_neg(self):
for fct, nfct in [(max, numpy.max), (min, numpy.min)]: for fct, nfct in [(max, numpy.max), (min, numpy.min)]:
n = as_tensor_variable(numpy.random.rand(2, 3)) n = as_tensor_variable(rand(2, 3))
v = eval_outputs(fct(n, -1)) v = eval_outputs(fct(n, -1))
self.assertTrue(v.shape == (2,)) self.assertTrue(v.shape == (2,))
self.assertTrue(numpy.all(v == nfct(n.value, -1))) self.assertTrue(numpy.all(v == nfct(n.value, -1)))
...@@ -1816,7 +1825,7 @@ class T_min_max(unittest.TestCase): ...@@ -1816,7 +1825,7 @@ class T_min_max(unittest.TestCase):
def test3(self): def test3(self):
# Test with 1 axis or all axis out of 3 dims # Test with 1 axis or all axis out of 3 dims
data = numpy.random.rand(2, 3, 4) data = rand(2, 3, 4)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for fct, nfct in [(max, numpy.max), (min, numpy.min)]: for fct, nfct in [(max, numpy.max), (min, numpy.min)]:
for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (2, 2), for (axis, np_axis) in [(-1, -1), (0, 0), (1, 1), (2, 2),
...@@ -1829,7 +1838,7 @@ class T_min_max(unittest.TestCase): ...@@ -1829,7 +1838,7 @@ class T_min_max(unittest.TestCase):
def test3b(self): def test3b(self):
# Test with 2 axis out of 3 dims # Test with 2 axis out of 3 dims
data = numpy.random.rand(2, 3, 4) data = rand(2, 3, 4)
n = as_tensor_variable(data) n = as_tensor_variable(data)
for fct, nfct in [(max, numpy.max), (min, numpy.min)]: for fct, nfct in [(max, numpy.max), (min, numpy.min)]:
for axis in [[0, 1], [1, 2], [0, 2]]: for axis in [[0, 1], [1, 2], [0, 2]]:
...@@ -1840,7 +1849,7 @@ class T_min_max(unittest.TestCase): ...@@ -1840,7 +1849,7 @@ class T_min_max(unittest.TestCase):
assert tuple(v_shape) == np_v.shape assert tuple(v_shape) == np_v.shape
def test_grad_max(self): def test_grad_max(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
def check_grad_max(data, max_grad_data, axis=None): def check_grad_max(data, max_grad_data, axis=None):
...@@ -1874,7 +1883,7 @@ class T_min_max(unittest.TestCase): ...@@ -1874,7 +1883,7 @@ class T_min_max(unittest.TestCase):
check_grad_max(data, eval_outputs(grad(max(n.flatten()), n))) check_grad_max(data, eval_outputs(grad(max(n.flatten()), n)))
def test_grad_min(self): def test_grad_min(self):
data = numpy.random.rand(2, 3) data = rand(2, 3)
n = as_tensor_variable(data) n = as_tensor_variable(data)
def check_grad_min(data, min_grad_data, axis=None): def check_grad_min(data, min_grad_data, axis=None):
...@@ -1914,7 +1923,7 @@ class T_min_max(unittest.TestCase): ...@@ -1914,7 +1923,7 @@ class T_min_max(unittest.TestCase):
This not implemented, so we disable the test. See ticket: This not implemented, so we disable the test. See ticket:
http://trac-hg.assembla.com/theano/ticket/511 http://trac-hg.assembla.com/theano/ticket/511
""" """
data = numpy.random.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]:
utt.verify_grad(lambda v: fct(v, axis=[0, 1]), [data]) utt.verify_grad(lambda v: fct(v, axis=[0, 1]), [data])
...@@ -2191,7 +2200,7 @@ class T_subtensor(unittest.TestCase): ...@@ -2191,7 +2200,7 @@ class T_subtensor(unittest.TestCase):
def test_grad_1d(self): def test_grad_1d(self):
subi = 0 subi = 0
data = numpy.asarray(numpy.random.rand(2,3), dtype=self.dtype) data = numpy.asarray(rand(2,3), dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
z = scal.constant(subi) z = scal.constant(subi)
t = n[z:,z] t = n[z:,z]
...@@ -2211,7 +2220,7 @@ class T_subtensor(unittest.TestCase): ...@@ -2211,7 +2220,7 @@ class T_subtensor(unittest.TestCase):
self.assertTrue(numpy.allclose(gval, good), (gval, good)) self.assertTrue(numpy.allclose(gval, good), (gval, good))
def test_grad_0d(self): def test_grad_0d(self):
data = numpy.asarray(numpy.random.rand(2,3), dtype=self.dtype) data = numpy.asarray(rand(2,3), dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
t = n[1,0] t = n[1,0]
gn = grad(sum(exp(t)), n) gn = grad(sum(exp(t)), n)
...@@ -2229,16 +2238,16 @@ class T_subtensor(unittest.TestCase): ...@@ -2229,16 +2238,16 @@ class T_subtensor(unittest.TestCase):
self.assertTrue(numpy.allclose(gval, good), (gval, good)) self.assertTrue(numpy.allclose(gval, good), (gval, good))
def test_ok_list(self): def test_ok_list(self):
for data, idx in [(numpy.random.rand(4), [1,0]), for data, idx in [(rand(4), [1,0]),
(numpy.random.rand(4,5), [2,3]), (rand(4,5), [2,3]),
(numpy.random.rand(4,2,3), [0,3]), (rand(4,2,3), [0,3]),
(numpy.random.rand(4,2,3), [3,3,1,1,2,2,0,0]), (rand(4,2,3), [3,3,1,1,2,2,0,0]),
(numpy.random.rand(4,2,3), [3,3,1,1,2,2,0,0,-1,-2,-3,-4]), (rand(4,2,3), [3,3,1,1,2,2,0,0,-1,-2,-3,-4]),
# Test 4 dims as gpu code use another algo in that case # Test 4 dims as gpu code use another algo in that case
# This new algo is not as much optimized for that case. # This new algo is not as much optimized for that case.
(numpy.random.rand(4,4,2,3), [3,3,1,1,2,2,0,0,-1,-2,-3,-4]), (rand(4,4,2,3), [3,3,1,1,2,2,0,0,-1,-2,-3,-4]),
# Test with TensorConstant index. # Test with TensorConstant index.
(numpy.random.rand(4,2,3), constant([3,3,1,1,2,2,0,0])), (rand(4,2,3), constant([3,3,1,1,2,2,0,0])),
]: ]:
data = numpy.asarray(data, dtype=self.dtype) data = numpy.asarray(data, dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
...@@ -2566,7 +2575,7 @@ class T_subtensor(unittest.TestCase): ...@@ -2566,7 +2575,7 @@ class T_subtensor(unittest.TestCase):
pass pass
def test_grad_list(self): def test_grad_list(self):
data = numpy.random.rand(4) data = rand(4)
data = numpy.asarray(data, dtype=self.dtype) data = numpy.asarray(data, dtype=self.dtype)
idxs = [[i] for i in range(data.shape[0])] idxs = [[i] for i in range(data.shape[0])]
for i in range(data.shape[0]): for i in range(data.shape[0]):
...@@ -2574,20 +2583,20 @@ class T_subtensor(unittest.TestCase): ...@@ -2574,20 +2583,20 @@ class T_subtensor(unittest.TestCase):
idxs.append([i,j,(i+1)%data.shape[0]]) idxs.append([i,j,(i+1)%data.shape[0]])
self.grad_list_(idxs, data) self.grad_list_(idxs, data)
data = numpy.random.rand(4,3) data = rand(4,3)
data = numpy.asarray(data, dtype=self.dtype) data = numpy.asarray(data, dtype=self.dtype)
self.grad_list_(idxs, data) self.grad_list_(idxs, data)
data = numpy.random.rand(4,3,2) data = rand(4,3,2)
data = numpy.asarray(data, dtype=self.dtype) data = numpy.asarray(data, dtype=self.dtype)
self.grad_list_(idxs, data) self.grad_list_(idxs, data)
def test_shape_list(self): def test_shape_list(self):
#TODO for all type of subtensor shape #TODO for all type of subtensor shape
for data, idx in [(numpy.random.rand(4), [1,0]), for data, idx in [(rand(4), [1,0]),
(numpy.random.rand(4,2), [2,3]), (rand(4,2), [2,3]),
(numpy.random.rand(4,2,3), [0,3]), (rand(4,2,3), [0,3]),
(numpy.random.rand(4,2,3), [3,3,1,2,2,]), (rand(4,2,3), [3,3,1,2,2,]),
]: ]:
data = numpy.asarray(data, dtype=self.dtype) data = numpy.asarray(data, dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
...@@ -3264,13 +3273,13 @@ class T_add(unittest.TestCase): ...@@ -3264,13 +3273,13 @@ class T_add(unittest.TestCase):
self.assertTrue(a.type.values_eq_approx(fn(a.data, b.data), f(a.data, b.data))) self.assertTrue(a.type.values_eq_approx(fn(a.data, b.data), f(a.data, b.data)))
def test_grad_scalar_l(self): def test_grad_scalar_l(self):
utt.verify_grad(add, [numpy.asarray([3.0]), numpy.random.rand(3)]) utt.verify_grad(add, [numpy.asarray([3.0]), rand(3)])
def test_grad_scalar_r(self): def test_grad_scalar_r(self):
utt.verify_grad(add, [numpy.random.rand(3), numpy.asarray([3.0])]) utt.verify_grad(add, [rand(3), numpy.asarray([3.0])])
def test_grad_row(self): def test_grad_row(self):
utt.verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) utt.verify_grad(add, [rand(3, 5), rand(1, 5)])
def test_grad_col(self): def test_grad_col(self):
utt.verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) utt.verify_grad(add, [rand(3, 5), rand(3, 1)])
class T_ceil(unittest.TestCase): class T_ceil(unittest.TestCase):
def test_complex(self): def test_complex(self):
...@@ -3336,7 +3345,7 @@ class T_mean(unittest.TestCase): ...@@ -3336,7 +3345,7 @@ class T_mean(unittest.TestCase):
#Simple test... #Simple test...
x = tensor.vector() x = tensor.vector()
f = theano.function([x],tensor.mean(x)) f = theano.function([x],tensor.mean(x))
data = numpy.asarray(numpy.random.rand(50), dtype=config.floatX) data = numpy.asarray(rand(50), dtype=config.floatX)
assert numpy.allclose(f(data), numpy.mean(data)) assert numpy.allclose(f(data), numpy.mean(data))
...@@ -3368,8 +3377,8 @@ class test_matinv(unittest.TestCase): ...@@ -3368,8 +3377,8 @@ class test_matinv(unittest.TestCase):
fn = inplace_func([a,b], [ssdiff,g_b]) fn = inplace_func([a,b], [ssdiff,g_b])
# use the function # use the function
x = numpy.random.rand(dim,dim)+0.1 # Initialized s.t. x is not too tiny x = rand(dim,dim)+0.1 # Initialized s.t. x is not too tiny
w = numpy.random.rand(dim,dim) w = rand(dim,dim)
x = numpy.asarray(x, dtype=config.floatX) x = numpy.asarray(x, dtype=config.floatX)
w = numpy.asarray(w, dtype=config.floatX) w = numpy.asarray(w, dtype=config.floatX)
...@@ -3388,8 +3397,8 @@ class test_matinv(unittest.TestCase): ...@@ -3388,8 +3397,8 @@ class test_matinv(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
# hand-coded numpy implementation for verification # hand-coded numpy implementation for verification
x = numpy.random.rand(3,3)+0.1 x = rand(3,3)+0.1
w = numpy.random.rand(3,3) w = rand(3,3)
x = numpy.asarray(x, dtype=config.floatX) x = numpy.asarray(x, dtype=config.floatX)
w = numpy.asarray(w, dtype=config.floatX) w = numpy.asarray(w, dtype=config.floatX)
ones = numpy.ones((3,3), dtype=config.floatX) ones = numpy.ones((3,3), dtype=config.floatX)
...@@ -3408,7 +3417,7 @@ class t_dot(unittest.TestCase): ...@@ -3408,7 +3417,7 @@ class t_dot(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
@staticmethod @staticmethod
def rand(*args): def rand(*args):
return numpy.random.rand(*args) return rand(*args)
def cmp_dot(self,x,y): def cmp_dot(self,x,y):
#x, y are matrices or numbers #x, y are matrices or numbers
...@@ -3712,7 +3721,7 @@ class T_op_cache(unittest.TestCase): ...@@ -3712,7 +3721,7 @@ class T_op_cache(unittest.TestCase):
fn_py = inplace_func([v], gv) fn_py = inplace_func([v], gv)
fn_c_or_py = inplace_func([v], gv) fn_c_or_py = inplace_func([v], gv)
a = numpy.random.rand(5,2).astype(config.floatX) a = rand(5,2).astype(config.floatX)
self.assertTrue(numpy.all(fn_py(a) == fn_c_or_py(a))) self.assertTrue(numpy.all(fn_py(a) == fn_c_or_py(a)))
class T_reshape(unittest.TestCase): class T_reshape(unittest.TestCase):
...@@ -3801,7 +3810,7 @@ class T_reshape(unittest.TestCase): ...@@ -3801,7 +3810,7 @@ class T_reshape(unittest.TestCase):
f = function([a, shapes], z.shape) f = function([a, shapes], z.shape)
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = numpy.random.RandomState(seed=utt.fetch_seed())
a_val = rng.uniform(size=(3,4)).astype(config.floatX) a_val = rng.uniform(size=(3, 4)).astype(config.floatX)
self.assertTrue((f(a_val, [4, 3]) == [4, 3]).all()) self.assertTrue((f(a_val, [4, 3]) == [4, 3]).all())
self.assertTrue((f(a_val, [-1, 3]) == [4, 3]).all()) self.assertTrue((f(a_val, [-1, 3]) == [4, 3]).all())
...@@ -4281,13 +4290,13 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -4281,13 +4290,13 @@ class TestPermuteRowElements(unittest.TestCase):
def test_2_1(self): def test_2_1(self):
"""Test broadcasting in PermuteRowElements(matrix, vector)""" """Test broadcasting in PermuteRowElements(matrix, vector)"""
input = dmatrix() input = matrix()
p = ivector() p = ivector()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
permute = function([input, p], out) permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(3,5)) input_val = rng.uniform(size=(3, 5)).astype(config.floatX)
p_val = rng.permutation(5).astype('int32') p_val = rng.permutation(5).astype('int32')
out_val = permute(input_val, p_val) out_val = permute(input_val, p_val)
...@@ -4303,13 +4312,13 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -4303,13 +4312,13 @@ class TestPermuteRowElements(unittest.TestCase):
def test_2_2(self): def test_2_2(self):
"""Test PermuteRowElements(matrix, matrix)""" """Test PermuteRowElements(matrix, matrix)"""
input = dmatrix() input = matrix()
p = imatrix() p = imatrix()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
permute = function([input, p], out) permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(3,5)) input_val = rng.uniform(size=(3, 5)).astype(config.floatX)
p_val = numpy.asarray([rng.permutation(5) for i in range(3)], p_val = numpy.asarray([rng.permutation(5) for i in range(3)],
dtype='int32') dtype='int32')
out_val = permute(input_val, p_val) out_val = permute(input_val, p_val)
...@@ -4328,13 +4337,13 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -4328,13 +4337,13 @@ class TestPermuteRowElements(unittest.TestCase):
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 = dvector() input = vector()
p = imatrix() p = imatrix()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
permute = function([input, p], out) permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(5,)) input_val = rng.uniform(size=(5,)).astype(config.floatX)
p_val = numpy.asarray([rng.permutation(5) for i in range(3)], dtype='int32') p_val = numpy.asarray([rng.permutation(5) for i in range(3)], dtype='int32')
out_val = permute(input_val, p_val) out_val = permute(input_val, p_val)
...@@ -4353,13 +4362,13 @@ class TestPermuteRowElements(unittest.TestCase): ...@@ -4353,13 +4362,13 @@ class TestPermuteRowElements(unittest.TestCase):
input.type.broadcastable = (False, True, False), input.type.broadcastable = (False, True, False),
p.type.broadcastable = (False, False).""" p.type.broadcastable = (False, False)."""
input = TensorType('float64', (False, True, False))() input = TensorType('floatX', (False, True, False))()
p = imatrix() p = imatrix()
out = permute_row_elements(input, p) out = permute_row_elements(input, p)
permute = function([input, p], out) permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(4,1,5)) input_val = rng.uniform(size=(4, 1, 5)).astype(config.floatX)
p_val = numpy.asarray([rng.permutation(5) for i in range(3)], p_val = numpy.asarray([rng.permutation(5) for i in range(3)],
dtype='int32') dtype='int32')
out_val = permute(input_val, p_val) out_val = permute(input_val, p_val)
...@@ -4381,7 +4390,7 @@ class test_tensordot(unittest.TestCase): ...@@ -4381,7 +4390,7 @@ class test_tensordot(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def rand(self, *shape): def rand(self, *shape):
return numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX) return numpy.asarray(rand(*shape), dtype=config.floatX)
def test0(self): def test0(self):
...@@ -4509,7 +4518,7 @@ class test_tensordot(unittest.TestCase): ...@@ -4509,7 +4518,7 @@ class test_tensordot(unittest.TestCase):
bmat = dmatrix()# We let at float64 to test mix of float32 and float64. bmat = dmatrix()# We let at float64 to test mix of float32 and float64.
axes = 1 axes = 1
aval = self.rand(4,5).astype('float32') aval = self.rand(4,5).astype('float32')
bval = numpy.random.rand(5,3) bval = rand(5,3)
c = tensordot(amat, bmat, axes) c = tensordot(amat, bmat, axes)
f3 = inplace_func([amat,bmat],c) f3 = inplace_func([amat,bmat],c)
self.assertTrue(numpy.allclose(numpy.tensordot(aval,bval,axes), self.assertTrue(numpy.allclose(numpy.tensordot(aval,bval,axes),
...@@ -5087,8 +5096,8 @@ def test_unalign(): ...@@ -5087,8 +5096,8 @@ def test_unalign():
b = numpy.empty(1e4, dtype=dtype)['f1'] b = numpy.empty(1e4, dtype=dtype)['f1']
assert not a.flags.aligned assert not a.flags.aligned
assert not b.flags.aligned assert not b.flags.aligned
a[:] = numpy.random.rand(len(a)) a[:] = rand(len(a))
b[:] = numpy.random.rand(len(b)) b[:] = rand(len(b))
out_numpy = 2*a + 3*b out_numpy = 2*a + 3*b
av,bv = tensor.vectors('ab') av,bv = tensor.vectors('ab')
...@@ -5149,10 +5158,10 @@ class T_get_constant_value(unittest.TestCase): ...@@ -5149,10 +5158,10 @@ class T_get_constant_value(unittest.TestCase):
assert get_constant_value(v.shape[0])==1 assert get_constant_value(v.shape[0])==1
def test_subtensor_of_constant(self): def test_subtensor_of_constant(self):
c = constant(numpy.random.rand(5)) c = constant(rand(5))
for i in range(c.value.shape[0]): for i in range(c.value.shape[0]):
assert get_constant_value(c[i]) == c.value[i] assert get_constant_value(c[i]) == c.value[i]
c = constant(numpy.random.rand(5,5)) c = constant(rand(5,5))
for i in range(c.value.shape[0]): for i in range(c.value.shape[0]):
for j in range(c.value.shape[1]): for j in range(c.value.shape[1]):
assert get_constant_value(c[i,j]) == c.value[i,j] assert get_constant_value(c[i,j]) == c.value[i,j]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论