提交 09e09da0 authored 作者: Frederic's avatar Frederic

sparse.Dot() test grad with mixed sparse/dense.

Do not enforce that if the input is sparse, we need the grad to return sparse.
上级 2b076111
......@@ -1088,14 +1088,17 @@ def _populate_grad_dict(var_to_app_to_idx,
if len(input_grads) != len(inputs):
raise ValueError(("%s returned the wrong number of" +
" gradient terms.") % str(node.op))
for ig, i in zip(input_grads, inputs):
if (not isinstance(ig.type, DisconnectedType) and
type(ig.type) != type(i.type)):
raise ValueError(
"%s returned the wrong type for gradient terms."
" Sparse inputs must have sparse grads and dense"
" inputs must have dense grad" % str(node.op))
# We can not enforce this, as AdvancedSubtensor1 have an option to
# return the sparse grad for optimization reason.
# for ig, i in zip(input_grads, inputs):
# if (not isinstance(ig.type, (DisconnectedType, NullType)) and
# type(ig.type) != type(i.type)):
# raise ValueError(
# "%s returned the wrong type for gradient terms."
# " Sparse inputs must have sparse grads and dense"
# " inputs must have dense grad. Got %s, expected %s" % (
# str(node.op), ig.type, i.type))
# must convert to list in case the op returns a tuple
# we won't be able to post-process out the Nones if it does that
......
......@@ -3709,11 +3709,15 @@ class Dot(gof.op.Op):
raise NotImplementedError()
def make_node(self, x, y):
dtype_out = scalar.upcast(x.type.dtype, y.type.dtype)
dtype_out = scalar.upcast(x.dtype, y.dtype)
# Sparse dot product should have at least one sparse variable
# as input. If the other one is not sparse, it has to be converted
# into a tensor.
if isinstance(x, scipy.sparse.spmatrix):
x = as_sparse_variable(x)
if isinstance(y, scipy.sparse.spmatrix):
y = as_sparse_variable(y)
x_is_sparse_var = _is_sparse_variable(x)
y_is_sparse_var = _is_sparse_variable(y)
......
......@@ -1337,6 +1337,20 @@ class DotTests(utt.InferShapeTester):
dtype=intX)
f(i, a)
def test_csr_dense_grad(self):
#shortcut: testing csc in float32, testing csr in float64
# allocate a random sparse matrix
spmat = sp.csr_matrix(random_lil((4, 3), 'float64', 3))
mat = numpy.asarray(numpy.random.randn(2, 4), 'float64')
def buildgraph_T(mat):
return Dot()(mat, spmat)
theano.tests.unittest_tools.verify_grad(buildgraph_T, [mat])
class UsmmTests(unittest.TestCase):
""" Test the Usmm and UsmmCscDense class and related optimization """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论