def test_log_add():
m = theano.config.mode
if m == 'FAST_COMPILE':
m = 'FAST_RUN'
m = compile.mode.get_mode(m)
m = m.excluding('fusion')
m = copy.copy(m)
# No need to put them back as we have a new object
m.check_isfinite = False
# check some basic cases
x = dvector()
y = dvector()
f = function([x, y], T.log(T.exp(x) + T.exp(y)), mode=m)
f([10000], [10000]) # causes overflow if handled incorrectly
assert numpy.isfinite(f([10000], [10000]))
utt.assert_allclose(f([10000], [10000]), 10000 + numpy.log1p(1))
# test that it give the same result when it don't overflow
f([10], [10]) # don't causes overflow
utt.assert_allclose(f([10], [10]), 10 + numpy.log1p(1))
# test that it also works with more than two args, (this currently fails)
x = dvector()
y = dvector()
f = function([x, y], T.log(T.exp(x) + T.exp(y) + T.exp(x - y) + T.exp(
x + y)), mode=m)
try:
f([10000], [10000]) # causes overflow if handled incorrectly
utt.assert_allclose(f([10000], [10000]), 20000)
except utt.WrongValue:
raise SkipTest("log(add(exp)) is not stabilized when adding "
"more than 2 elements, see #623")
# TODO: test that the optimization works in the presence of broadcasting.
# TODO: (write and) test that the optimization works with Sum in addition to working with Add.
评论列表
文章目录