def test_abs_mul_div(self):
"""
test that if we have
4 * x / abs(2*x) it get simplifier during canonicalisation.
"""
x = T.dscalar()
a = T.abs_(x)
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.mode.get_mode('FAST_RUN').excluding(
"local_elemwise_fusion")
else:
mode = theano.compile.mode.get_default_mode().excluding(
"local_elemwise_fusion")
f = theano.function([x], [(4 * x) / abs(2 * x)], mode=mode)
print(f.maker.fgraph.toposort())
print()
f(.1)
f(-1)
# some stabilization optimization make the output be finite instead of nan
# debug_mode will raise an error when he see nan
if not isinstance(mode, theano.compile.debugmode.DebugMode):
assert numpy.isfinite(f(0))
assert len(f.maker.fgraph.toposort()) == 2
assert f.maker.fgraph.toposort()[0].op == T.sgn
f = theano.function([x], [(4 * x) / abs(x / 2)], mode=mode)
print(f.maker.fgraph.toposort())
print()
f(.1)
f(-1)
# some stabilization optimization make the output be finite instead of nan
# debug_mode will raise an error when he see nan
if not isinstance(mode, theano.compile.debugmode.DebugMode):
assert numpy.isfinite(f(0))
assert len(f.maker.fgraph.toposort()) == 2
assert f.maker.fgraph.toposort()[0].op == T.sgn
评论列表
文章目录