提交 79e94fe4 authored 作者: James Bergstra's avatar James Bergstra

adding a test of nested scalar Ops with support code for Composite

上级 4cdd1632
...@@ -1117,6 +1117,52 @@ class test_fusion(unittest.TestCase): ...@@ -1117,6 +1117,52 @@ class test_fusion(unittest.TestCase):
# cases[id]=None #to remove g, that link to out that link to the ndarray! # cases[id]=None #to remove g, that link to out that link to the ndarray!
#g.owner.inputs[0] is out... make owner a weakref? #g.owner.inputs[0] is out... make owner a weakref?
class TestCompositeCodegen(unittest.TestCase):
"""
Test The Composite Ops code generation in a case where there is multiple
scalar ops with support code.
"""
def setUp(self):
class TimesN(theano.scalar.basic.UnaryScalarOp):
def __init__(self, n, *args, **kwargs):
self.n = n
theano.scalar.basic.UnaryScalarOp.__init__(self, *args, **kwargs)
def impl(self, x):
return x * self.n
def c_support_code_apply(self, node, nodename):
n = str(self.n)
return """
float %(nodename)s_timesn(float x) { return x * %(n)s; }
""" % locals()
def c_code(self, node, name, (x, ), (z, ), sub):
return "%(z)s = %(name)s_timesn(%(x)s);" % locals()
upgrade_to_float = theano.scalar.basic.upgrade_to_float
self.scal_times_2 = TimesN(2, upgrade_to_float, name='times_2')
self.times_2 = theano.tensor.elemwise.Elemwise(
self.scal_times_2,
name='times_2')
self.scal_times_3 = TimesN(3, upgrade_to_float, name='times_3')
self.times_3 = theano.tensor.elemwise.Elemwise(
self.scal_times_3,
name='times_3')
self.x = fvector()
def test_nested_composite(self):
y = self.times_2(self.x)
z = self.times_3(y)
f = function([self.x], z)
assert len(f.maker.env.toposort()) == 1
fval = f([1, 2, 3])
assert numpy.all(fval == [6, 12, 18])
def test_log1p(): def test_log1p():
m = theano.config.mode m = theano.config.mode
if m == 'FAST_COMPILE': if m == 'FAST_COMPILE':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论