提交 970c704d authored 作者: Frederic's avatar Frederic

Allow theanos.sparse.add() with ndarray-like as input

上级 e637a632
...@@ -1843,6 +1843,8 @@ class AddSD(gof.op.Op): ...@@ -1843,6 +1843,8 @@ class AddSD(gof.op.Op):
def infer_shape(self, node, shapes): def infer_shape(self, node, shapes):
return [shapes[3]] return [shapes[3]]
def c_code_cache_version(self):
return (1,)
add_s_d = AddSD() add_s_d = AddSD()
...@@ -1918,6 +1920,10 @@ def add(x, y): ...@@ -1918,6 +1920,10 @@ def add(x, y):
x = as_sparse_variable(x) x = as_sparse_variable(x)
if hasattr(y, 'getnnz'): if hasattr(y, 'getnnz'):
y = as_sparse_variable(y) y = as_sparse_variable(y)
if not isinstance(x, theano.Variable):
x = theano.tensor.as_tensor_variable(x)
if not isinstance(y, theano.Variable):
y = theano.tensor.as_tensor_variable(y)
x_is_sparse_variable = _is_sparse_variable(x) x_is_sparse_variable = _is_sparse_variable(x)
y_is_sparse_variable = _is_sparse_variable(y) y_is_sparse_variable = _is_sparse_variable(y)
......
...@@ -567,67 +567,57 @@ class T_AddMul(unittest.TestCase): ...@@ -567,67 +567,57 @@ class T_AddMul(unittest.TestCase):
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]])):
for mtype in _mtypes: for mtype in _mtypes:
a = numpy.array(array1) for a in [numpy.array(array1), tensor.as_tensor_variable(array1)]:
aR = tensor.as_tensor_variable(a)
self.assertFalse(aR.data is a) # constants are copied
self.assertTrue(_is_dense(a))
self.assertTrue(_is_dense_variable(aR))
b = mtype(array2) b = mtype(array2)
bR = as_sparse_variable(b) bR = as_sparse_variable(b)
self.assertFalse(bR.data is b) # constants are copied self.assertFalse(bR.data is b) # constants are copied
self.assertTrue(_is_sparse(b)) self.assertTrue(_is_sparse(b))
self.assertTrue(_is_sparse_variable(bR)) self.assertTrue(_is_sparse_variable(bR))
apb = op(aR, bR) apb = op(a, bR)
self.assertTrue(apb.type.dtype == aR.type.dtype, apb.type.dtype) self.assertTrue(apb.type.dtype == a.dtype, apb.type.dtype)
self.assertTrue(apb.type.dtype == bR.type.dtype, apb.type.dtype) self.assertTrue(apb.type.dtype == bR.type.dtype, apb.type.dtype)
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(_is_dense_variable(apb)) self.assertTrue(_is_dense_variable(apb))
self.assertTrue(numpy.all(val == (a + b))) self.assertTrue(numpy.all(val == (array1 + b)))
ans = numpy.array([[1., 2], [3, 4], [5, 6]]) ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val == ans)) self.assertTrue(numpy.all(val == ans))
elif op is mul: elif op is mul:
self.assertTrue(_is_sparse_variable(apb)) self.assertTrue(_is_sparse_variable(apb))
self.assertTrue(numpy.all(val.todense() == (b.multiply(a)))) self.assertTrue(numpy.all(val.todense() == (b.multiply(array1))))
self.assertTrue(numpy.all(val.todense() == numpy.array( self.assertTrue(numpy.all(val.todense() == numpy.array(
[[1, 0], [9, 0], [0, 36]]))) [[1, 0], [9, 0], [0, 36]])))
def _testDS(self, op, array1=numpy.array([[1., 0], [3, 0], [0, 6]]), def _testDS(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]])):
for mtype in _mtypes: for mtype in _mtypes:
for b in [numpy.asarray(array2), tensor.as_tensor_variable(array2)]:
a = mtype(array1) a = mtype(array1)
aR = as_sparse_variable(a) aR = as_sparse_variable(a)
self.assertFalse(aR.data is a) self.assertFalse(aR.data is a)
self.assertTrue(_is_sparse(a)) self.assertTrue(_is_sparse(a))
self.assertTrue(_is_sparse_variable(aR)) self.assertTrue(_is_sparse_variable(aR))
b = numpy.asarray(array2) apb = op(aR, b)
bR = tensor.as_tensor_variable(b)
self.assertFalse(bR.data is b)
self.assertTrue(_is_dense(b))
self.assertTrue(_is_dense_variable(bR))
apb = op(aR, bR)
self.assertTrue(apb.type.dtype == aR.type.dtype, apb.type.dtype) self.assertTrue(apb.type.dtype == aR.type.dtype, apb.type.dtype)
self.assertTrue(apb.type.dtype == bR.type.dtype, apb.type.dtype) self.assertTrue(apb.type.dtype == b.dtype, apb.type.dtype)
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(_is_dense_variable(apb)) self.assertTrue(_is_dense_variable(apb))
self.assertTrue(numpy.all(val == (a + b))) self.assertTrue(numpy.all(val == (a + array2)))
ans = numpy.array([[1., 2], [3, 4], [5, 6]]) ans = numpy.array([[1., 2], [3, 4], [5, 6]])
self.assertTrue(numpy.all(val == ans)) self.assertTrue(numpy.all(val == ans))
elif op is mul: elif op is mul:
self.assertTrue(_is_sparse_variable(apb)) self.assertTrue(_is_sparse_variable(apb))
ans = numpy.array([[1, 0], [9, 0], [0, 36]]) ans = numpy.array([[1, 0], [9, 0], [0, 36]])
self.assertTrue(numpy.all(val.todense() == (a.multiply(b)))) self.assertTrue(numpy.all(val.todense() == (a.multiply(array2))))
self.assertTrue(numpy.all(val.todense() == ans)) self.assertTrue(numpy.all(val.todense() == ans))
def test_upcast(self): def test_upcast(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论