提交 1d468fa5 authored 作者: Frederic's avatar Frederic

Implement theano.sparse.mul(sparse1, sparse2) when both input don't have the same sparsity pattern.

Also add test for the mul grad.
上级 64bdbbe2
...@@ -35,6 +35,11 @@ New Features ...@@ -35,6 +35,11 @@ New Features
* MRG random now raises an error with a clear message when the passed shape * MRG random now raises an error with a clear message when the passed shape
contains dimensions with bad value like 0. (Frédéric B. reported by Ian G.) contains dimensions with bad value like 0. (Frédéric B. reported by Ian G.)
Sparse
* Implement theano.sparse.mul(sparse1, sparse2) when both input don't
have the same sparsity pattern. (Frederic B.)
Sparse Sandbox graduate Sparse Sandbox graduate
* Remove0 op: it removes stored elements with value 0. (Frederic B.) * Remove0 op: it removes stored elements with value 0. (Frederic B.)
......
...@@ -1180,12 +1180,9 @@ class MulSS(gof.op.Op): ...@@ -1180,12 +1180,9 @@ class MulSS(gof.op.Op):
assert _is_sparse(x) and _is_sparse(y) assert _is_sparse(x) and _is_sparse(y)
assert len(x.shape) == 2 assert len(x.shape) == 2
assert y.shape == x.shape assert y.shape == x.shape
if (numpy.all(y.indptr == x.indptr) and # This call the element-wise multiple
numpy.all(y.indices == x.indices)): # x * y call dot...
out[0] = y.copy() out[0] = x.multiply(y)
out[0].data *= x.data
else:
raise NotImplementedError() # RowScale / ColScale
def grad(self, (x, y), (gz,)): def grad(self, (x, y), (gz,)):
return y * gz, x * gz return y * gz, x * gz
......
...@@ -323,17 +323,17 @@ class T_AddMul(unittest.TestCase): ...@@ -323,17 +323,17 @@ class T_AddMul(unittest.TestCase):
def testMulSS(self): def testMulSS(self):
self._testSS(mul, self._testSS(mul,
numpy.array([[1., 0], [3, 0], [0, 6]]), numpy.array([[1., 0], [3, 0], [0, 6]]),
numpy.array([[1., 0], [3, 0], [0, 6]])) numpy.array([[1., 2], [3, 0], [0, 6]]))
def testMulSD(self): def testMulSD(self):
self._testSD(mul, self._testSD(mul,
numpy.array([[1., 0], [3, 0], [0, 6]]), numpy.array([[1., 0], [3, 0], [0, 6]]),
numpy.array([[1., 0], [3, 0], [0, 6]])) numpy.array([[1., 2], [3, 0], [0, 6]]))
def testMulDS(self): def testMulDS(self):
self._testDS(mul, self._testDS(mul,
numpy.array([[1., 0], [3, 0], [0, 6]]), numpy.array([[1., 0], [3, 0], [0, 6]]),
numpy.array([[1., 0], [3, 0], [0, 6]])) numpy.array([[1., 2], [3, 0], [0, 6]]))
def _testSS(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]), def _testSS(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]),
array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])): array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
...@@ -361,15 +361,12 @@ class T_AddMul(unittest.TestCase): ...@@ -361,15 +361,12 @@ class T_AddMul(unittest.TestCase):
val = eval_outputs([apb]) val = eval_outputs([apb])
self.assertTrue(val.shape == (3, 2)) self.assertTrue(val.shape == (3, 2))
if op is add: if op is add:
self.assertTrue(numpy.all(val.todense() == (a + b).todense())) self.assertTrue(numpy.all(val.todense() == (array1 + array2)))
ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val.todense() == ans))
verify_grad_sparse(op, [a, b], structured=False) verify_grad_sparse(op, [a, b], structured=False)
elif op is mul: elif op is mul:
self.assertTrue(numpy.all(val.todense() self.assertTrue(numpy.all(val.todense()
== (a.multiply(b)).todense())) == (array1 * array2)))
ans = numpy.array([[1, 0], [9, 0], [0, 36]]) verify_grad_sparse(op, [a, b], structured=False)
self.assertTrue(numpy.all(val.todense() == ans))
def _testSD(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]), def _testSD(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]),
array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])): array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论