提交 155e2eb9 authored 作者: Frederic's avatar Frederic

crash fix: theano.sparse.Dot didn't created the output var with the right number…

crash fix: theano.sparse.Dot didn't created the output var with the right number of dimensions when one input was a vector.
上级 d82dd98a
...@@ -3152,12 +3152,24 @@ class Dot(gof.op.Op): ...@@ -3152,12 +3152,24 @@ class Dot(gof.op.Op):
if not x_is_sparse_var: if not x_is_sparse_var:
x = tensor.as_tensor_variable(x) x = tensor.as_tensor_variable(x)
if x.ndim not in (1, 2):
raise TypeError(
'theano.sparse.Dot: input 0 (0-indexed) must have ndim of '
'1 or 2, %d given.' % x.ndim)
if not y_is_sparse_var: if not y_is_sparse_var:
y = tensor.as_tensor_variable(y) y = tensor.as_tensor_variable(y)
if y.ndim not in (1, 2):
raise TypeError(
'theano.sparse.Dot: input 1 (1-indexed) must have ndim of '
'1 or 2, %d given.' % y.ndim)
if y.ndim == 1 or x.ndim == 1:
bz = (False,)
else:
bz = (False, False)
return gof.Apply(self, [x, y], [tensor.tensor(dtype=dtype_out, return gof.Apply(self, [x, y], [tensor.tensor(dtype=dtype_out,
broadcastable=(False, False))]) broadcastable=bz)])
def perform(self, node, inputs, out): def perform(self, node, inputs, out):
x, y = inputs x, y = inputs
......
...@@ -1027,8 +1027,9 @@ class test_structureddot(unittest.TestCase): ...@@ -1027,8 +1027,9 @@ class test_structureddot(unittest.TestCase):
overhead_tol) overhead_tol)
class DotTests(unittest.TestCase): class DotTests(utt.InferShapeTester):
def setUp(self): def setUp(self):
super(DotTests, self).setUp()
x_size = (10, 100) x_size = (10, 100)
y_size = (100, 1000) y_size = (100, 1000)
utt.seed_rng() utt.seed_rng()
...@@ -1043,10 +1044,15 @@ class DotTests(unittest.TestCase): ...@@ -1043,10 +1044,15 @@ class DotTests(unittest.TestCase):
numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX) numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX)
self.y_csc = scipy.sparse.csc_matrix( self.y_csc = scipy.sparse.csc_matrix(
numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX) numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX)
self.v_10 = numpy.asarray(numpy.random.uniform(-1, 1, 10),
dtype=theano.config.floatX)
self.v_100 = numpy.asarray(numpy.random.uniform(-1, 1, 100),
dtype=theano.config.floatX)
def test_csr_dense(self): def test_csr_dense(self):
x = theano.sparse.csr_matrix('x') x = theano.sparse.csr_matrix('x')
y = theano.tensor.matrix('y') y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
f_a = theano.function([x, y], theano.sparse.dot(x, y)) f_a = theano.function([x, y], theano.sparse.dot(x, y))
f_b = lambda x, y: x * y f_b = lambda x, y: x * y
...@@ -1054,20 +1060,20 @@ class DotTests(unittest.TestCase): ...@@ -1054,20 +1060,20 @@ class DotTests(unittest.TestCase):
assert _allclose(f_a(self.x_csr, self.y), f_b(self.x_csr, self.y)) assert _allclose(f_a(self.x_csr, self.y), f_b(self.x_csr, self.y))
# Test infer_shape # Test infer_shape
f_a = theano.function([x, y], theano.sparse.dot(x, y).shape) self._compile_and_check([x, y], [theano.sparse.dot(x, y)],
f_b = lambda x, y: (x * y).shape [self.x_csr, self.y],
assert numpy.all(f_a(self.x_csr, self.y) == f_b(self.x_csr, self.y)) (Dot, Usmm, UsmmCscDense))
topo = f_a.maker.fgraph.toposort() self._compile_and_check([v, x], [theano.sparse.dot(v, x)],
if theano.config.mode != 'FAST_COMPILE': [self.v_10, self.x_csr],
nb = 0 (Dot, Usmm, UsmmCscDense))
else: self._compile_and_check([x, v], [theano.sparse.dot(x, v)],
nb = 1 [self.x_csr, self.v_100],
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense)) (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb
def test_csc_dense(self): def test_csc_dense(self):
x = theano.sparse.csc_matrix('x') x = theano.sparse.csc_matrix('x')
y = theano.tensor.matrix('y') y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
f_a = theano.function([x, y], theano.sparse.dot(x, y)) f_a = theano.function([x, y], theano.sparse.dot(x, y))
f_b = lambda x, y: x * y f_b = lambda x, y: x * y
...@@ -1075,16 +1081,15 @@ class DotTests(unittest.TestCase): ...@@ -1075,16 +1081,15 @@ class DotTests(unittest.TestCase):
assert _allclose(f_a(self.x_csc, self.y), f_b(self.x_csc, self.y)) assert _allclose(f_a(self.x_csc, self.y), f_b(self.x_csc, self.y))
# Test infer_shape # Test infer_shape
f_a = theano.function([x, y], theano.sparse.dot(x, y).shape) self._compile_and_check([x, y], [theano.sparse.dot(x, y)],
f_b = lambda x, y: (x * y).shape [self.x_csc, self.y],
assert numpy.all(f_a(self.x_csc, self.y) == f_b(self.x_csc, self.y)) (Dot, Usmm, UsmmCscDense))
topo = f_a.maker.fgraph.toposort() self._compile_and_check([v, x], [theano.sparse.dot(v, x)],
if theano.config.mode != 'FAST_COMPILE': [self.v_10, self.x_csc],
nb = 0 (Dot, Usmm, UsmmCscDense))
else: self._compile_and_check([x, v], [theano.sparse.dot(x, v)],
nb = 1 [self.x_csc, self.v_100],
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense)) (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb
def test_sparse_sparse(self): def test_sparse_sparse(self):
for d1, d2 in [('float32', 'float32'), for d1, d2 in [('float32', 'float32'),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论