提交 2f29cf86 authored 作者: Frederic's avatar Frederic

[CRASH] in theano.grad when mulsd do not return a dense grad for dense input.

Also make theano.grad test the output is good.
上级 aa892a7e
......@@ -1089,6 +1089,14 @@ def _populate_grad_dict(var_to_app_to_idx,
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))
# 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
input_grads = list(input_grads)
......
......@@ -2244,7 +2244,7 @@ class MulSD(gof.op.Op):
def grad(self, (x, y), (gz,)):
assert _is_sparse_variable(x) and _is_dense_variable(y)
assert _is_sparse_variable(gz)
return y * gz, x * gz
return y * gz, dense_from_sparse(x * gz)
def infer_shape(self, node, shapes):
return [shapes[0]]
......
......@@ -561,6 +561,8 @@ class T_AddMul(unittest.TestCase):
theano.shared(array1)]:
for dtype1, dtype2 in [('float64', 'int8'),
('int8', 'float64'),
# Needed to test the grad
('float32', 'float64'),
]:
a = a.astype(dtype1)
b = mtype(array2).astype(dtype2)
......@@ -580,6 +582,8 @@ class T_AddMul(unittest.TestCase):
self.assertTrue(numpy.all(val == ans))
if isinstance(a, theano.Constant):
a = a.data
if getattr(a, 'owner', None):
continue
if dtype1.startswith('float') and dtype2.startswith('float'):
verify_grad_sparse(op, [a, b], structured=True)
elif op is mul:
......@@ -589,6 +593,8 @@ class T_AddMul(unittest.TestCase):
[[1, 0], [9, 0], [0, 36]])))
if isinstance(a, theano.Constant):
a = a.data
if getattr(a, 'owner', None):
continue
if dtype1.startswith('float') and dtype2.startswith('float'):
verify_grad_sparse(op, [a, b], structured=False)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论