提交 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
* 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.)
Sparse
* Implement theano.sparse.mul(sparse1, sparse2) when both input don't
have the same sparsity pattern. (Frederic B.)
Sparse Sandbox graduate
* Remove0 op: it removes stored elements with value 0. (Frederic B.)
......
......@@ -1180,12 +1180,9 @@ class MulSS(gof.op.Op):
assert _is_sparse(x) and _is_sparse(y)
assert len(x.shape) == 2
assert y.shape == x.shape
if (numpy.all(y.indptr == x.indptr) and
numpy.all(y.indices == x.indices)):
out[0] = y.copy()
out[0].data *= x.data
else:
raise NotImplementedError() # RowScale / ColScale
# This call the element-wise multiple
# x * y call dot...
out[0] = x.multiply(y)
def grad(self, (x, y), (gz,)):
return y * gz, x * gz
......
......@@ -323,17 +323,17 @@ class T_AddMul(unittest.TestCase):
def testMulSS(self):
self._testSS(mul,
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):
self._testSD(mul,
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):
self._testDS(mul,
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]]),
array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
......@@ -361,15 +361,12 @@ class T_AddMul(unittest.TestCase):
val = eval_outputs([apb])
self.assertTrue(val.shape == (3, 2))
if op is add:
self.assertTrue(numpy.all(val.todense() == (a + b).todense()))
ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val.todense() == ans))
self.assertTrue(numpy.all(val.todense() == (array1 + array2)))
verify_grad_sparse(op, [a, b], structured=False)
elif op is mul:
self.assertTrue(numpy.all(val.todense()
== (a.multiply(b)).todense()))
ans = numpy.array([[1, 0], [9, 0], [0, 36]])
self.assertTrue(numpy.all(val.todense() == ans))
== (array1 * array2)))
verify_grad_sparse(op, [a, b], structured=False)
def _testSD(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]),
array2=numpy.asarray([[0, 2.], [0, 4], [5, 0]])):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论