提交 8d3412b5 authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: Frederic Bastien

Make sure the input of Neg.impl is not a numpy.bool_

Otherwise, -False is True, and -True is False.
上级 b3d953d5
...@@ -2399,9 +2399,19 @@ round_half_away_from_zero = RoundHalfAwayFromZero(same_out_float_only) ...@@ -2399,9 +2399,19 @@ round_half_away_from_zero = RoundHalfAwayFromZero(same_out_float_only)
class Neg(UnaryScalarOp): class Neg(UnaryScalarOp):
# We can use numpy.negative here, because even if it gives unexpected
# results on Boolean arrays, it will be passed other dtypes as Theano
# does not have a Boolean type for tensors.
nfunc_spec = ('negative', 1, 1) nfunc_spec = ('negative', 1, 1)
def impl(self, x): def impl(self, x):
# We have to make sure x is not a numpy.bool_, because
# `-numpy.bool_(True)` is `False` (we want 0), and
# `-numpy.bool_(False)` is `True` (we want 1).
# This happens for Composite, as the intermediate results are not
# casted in the dtype of the intermediate variable in general.
if isinstance(x, numpy.bool_):
x = numpy.int8(x)
return -x return -x
def grad(self, inputs, gout): def grad(self, inputs, gout):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论