提交 647921d7 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Merge pull request #2162 from SinaHonari/issue1914

Issue1914
...@@ -1271,6 +1271,26 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1271,6 +1271,26 @@ class TestAdvancedSubtensor(unittest.TestCase):
[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):
# tests advanced indexing in Theano for 2D and 3D tensors
rng = numpy.random.RandomState(utt.seed_rng())
a = rng.uniform(size=(3, 3))
b = theano.shared(a)
i = tensor.iscalar()
j = tensor.iscalar()
z = b[[i, j], :]
f1 = theano.function([i, j], z)
cmd = f1(0, 1) == a[[0, 1], :]
self.assertTrue(numpy.all(cmp))
aa = rng.uniform(size=(4, 2, 3))
bb = theano.shared(aa)
k = tensor.iscalar()
z = bb[[i, j, k], :, i:k]
f2 = theano.function([i, j, k], z)
cmd = f2(0, 1, 2) == aa[[0, 1, 2], :, 0:2]
self.assertTrue(numpy.all(cmp))
class TestInferShape(utt.InferShapeTester): class TestInferShape(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
......
...@@ -16,6 +16,12 @@ from theano.tensor.type import TensorType ...@@ -16,6 +16,12 @@ from theano.tensor.type import TensorType
from theano.configparser import config from theano.configparser import config
def equal_slices(s1, s2):
return (s1.start == s2.start and
s1.stop == s2.stop and
s1.step == s2.step)
class AsTensorError(TypeError): class AsTensorError(TypeError):
"""Raised when as_tensor_variable isn't able to create a """Raised when as_tensor_variable isn't able to create a
TensorVariable. TensorVariable.
...@@ -375,15 +381,17 @@ class _tensor_py_operators: ...@@ -375,15 +381,17 @@ class _tensor_py_operators:
if advanced: if advanced:
if (axis is not None if (axis is not None
and all(a == slice(None) for a in args[:axis]) and all(isinstance(a, slice) and
and all(a == slice(None) for a in args[axis + 1:]) equal_slices(a, slice(None)) for a in args[:axis])
and all(isinstance(a, slice) and
equal_slices(a, slice(None)) for a in args[axis + 1:])
and isinstance(args[axis], ( and isinstance(args[axis], (
numpy.ndarray, numpy.ndarray,
list, list,
TensorVariable, TensorVariable,
TensorConstant, TensorConstant,
theano.tensor.sharedvar.TensorSharedVariable))): theano.tensor.sharedvar.TensorSharedVariable))):
return self.take(arg, axis) return self.take(args[axis], axis)
else: else:
return theano.tensor.subtensor.advanced_subtensor(self, *args) return theano.tensor.subtensor.advanced_subtensor(self, *args)
else: else:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论