提交 42750428 authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #5245 from lamblin/fix_subtensor_setsubtensor

Add tests for local_subtensor_inc_subtensor
...@@ -1882,76 +1882,122 @@ def test_local_subtensor_remove_broadcastable_index(): ...@@ -1882,76 +1882,122 @@ def test_local_subtensor_remove_broadcastable_index():
f2(xn) f2(xn)
def test_subtensor_inc_subtensor(): class Test_subtensor_inc_subtensor(unittest.TestCase):
# basic test @classmethod
x = tensor.matrix('x') def setUpClass(cls):
i = tensor.iscalar('i') cls.mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor')
v = tensor.vector('v')
y = tensor.set_subtensor(x[i], v) def test_basic(self):
z = y[i] # basic test
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') x = tensor.matrix('x')
f = theano.function([x, i, v], z, mode=mode) i = tensor.iscalar('i')
prog = f.maker.fgraph.toposort() v = tensor.vector('v')
assert len(prog) == 1 y = tensor.set_subtensor(x[i], v)
assert isinstance(prog[0].op, DeepCopyOp) z = y[i]
# basic test, numerical check f = theano.function([x, i, v], z, mode=self.mode)
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX) prog = f.maker.fgraph.toposort()
v_ = numpy.random.uniform(size=[4, ]).astype(config.floatX) assert len(prog) == 1
i_ = 1 assert isinstance(prog[0].op, DeepCopyOp)
assert numpy.array_equal(f(x_, i_, v_), v_) # basic test, numerical check
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX)
# complicated test v_ = numpy.random.uniform(size=[4, ]).astype(config.floatX)
x = tensor.tensor4('x') i_ = 1
i1 = tensor.iscalar('i1') assert numpy.array_equal(f(x_, i_, v_), v_)
i2 = tensor.iscalar('i2')
i3 = tensor.iscalar('i3') def test_multiple_idx(self):
i4 = tensor.iscalar('i4') # complicated test
v = tensor.tensor3('v') x = tensor.tensor4('x')
y = tensor.set_subtensor(x[i1, :i2, i3:, ::i4], v) i1 = tensor.iscalar('i1')
z = y[i1, :i2, i3:, ::i4] i2 = tensor.iscalar('i2')
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') i3 = tensor.iscalar('i3')
f = theano.function([x, i1, i2, i3, i4, v], z, mode=mode) i4 = tensor.iscalar('i4')
prog = f.maker.fgraph.toposort() v = tensor.tensor3('v')
assert len(prog) == 1 y = tensor.set_subtensor(x[i1, :i2, i3:, ::i4], v)
assert isinstance(prog[0].op, DeepCopyOp) z = y[i1, :i2, i3:, ::i4]
# complicated test, numerical check f = theano.function([x, i1, i2, i3, i4, v], z, mode=self.mode)
x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX) prog = f.maker.fgraph.toposort()
v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX) assert len(prog) == 1
i1_, i2_, i3_, i4_ = 1, 2, 3, 4 assert isinstance(prog[0].op, DeepCopyOp)
assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), v_) # complicated test, numerical check
x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX)
# case not use this optimization v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX)
z = y[i1, :i3, i2:, ::i4] i1_, i2_, i3_, i4_ = 1, 2, 3, 4
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), v_)
f = theano.function([x, i1, i2, i3, i4, v], z, mode=mode)
prog = f.maker.fgraph.toposort() def test_not_applied(self):
assert len(prog) != 1 # case not use this optimization
assert any(isinstance(x.op, tensor.IncSubtensor) for x in prog) x = tensor.tensor4('x')
assert any(isinstance(x.op, tensor.Subtensor) for x in prog) i1 = tensor.iscalar('i1')
# case not use this optimization, numerical check i2 = tensor.iscalar('i2')
x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX) i3 = tensor.iscalar('i3')
v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX) i4 = tensor.iscalar('i4')
i1_, i2_, i3_, i4_ = 1, 2, 3, 4 v = tensor.tensor3('v')
x_[i1_, :i2_, i3_:, ::i4_] = v_ y = tensor.set_subtensor(x[i1, :i2, i3:, ::i4], v)
assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), x_[i1_, :i3_, i2_:, ::i4_]) z = y[i1, :i3, i2:, ::i4]
f = theano.function([x, i1, i2, i3, i4, v], z, mode=self.mode)
# case when v is broadcastable prog = f.maker.fgraph.toposort()
x = tensor.matrix('x') assert len(prog) != 1
i1 = tensor.iscalar('i') assert any(isinstance(x.op, tensor.IncSubtensor) for x in prog)
i2 = tensor.iscalar('i') assert any(isinstance(x.op, tensor.Subtensor) for x in prog)
v = tensor.vector('v') # case not use this optimization, numerical check
y = tensor.set_subtensor(x[:i1, :i2], v) x_ = numpy.random.uniform(size=[3, 4, 5, 6]).astype(config.floatX)
z = y[:i1, :i2] v_ = numpy.random.uniform(size=[2, 2, 2]).astype(config.floatX)
mode = theano.compile.mode.get_default_mode().including('local_subtensor_inc_subtensor') i1_, i2_, i3_, i4_ = 1, 2, 3, 4
f = theano.function([x, i1, i2, v], z, mode=mode) x_[i1_, :i2_, i3_:, ::i4_] = v_
prog = f.maker.fgraph.toposort() assert numpy.array_equal(f(x_, i1_, i2_, i3_, i4_, v_), x_[i1_, :i3_, i2_:, ::i4_])
assert any(isinstance(x.op, tensor.Alloc) for x in prog)
# case when v is broadcastable, numerical check def test_fewer_dims(self):
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX) # case when v has fewer dimensions
v_ = numpy.random.uniform(size=[2, ]).astype(config.floatX) x = tensor.matrix('x')
i1_, i2_ = 2, 2 i1 = tensor.iscalar('i')
x_[:i1_, :i2_] = v_ i2 = tensor.iscalar('i')
assert numpy.array_equal(f(x_, i1_, i2_, v_), x_[:i1_, :i2_]) v = tensor.vector('v')
y = tensor.set_subtensor(x[:i1, :i2], v)
z = y[:i1, :i2]
f = theano.function([x, i1, i2, v], z, mode=self.mode)
prog = f.maker.fgraph.toposort()
assert any(isinstance(x.op, tensor.Alloc) for x in prog)
# case when v is broadcastable, numerical check
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX)
v_ = numpy.random.uniform(size=[2, ]).astype(config.floatX)
i1_, i2_ = 2, 2
x_[:i1_, :i2_] = v_
assert numpy.array_equal(f(x_, i1_, i2_, v_), x_[:i1_, :i2_])
def test_broadcasted(self):
# case when v has the same number of dimensions, some broadcastable
x = tensor.matrix('x')
i1 = tensor.iscalar('i')
i2 = tensor.iscalar('i')
v = tensor.col('v')
y = tensor.set_subtensor(x[:i1, :i2], v)
z = y[:i1, :i2]
f = theano.function([x, i1, i2, v], z, mode=self.mode)
prog = f.maker.fgraph.toposort()
assert any(isinstance(x.op, tensor.Alloc) for x in prog)
# case when v is broadcastable, numerical check
x_ = numpy.random.uniform(size=[3, 4]).astype(config.floatX)
v_ = numpy.random.uniform(size=[2, 1]).astype(config.floatX)
i1_, i2_ = 2, 2
x_[:i1_, :i2_] = v_
assert numpy.array_equal(f(x_, i1_, i2_, v_), x_[:i1_, :i2_])
def test_different_dtypes(self):
# Case when the dtype differs
x = tensor.bmatrix('x')
i = tensor.iscalar('i')
v = tensor.vector('v')
y = tensor.set_subtensor(x[i], v)
z = y[i]
f = theano.function([x, i, v], z, mode=self.mode)
prog = f.maker.fgraph.toposort()
assert len(prog) == 1
assert prog[0].op == tensor.basic._convert_to_int8
# basic test, numerical check
x_ = numpy.random.randint(12, size=[3, 4]).astype('int8')
v_ = numpy.random.uniform(12, size=[4, ]).astype(config.floatX)
i_ = 1
assert numpy.array_equal(f(x_, i_, v_), v_.astype('int8'))
class test_local_subtensor_make_vector(unittest.TestCase): class test_local_subtensor_make_vector(unittest.TestCase):
...@@ -6763,15 +6809,3 @@ def test_local_log_sum_exp3(): ...@@ -6763,15 +6809,3 @@ def test_local_log_sum_exp3():
optimised_ret = f(x_val) optimised_ret = f(x_val)
assert numpy.allclose(optimised_ret, 100.) assert numpy.allclose(optimised_ret, 100.)
if __name__ == '__main__':
t = TestMakeVector('setUp')
t.setUp()
# t.test_perform()
t.test_infer_shape()
test_subtensor_inc_subtensor()
"""
# unittest.main()
test_fusion().tes_memory_leak()
"""
...@@ -15,22 +15,20 @@ import theano ...@@ -15,22 +15,20 @@ import theano
import theano.scalar as scal import theano.scalar as scal
import theano.tensor as tensor import theano.tensor as tensor
from theano import config, gof from theano import config, gof
from theano.compat import PY3, exc_message, izip from theano.compat import PY3, izip
from theano.compile import DeepCopyOp from theano.compile import DeepCopyOp
from theano.tensor import (MakeSlice, NotScalarConstantError, _shared, from theano.tensor import (_shared, cscalar, ctensor3, dmatrix,
as_tensor_variable, cscalar, ctensor3, dmatrix,
dscalar, dtensor4, dvector, fmatrix, fscalar, dscalar, dtensor4, dvector, fmatrix, fscalar,
fvector, ftensor4, iscalar, lmatrix, lrow, lvector, fvector, ftensor4, iscalar, lmatrix, lrow, lvector,
matrix, vector) matrix, vector)
from theano.tensor.basic import DimShuffle from theano.tensor.basic import DimShuffle
from theano.tensor.subtensor import (AdvancedIncSubtensor, from theano.tensor.subtensor import (AdvancedIncSubtensor,
AdvancedIncSubtensor1, AdvancedSubtensor, AdvancedIncSubtensor1, AdvancedSubtensor,
AdvancedSubtensor1, IncSubtensor, IncSubtensor,
Subtensor, advanced_inc_subtensor, Subtensor, advanced_inc_subtensor,
advanced_inc_subtensor1, advanced_inc_subtensor1,
advanced_set_subtensor, advanced_set_subtensor,
advanced_set_subtensor1, advanced_set_subtensor1,
advanced_subtensor1,
get_canonical_form_slice, inc_subtensor, get_canonical_form_slice, inc_subtensor,
inplace_increment, set_subtensor) inplace_increment, set_subtensor)
from theano.tensor.tests.test_basic import inplace_func, rand, randint_ranged from theano.tensor.tests.test_basic import inplace_func, rand, randint_ranged
...@@ -42,7 +40,7 @@ if PY3: ...@@ -42,7 +40,7 @@ if PY3:
return i return i
else: else:
def L(i): def L(i):
return long(i) return long(i) # noqa for Python 3
class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
...@@ -122,7 +120,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -122,7 +120,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# it is impossible to retrieve a view of a 0-d tensor # it is impossible to retrieve a view of a 0-d tensor
n = self.shared(numpy.ones((), dtype=self.dtype)) n = self.shared(numpy.ones((), dtype=self.dtype))
try: try:
t = n[0] n[0]
except ValueError as e: except ValueError as e:
self.assertTrue(hasattr(e, 'subtensor_invalid')) self.assertTrue(hasattr(e, 'subtensor_invalid'))
return return
...@@ -144,7 +142,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -144,7 +142,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
try: try:
try: try:
self.eval_output_and_check(t) self.eval_output_and_check(t)
except IndexError as e: except IndexError:
return return
self.fail() self.fail()
finally: finally:
...@@ -153,8 +151,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -153,8 +151,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
def test1_err_subslice(self): def test1_err_subslice(self):
n = self.shared(numpy.ones(3, dtype=self.dtype)) n = self.shared(numpy.ones(3, dtype=self.dtype))
try: try:
t = n[slice(0, slice(1, 2, None), None)] n[slice(0, slice(1, 2, None), None)]
except Exception as e: except Exception:
# Relax constraint on the type of Exception, # Relax constraint on the type of Exception,
# since this might be handled by AvancedSubtensor # since this might be handled by AvancedSubtensor
# if e[0] != Subtensor.e_indextype: # if e[0] != Subtensor.e_indextype:
...@@ -190,7 +188,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -190,7 +188,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
def test1_err_invalid(self): def test1_err_invalid(self):
n = self.shared(numpy.ones(1, dtype=self.dtype)) n = self.shared(numpy.ones(1, dtype=self.dtype))
try: try:
t = n[0, 0] n[0, 0]
except ValueError as e: except ValueError as e:
self.assertTrue(hasattr(e, 'subtensor_invalid')) self.assertTrue(hasattr(e, 'subtensor_invalid'))
return return
...@@ -407,10 +405,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -407,10 +405,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
vs1, vn3, vn4 = theano.function([s], [s1, n3, n4])(-2.0) vs1, vn3, vn4 = theano.function([s], [s1, n3, n4])(-2.0)
assert numpy.all(vs1 == [-2.0]) assert numpy.all(vs1 == [-2.0])
assert numpy.all(vn3 assert numpy.all(vn3 ==
== numpy.arange(24)[newaxis, :, newaxis]) numpy.arange(24)[newaxis, :, newaxis])
assert numpy.all(vn4 assert numpy.all(vn4 ==
== numpy.arange(24).reshape((2, 3, 4))[:, :, :, newaxis]) numpy.arange(24).reshape((2, 3, 4))[:, :, :, newaxis])
def test_grad_1d(self): def test_grad_1d(self):
subi = 0 subi = 0
...@@ -463,14 +461,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -463,14 +461,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n) gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n)
f = self.function([], gn) f = self.function([], gn)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
topo_ = [node for node in topo if not isinstance(node.op, topo_ = [node for node in topo
self.ignore_topo)] if not isinstance(node.op, self.ignore_topo)]
if not self.fast_compile: if not self.fast_compile:
assert_equal(len(topo_), 6) assert_equal(len(topo_), 6)
assert numpy.sum([isinstance(node.op, self.inc_sub) assert numpy.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1 for node in topo_]) == 1
assert numpy.sum([isinstance(node.op, self.sub) assert numpy.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1 for node in topo_]) == 1
gval = f() gval = f()
good = numpy.zeros_like(data) good = numpy.zeros_like(data)
...@@ -487,8 +485,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -487,8 +485,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# Test 4 dims as gpu code use another algo # Test 4 dims as gpu code use another algo
# in that case This new algo is not as much # in that case This new algo is not as much
# optimized for that case. # optimized for that case.
(rand(4, 4, 2, 3), [3, (rand(4, 4, 2, 3),
3, 1, 1, 2, 2, 0, 0, -1, -2, -3, -4]), [3, 3, 1, 1, 2, 2, 0, 0, -1, -2, -3, -4]),
# Test with TensorConstant index. # Test with TensorConstant index.
(rand(4, 2, 3), (rand(4, 2, 3),
theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])), theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])),
...@@ -526,7 +524,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -526,7 +524,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
g = self.function([], gn, op=self.adv_incsub1) g = self.function([], gn, op=self.adv_incsub1)
utt.verify_grad(lambda m: m[[1, 3]], utt.verify_grad(lambda m: m[[1, 3]],
[numpy.random.rand(5, 5).astype(self.dtype)]) [numpy.random.rand(5, 5).astype(self.dtype)])
g_0 = g() g()
utt.verify_grad(lambda m: m[idx], utt.verify_grad(lambda m: m[idx],
[data]) [data])
...@@ -558,7 +556,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -558,7 +556,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
def test_adv_sub1_broadcast(self): def test_adv_sub1_broadcast(self):
v = numpy.arange(3, dtype=self.dtype).reshape((1, 3)) v = numpy.arange(3, dtype=self.dtype).reshape((1, 3))
n = self.shared(v*5, broadcastable=(True, False)) n = self.shared(v * 5, broadcastable=(True, False))
idx = tensor.lvector() idx = tensor.lvector()
t = n[idx] t = n[idx]
self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor1)) self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor1))
...@@ -571,10 +569,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -571,10 +569,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(isinstance(topo_[0].op, self.adv_sub1)) self.assertTrue(isinstance(topo_[0].op, self.adv_sub1))
f_0 = f([0]) f_0 = f([0])
self.assertTrue(f_0.shape == (1, 3)) self.assertTrue(f_0.shape == (1, 3))
self.assertTrue(numpy.allclose(f_0, v*5)) self.assertTrue(numpy.allclose(f_0, v * 5))
f_00 = f([0, 0]) f_00 = f([0, 0])
self.assertTrue(f_00.shape == (2, 3)) self.assertTrue(f_00.shape == (2, 3))
self.assertTrue(numpy.allclose(f_00, v*5)) self.assertTrue(numpy.allclose(f_00, v * 5))
self.assertRaises(IndexError, f, [0, 1]) self.assertRaises(IndexError, f, [0, 1])
# Test the gradient # Test the gradient
...@@ -603,7 +601,6 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -603,7 +601,6 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# test set_subtensor broadcast # test set_subtensor broadcast
self.dtype = 'float32' self.dtype = 'float32'
from theano.sandbox.cuda.dnn import dnn_conv
x = tensor.tensor4('x', dtype=self.dtype) x = tensor.tensor4('x', dtype=self.dtype)
indexes = theano.shared(numpy.int32([1, 2, 3, 4])) indexes = theano.shared(numpy.int32([1, 2, 3, 4]))
...@@ -667,8 +664,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -667,8 +664,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
t_shapes = f() t_shapes = f()
for t_shape, shape in zip(t_shapes, shapes): for t_shape, shape in zip(t_shapes, shapes):
assert numpy.all(t_shape == shape) assert numpy.all(t_shape == shape)
assert tensor.Subtensor not in [x.op for x in assert tensor.Subtensor not in [x.op
f.maker.fgraph.toposort()] for x in f.maker.fgraph.toposort()]
def test_shape_i_scalar(self): def test_shape_i_scalar(self):
# Each axis is treated independently by shape_i/shape operators # Each axis is treated independently by shape_i/shape operators
...@@ -685,8 +682,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -685,8 +682,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
mode=mode_opt, mode=mode_opt,
op=self.ops, op=self.ops,
N=0) N=0)
assert tensor.Subtensor not in [x.op for x in f.maker. assert tensor.Subtensor not in [x.op
fgraph.toposort()] for x in f.maker.fgraph.toposort()]
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-3, -1, 2, 5]: for step in [-3, -1, 2, 5]:
...@@ -708,7 +705,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -708,7 +705,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
length = 5 length = 5
a = numpy.arange(length) a = numpy.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]: for step in [-6, -3, -1, 2, 5]:
out = f(start, stop, step, length) out = f(start, stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
...@@ -729,7 +726,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -729,7 +726,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
length = 5 length = 5
a = numpy.arange(length) a = numpy.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]: for step in [-6, -3, -1, 2, 5]:
out = f(stop, step, length) out = f(stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
...@@ -772,7 +769,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -772,7 +769,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
length = 5 length = 5
a = numpy.arange(length) a = numpy.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(start, stop, length) out = f(start, stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:None] v_out = a[start:stop:None]
...@@ -829,7 +826,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -829,7 +826,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
length = 5 length = 5
a = numpy.arange(length) a = numpy.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(stop, length) out = f(stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[None:stop:None] v_out = a[None:stop:None]
...@@ -847,11 +844,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -847,11 +844,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
f = self.function([], [gn, gn.shape], op=self.adv_incsub1) f = self.function([], [gn, gn.shape], op=self.adv_incsub1)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
if not self.fast_compile: if not self.fast_compile:
assert any([isinstance(node.op, self. assert any([isinstance(node.op, self.adv_incsub1) and
adv_incsub1) and node.op.inplace for node in topo]) node.op.inplace for node in topo])
else: else:
assert any([isinstance(node.op, self. assert any([isinstance(node.op, self.adv_incsub1)
adv_incsub1) for node in topo]) for node in topo])
assert any([isinstance(node.op, self.adv_sub1) for node in topo]) assert any([isinstance(node.op, self.adv_sub1) for node in topo])
gval, gshape = f() gval, gshape = f()
good = numpy.zeros_like(data) good = numpy.zeros_like(data)
...@@ -970,7 +967,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -970,7 +967,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
""" """
X = self.shared(numpy.ones((9, 9)).astype(self.dtype)) X = self.shared(numpy.ones((9, 9)).astype(self.dtype))
y = set_subtensor(X[1::, 1::], 0) y = set_subtensor(X[1::, 1::], 0)
f = self.function([], [y], f = self.function([], [y],
op=self.inc_sub, op=self.inc_sub,
N=1) N=1)
...@@ -1257,7 +1254,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1257,7 +1254,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
def test_take(self): def test_take(self):
a = tensor.matrix() a = tensor.matrix()
f = theano.function([a], a.take(0, axis=-1), allow_input_downcast=True) f = theano.function([a], a.take(0, axis=-1), allow_input_downcast=True)
x = f(numpy.random.normal(0, 1, (30, 4))) f(numpy.random.normal(0, 1, (30, 4)))
class TestIncSubtensor1(unittest.TestCase): class TestIncSubtensor1(unittest.TestCase):
...@@ -1370,8 +1367,8 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1370,8 +1367,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def eval_output_and_check(self, t): def eval_output_and_check(self, t):
f = inplace_func([], t, mode=self.mode) f = inplace_func([], t, mode=self.mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
topo_ = [node for node in topo if not isinstance(node.op, topo_ = [node for node in topo
self.ignore_topo)] if not isinstance(node.op, self.ignore_topo)]
assert len(topo_) == 1 assert len(topo_) == 1
assert isinstance(topo_[0].op, self.sub) assert isinstance(topo_[0].op, self.sub)
tval = f() tval = f()
...@@ -1388,7 +1385,7 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1388,7 +1385,7 @@ class TestAdvancedSubtensor(unittest.TestCase):
a = self.v[self.ix2] a = self.v[self.ix2]
assert a.dtype == self.v.dtype, (a.dtype, self.v.dtype) assert a.dtype == self.v.dtype, (a.dtype, self.v.dtype)
assert a.broadcastable == self.ix2.broadcastable, ( assert a.broadcastable == self.ix2.broadcastable, (
a.broadcastable, self.ix2.broadcastable) a.broadcastable, self.ix2.broadcastable)
def test_index_into_mat_w_row(self): def test_index_into_mat_w_row(self):
a = self.m[self.ixr] a = self.m[self.ixr]
...@@ -1406,8 +1403,8 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1406,8 +1403,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
# Test 4 dims as gpu code use another algo # Test 4 dims as gpu code use another algo
# in that case This new algo is not as much # in that case This new algo is not as much
# optimized for that case. # optimized for that case.
(rand(4, 4, 2, 3), [3, (rand(4, 4, 2, 3),
3, 1, 1, 2, 2, 0, 0, -1, -2, -3, -4]), [3, 3, 1, 1, 2, 2, 0, 0, -1, -2, -3, -4]),
# Test with TensorConstant index. # Test with TensorConstant index.
(rand(2, 4, 3), (rand(2, 4, 3),
theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])), theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])),
...@@ -1499,9 +1496,9 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1499,9 +1496,9 @@ class TestAdvancedSubtensor(unittest.TestCase):
[1, 2, 1], [1, 2, 1],
[0, 1, 0]) [0, 1, 0])
assert numpy.allclose(aval, assert numpy.allclose(aval,
[[.4, .9, .1], [[.4, .9, .1],
[5 * 3, 6, 7], [5 * 3, 6, 7],
[.5, .3 * 2, .15]]), aval [.5, .3 * 2, .15]]), aval
def test_inc_adv_subtensor_with_broadcasting(self): def test_inc_adv_subtensor_with_broadcasting(self):
if inplace_increment is None: if inplace_increment is None:
...@@ -1521,9 +1518,9 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1521,9 +1518,9 @@ class TestAdvancedSubtensor(unittest.TestCase):
[0, 1, 0], [0, 1, 0],
2.1) 2.1)
assert numpy.allclose(aval, assert numpy.allclose(aval,
[[.4, .9, .1], [[.4, .9, .1],
[5 + 2.1 * 2, 6, 7], [5 + 2.1 * 2, 6, 7],
[.5, .3 + 2.1, .15]]), aval [.5, .3 + 2.1, .15]]), aval
assert numpy.allclose(gval, 3.0), gval assert numpy.allclose(gval, 3.0), gval
def test_inc_adv_subtensor1_with_broadcasting(self): def test_inc_adv_subtensor1_with_broadcasting(self):
...@@ -1543,9 +1540,9 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1543,9 +1540,9 @@ class TestAdvancedSubtensor(unittest.TestCase):
[0, 1, 0], [0, 1, 0],
2.1) 2.1)
assert numpy.allclose(aval, assert numpy.allclose(aval,
[[.4 + 2.1 * 2, .9 + 2.1 * 2, .1 + 2.1 * 2], [[.4 + 2.1 * 2, .9 + 2.1 * 2, .1 + 2.1 * 2],
[5 + 2.1, 6 + 2.1, 7 + 2.1], [5 + 2.1, 6 + 2.1, 7 + 2.1],
[.5, .3, .15]]), aval [.5, .3, .15]]), aval
assert numpy.allclose(gval, 9.0), gval assert numpy.allclose(gval, 9.0), gval
def test_inc_adv_subtensor_with_index_broadcasting(self): def test_inc_adv_subtensor_with_index_broadcasting(self):
...@@ -1564,9 +1561,9 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1564,9 +1561,9 @@ class TestAdvancedSubtensor(unittest.TestCase):
[[0, 1, 0], [[0, 1, 0],
[2, 2, 2]]) [2, 2, 2]])
assert numpy.allclose(aval, assert numpy.allclose(aval,
[[.4 + 2 * 2.1, .9, .1 + 2 * 2.1], [[.4 + 2 * 2.1, .9, .1 + 2 * 2.1],
[5, 6, 7], [5, 6, 7],
[.5, .3 + 2.1, .15 + 2.1]]), aval [.5, .3 + 2.1, .15 + 2.1]]), aval
def test_advanced_indexing(self): def test_advanced_indexing(self):
# tests advanced indexing in Theano for 2D and 3D tensors # tests advanced indexing in Theano for 2D and 3D tensors
...@@ -1620,72 +1617,72 @@ class TestInferShape(utt.InferShapeTester): ...@@ -1620,72 +1617,72 @@ class TestInferShape(utt.InferShapeTester):
adscal = dscalar() adscal = dscalar()
admat_val = rand(5, 4) admat_val = rand(5, 4)
self._compile_and_check([admat, bdmat], self._compile_and_check([admat, bdmat],
[inc_subtensor(admat[2:4], bdmat)], [inc_subtensor(admat[2:4], bdmat)],
[admat_val, [[1, 2, 3, 4]]], IncSubtensor) [admat_val, [[1, 2, 3, 4]]], IncSubtensor)
self._compile_and_check([admat, advec], self._compile_and_check([admat, advec],
[inc_subtensor(admat[2], advec)], [inc_subtensor(admat[2], advec)],
[admat_val, [1, 2, 3, 4]], IncSubtensor) [admat_val, [1, 2, 3, 4]], IncSubtensor)
self._compile_and_check([admat, adscal], self._compile_and_check([admat, adscal],
[inc_subtensor(admat[2, 3], adscal)], [inc_subtensor(admat[2, 3], adscal)],
[admat_val, 1], IncSubtensor) [admat_val, 1], IncSubtensor)
self._compile_and_check([admat, adscal], self._compile_and_check([admat, adscal],
[inc_subtensor(admat[1:3, 2], adscal)], [inc_subtensor(admat[1:3, 2], adscal)],
[admat_val, 1], IncSubtensor) [admat_val, 1], IncSubtensor)
self._compile_and_check([admat, bdmat], self._compile_and_check([admat, bdmat],
[set_subtensor(admat[2:4], bdmat)], [set_subtensor(admat[2:4], bdmat)],
[admat_val, [[1, 2, 3, 4]]], IncSubtensor) [admat_val, [[1, 2, 3, 4]]], IncSubtensor)
self._compile_and_check([admat, advec], self._compile_and_check([admat, advec],
[set_subtensor(admat[2], advec)], [set_subtensor(admat[2], advec)],
[admat_val, [1, 2, 3, 4]], IncSubtensor) [admat_val, [1, 2, 3, 4]], IncSubtensor)
self._compile_and_check([admat, adscal], self._compile_and_check([admat, adscal],
[set_subtensor(admat[2, 3], adscal)], [set_subtensor(admat[2, 3], adscal)],
[admat_val, 1], IncSubtensor) [admat_val, 1], IncSubtensor)
self._compile_and_check([admat, adscal], self._compile_and_check([admat, adscal],
[set_subtensor(admat[1:3, 2], adscal)], [set_subtensor(admat[1:3, 2], adscal)],
[admat_val, 1], IncSubtensor) [admat_val, 1], IncSubtensor)
adtens4 = dtensor4() adtens4 = dtensor4()
bdtens4 = dtensor4() bdtens4 = dtensor4()
adtens4_val = rand(3, 4, 2, 5) adtens4_val = rand(3, 4, 2, 5)
self._compile_and_check([adtens4, bdtens4], self._compile_and_check([adtens4, bdtens4],
[inc_subtensor(adtens4[::, 2:4, ::, ::], bdtens4)], [inc_subtensor(adtens4[::, 2:4, ::, ::], bdtens4)],
[adtens4_val, [[[[1, 2, 3, 4, 5]]]]], IncSubtensor, [adtens4_val, [[[[1, 2, 3, 4, 5]]]]], IncSubtensor,
warn=False) warn=False)
self._compile_and_check([adtens4, bdmat], self._compile_and_check([adtens4, bdmat],
[inc_subtensor(adtens4[2, 2:4, 1, ::], bdmat)], [inc_subtensor(adtens4[2, 2:4, 1, ::], bdmat)],
[adtens4_val, [[1, 2, 3, 4, 5]]], IncSubtensor) [adtens4_val, [[1, 2, 3, 4, 5]]], IncSubtensor)
self._compile_and_check([adtens4, advec], self._compile_and_check([adtens4, advec],
[inc_subtensor(adtens4[0, 1, ::, 4], advec)], [inc_subtensor(adtens4[0, 1, ::, 4], advec)],
[adtens4_val, [1, 2]], IncSubtensor) [adtens4_val, [1, 2]], IncSubtensor)
self._compile_and_check([adtens4, adscal], self._compile_and_check([adtens4, adscal],
[inc_subtensor(adtens4[1:3, 1, ::, 2:4], adscal)], [inc_subtensor(adtens4[1:3, 1, ::, 2:4], adscal)],
[adtens4_val, 1], IncSubtensor) [adtens4_val, 1], IncSubtensor)
self._compile_and_check([adtens4, bdtens4], self._compile_and_check([adtens4, bdtens4],
[set_subtensor(adtens4[::, 2:4, ::, ::], bdtens4)], [set_subtensor(adtens4[::, 2:4, ::, ::], bdtens4)],
[adtens4_val, [[[[1, 2, 3, 4, 5]]]]], IncSubtensor, [adtens4_val, [[[[1, 2, 3, 4, 5]]]]], IncSubtensor,
warn=False) warn=False)
self._compile_and_check([adtens4, bdmat], self._compile_and_check([adtens4, bdmat],
[set_subtensor(adtens4[2, 2:4, 1, ::], bdmat)], [set_subtensor(adtens4[2, 2:4, 1, ::], bdmat)],
[adtens4_val, [[1, 2, 3, 4, 5]]], IncSubtensor) [adtens4_val, [[1, 2, 3, 4, 5]]], IncSubtensor)
self._compile_and_check([adtens4, advec], self._compile_and_check([adtens4, advec],
[set_subtensor(adtens4[0, 1, ::, 4], advec)], [set_subtensor(adtens4[0, 1, ::, 4], advec)],
[adtens4_val, [1, 2]], IncSubtensor) [adtens4_val, [1, 2]], IncSubtensor)
self._compile_and_check([adtens4, adscal], self._compile_and_check([adtens4, adscal],
[set_subtensor(adtens4[1:3, 1, ::, 2:4], adscal)], [set_subtensor(adtens4[1:3, 1, ::, 2:4], adscal)],
[adtens4_val, 1], IncSubtensor) [adtens4_val, 1], IncSubtensor)
# AdvancedIncSubtensor1 # AdvancedIncSubtensor1
admat = dmatrix() admat = dmatrix()
...@@ -1695,39 +1692,39 @@ class TestInferShape(utt.InferShapeTester): ...@@ -1695,39 +1692,39 @@ class TestInferShape(utt.InferShapeTester):
admat_val = rand(5, 4) admat_val = rand(5, 4)
aivec_val = [2, 3] aivec_val = [2, 3]
self._compile_and_check([admat, bdmat], self._compile_and_check([admat, bdmat],
[set_subtensor(admat[aivec_val], bdmat)], [set_subtensor(admat[aivec_val], bdmat)],
[admat_val, [[1, 2, 3, 4]]], AdvancedIncSubtensor1) [admat_val, [[1, 2, 3, 4]]], AdvancedIncSubtensor1)
aivec_val = [1, 3, 2] aivec_val = [1, 3, 2]
self._compile_and_check([admat, advec], self._compile_and_check([admat, advec],
[set_subtensor(admat[aivec_val], advec)], [set_subtensor(admat[aivec_val], advec)],
[admat_val, [1, 2, 3, 4]], AdvancedIncSubtensor1) [admat_val, [1, 2, 3, 4]], AdvancedIncSubtensor1)
aivec_val = [0, 3, 0] aivec_val = [0, 3, 0]
self._compile_and_check([admat, adscal], self._compile_and_check([admat, adscal],
[set_subtensor(admat[aivec_val], adscal)], [set_subtensor(admat[aivec_val], adscal)],
[admat_val, 1], AdvancedIncSubtensor1) [admat_val, 1], AdvancedIncSubtensor1)
bdtens4 = dtensor4() bdtens4 = dtensor4()
adtens4_val = rand(4, 3, 2, 5) adtens4_val = rand(4, 3, 2, 5)
aivec_val = [2, 3] aivec_val = [2, 3]
self._compile_and_check([adtens4, bdtens4], self._compile_and_check([adtens4, bdtens4],
[set_subtensor(adtens4[aivec_val], bdtens4)], [set_subtensor(adtens4[aivec_val], bdtens4)],
[adtens4_val, [[[[1, 2, 3, 4, 5]]]]], [adtens4_val, [[[[1, 2, 3, 4, 5]]]]],
AdvancedIncSubtensor1, AdvancedIncSubtensor1,
warn=False) warn=False)
aivec_val = [1, 3, 2] aivec_val = [1, 3, 2]
self._compile_and_check([adtens4, advec], self._compile_and_check([adtens4, advec],
[set_subtensor(adtens4[aivec_val], advec)], [set_subtensor(adtens4[aivec_val], advec)],
[adtens4_val, [1, 2, 3, 4, 5]], [adtens4_val, [1, 2, 3, 4, 5]],
AdvancedIncSubtensor1) AdvancedIncSubtensor1)
aivec_val = [0, 3, 0] aivec_val = [0, 3, 0]
self._compile_and_check([adtens4, adscal], self._compile_and_check([adtens4, adscal],
[set_subtensor(adtens4[aivec_val], adscal)], [set_subtensor(adtens4[aivec_val], adscal)],
[adtens4_val, 1], [adtens4_val, 1],
AdvancedIncSubtensor1) AdvancedIncSubtensor1)
aivec_val = [2, 3] aivec_val = [2, 3]
self._compile_and_check([admat, bdmat], self._compile_and_check([admat, bdmat],
...@@ -1737,43 +1734,43 @@ class TestInferShape(utt.InferShapeTester): ...@@ -1737,43 +1734,43 @@ class TestInferShape(utt.InferShapeTester):
aivec_val = [1, 3, 2] aivec_val = [1, 3, 2]
self._compile_and_check([admat, advec], self._compile_and_check([admat, advec],
[inc_subtensor(admat[aivec_val], advec)], [inc_subtensor(admat[aivec_val], advec)],
[admat_val, [1, 2, 3, 4]], AdvancedIncSubtensor1) [admat_val, [1, 2, 3, 4]], AdvancedIncSubtensor1)
aivec_val = [0, 3, 0] aivec_val = [0, 3, 0]
self._compile_and_check([admat, adscal], self._compile_and_check([admat, adscal],
[inc_subtensor(admat[aivec_val], adscal)], [inc_subtensor(admat[aivec_val], adscal)],
[admat_val, 1], AdvancedIncSubtensor1) [admat_val, 1], AdvancedIncSubtensor1)
bdtens4 = dtensor4() bdtens4 = dtensor4()
adtens4_val = rand(4, 3, 2, 5) adtens4_val = rand(4, 3, 2, 5)
aivec_val = [2, 3] aivec_val = [2, 3]
self._compile_and_check([adtens4, bdtens4], self._compile_and_check([adtens4, bdtens4],
[inc_subtensor(adtens4[aivec_val], bdtens4)], [inc_subtensor(adtens4[aivec_val], bdtens4)],
[adtens4_val, [[[[1, 2, 3, 4, 5]]], [adtens4_val, [[[[1, 2, 3, 4, 5]]],
[[[6, 7, 8, 9, 10]]]]], [[[6, 7, 8, 9, 10]]]]],
AdvancedIncSubtensor1, AdvancedIncSubtensor1,
warn=False) warn=False)
aivec_val = [1, 2, 1] aivec_val = [1, 2, 1]
self._compile_and_check([adtens4, advec], self._compile_and_check([adtens4, advec],
[inc_subtensor(adtens4[aivec_val], advec)], [inc_subtensor(adtens4[aivec_val], advec)],
[adtens4_val, [1, 2, 3, 4, 5]], [adtens4_val, [1, 2, 3, 4, 5]],
AdvancedIncSubtensor1) AdvancedIncSubtensor1)
aivec_val = [0, 3, 0] aivec_val = [0, 3, 0]
self._compile_and_check([adtens4, adscal], self._compile_and_check([adtens4, adscal],
[inc_subtensor(adtens4[aivec_val], adscal)], [inc_subtensor(adtens4[aivec_val], adscal)],
[adtens4_val, 2], [adtens4_val, 2],
AdvancedIncSubtensor1) AdvancedIncSubtensor1)
# AdvancedIncSubtensor # AdvancedIncSubtensor
aivec_val = [1, 3, 2] aivec_val = [1, 3, 2]
bivec_val = [0, 3, 3] bivec_val = [0, 3, 3]
advec_val = [23, 24, 25] advec_val = [23, 24, 25]
self._compile_and_check([admat, advec], self._compile_and_check([admat, advec],
[set_subtensor(admat[aivec_val, bivec_val], advec)], [set_subtensor(admat[aivec_val, bivec_val], advec)],
[admat_val, advec_val], AdvancedIncSubtensor) [admat_val, advec_val], AdvancedIncSubtensor)
def test_adv_sub(self): def test_adv_sub(self):
admat = dmatrix() admat = dmatrix()
......
...@@ -46,7 +46,6 @@ whitelist_flake8 = [ ...@@ -46,7 +46,6 @@ whitelist_flake8 = [
"typed_list/tests/__init__.py", "typed_list/tests/__init__.py",
"tensor/__init__.py", "tensor/__init__.py",
"tensor/tests/__init__.py", "tensor/tests/__init__.py",
"tensor/tests/test_subtensor.py",
"tensor/tests/test_utils.py", "tensor/tests/test_utils.py",
"tensor/tests/test_nlinalg.py", "tensor/tests/test_nlinalg.py",
"tensor/tests/test_shared_randomstreams.py", "tensor/tests/test_shared_randomstreams.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论