提交 e571b789 authored 作者: Ian Goodfellow's avatar Ian Goodfellow

made true div return 0 gradient if its output is an integer and

a floating point gradient otherwise
上级 0e7eb0cc
...@@ -1305,22 +1305,28 @@ class TrueDiv(BinaryScalarOp): ...@@ -1305,22 +1305,28 @@ class TrueDiv(BinaryScalarOp):
return "%(z)s = %(x)s / %(y)s;" % locals() return "%(z)s = %(x)s / %(y)s;" % locals()
def grad(self, (x, y), (gz, )): def grad(self, (x, y), (gz, )):
if x.type in complex_types: if x.type in complex_types:
raise NotImplementedError() raise NotImplementedError()
if x.type in float_types:
first_part = cast(gz / y, x.type.dtype) # If the output of this op is discrete, then it
else: # it is locally flat everywhere, so the gradient
assert x.type in discrete_types # through it is 0.
first_part = None # This is different from it not being connected
# to the output; x/y is still a function of x
# and y; it's just a step function.
if (x/y).type in discrete_types:
return [ x.zeros_like(), y.zeros_like() ]
first_part = gz / y
if y.type in complex_types: if y.type in complex_types:
raise NotImplementedError() raise NotImplementedError()
if y.type in float_types:
second_part = cast(-(gz * x) / (y * y), y.type.dtype) second_part = cast(-(gz * x) / (y * y), y.type.dtype)
else:
assert y.type in discrete_types
second_part = None
return first_part, second_part return first_part, second_part
true_div = TrueDiv(upcast_out, name='true_div') true_div = TrueDiv(upcast_out, name='true_div')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论