提交 7624f7d6 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merge pull request #303 from nouiz/test_sigm_opt

Test sigm opt
...@@ -16,14 +16,18 @@ from theano.tensor.nnet.sigm import ( ...@@ -16,14 +16,18 @@ from theano.tensor.nnet.sigm import (
class T_sigmoid(unittest.TestCase): class T_sigmoid(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
def test_elemwise(self): def test_elemwise(self):
utt.verify_grad(sigmoid, [numpy.random.rand(3,4)]) utt.verify_grad(sigmoid, [numpy.random.rand(3, 4)])
class T_softplus(unittest.TestCase): class T_softplus(unittest.TestCase):
def setUp(self): def setUp(self):
utt.seed_rng() utt.seed_rng()
def test_elemwise(self): def test_elemwise(self):
utt.verify_grad(softplus, [numpy.random.rand(3,4)]) utt.verify_grad(softplus, [numpy.random.rand(3, 4)])
class T_sigmoid_opts(unittest.TestCase): class T_sigmoid_opts(unittest.TestCase):
...@@ -50,31 +54,89 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -50,31 +54,89 @@ class T_sigmoid_opts(unittest.TestCase):
def test_exp_over_1_plus_exp(self): def test_exp_over_1_plus_exp(self):
m = self.get_mode(excluding=['local_elemwise_fusion']) m = self.get_mode(excluding=['local_elemwise_fusion'])
x = T.dvector() x = T.vector()
data = numpy.random.rand(54).astype(config.floatX)
# tests exp_over_1_plus_exp # tests exp_over_1_plus_exp
f = theano.function([x], T.exp(x)/(1+T.exp(x)), mode=m) f = theano.function([x], T.exp(x) / (1 + T.exp(x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid] assert [node.op for node in f.maker.env.toposort()] == [sigmoid]
f(data)
f = theano.function([x], T.exp(x) / (2 + T.exp(x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid]
f(data)
f = theano.function([x], T.exp(x) / (1 - T.exp(x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid]
f(data)
f = theano.function([x], T.exp(x + 1) / (1 + T.exp(x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid]
f(data)
# tests inv_1_plus_exp # tests inv_1_plus_exp
f = theano.function([x], T.fill(x,1.0) / (1+T.exp(-x)), mode=m) f = theano.function([x], T.fill(x, 1.0) / (1 + T.exp(-x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid] assert [node.op for node in f.maker.env.toposort()] == [sigmoid]
f(data)
f = theano.function([x], T.fill(x, 1.0) / (2 + T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid]
f(data)
f = theano.function([x], T.fill(x, 1.0) / (1 - T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid]
f(data)
f = theano.function([x], T.fill(x, 1.1) / (1 + T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid]
f(data)
# tests inv_1_plus_exp with neg # tests inv_1_plus_exp with neg
f = theano.function([x], T.fill(x,-1.0) / (1+T.exp(-x)), mode=m) f = theano.function([x], T.fill(x, -1.0) / (1 + T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid, assert [node.op for node in f.maker.env.toposort()] == [sigmoid,
theano.tensor.inplace.neg_inplace] theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], T.fill(x, -1.0) / (1 - T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], T.fill(x, -1.0) / (2 + T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], T.fill(x, -1.1) / (1 + T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
theano.tensor.inplace.neg_inplace]
f(data)
# tests double inv_1_plus_exp with neg # tests double inv_1_plus_exp with neg
# (-1)(exp(x)) / (1+exp(x))(1+exp(-x)) # (-1)(exp(x)) / (1+exp(x))(1+exp(-x))
# = (-1)/(1+exp(-x)) * exp(x)/(1+exp(x)) # = (-1)/(1+exp(-x)) * exp(x)/(1+exp(x))
# = - (sigm(x) * sigm(x)) # = - (sigm(x) * sigm(x))
f = theano.function([x], (T.fill(x,-1.0)*T.exp(x)) / ((1+T.exp(x))*(1+T.exp(-x))), mode=m) f = theano.function([x], (T.fill(x, -1.0) * T.exp(x)) /
theano.printing.debugprint(f) ((1 + T.exp(x)) * (1 + T.exp(-x))), mode=m)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid, assert [node.op for node in f.maker.env.toposort()] == [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace] T.mul, theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], (T.fill(x, -1.1) * T.exp(x)) /
((1 + T.exp(x)) * (1 + T.exp(-x))), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], (T.fill(x, -1.0) * T.exp(x)) /
((2 + T.exp(x)) * (1 + T.exp(-x))), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], (T.fill(x, -1.0) * T.exp(x)) /
((1 + T.exp(x)) * (2 + T.exp(-x))), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], (T.fill(x, -1.0) * T.exp(x)) /
((1 + T.exp(x)) * (1 + T.exp(x))), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], (T.fill(x, -1.0) * T.exp(x)) /
((1 + T.exp(x)) * (2 + T.exp(-x))), mode=m)
assert [node.op for node in f.maker.env.toposort()] != [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace]
f(data)
def test_1msigmoid(self): def test_1msigmoid(self):
if not register_local_1msigmoid: if not register_local_1msigmoid:
...@@ -84,13 +146,12 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -84,13 +146,12 @@ class T_sigmoid_opts(unittest.TestCase):
x = T.fmatrix() x = T.fmatrix()
# tests exp_over_1_plus_exp # tests exp_over_1_plus_exp
f = theano.function([x], 1 - T.exp(x)/(1+T.exp(x)), mode=m) f = theano.function([x], 1 - T.exp(x) / (1 + T.exp(x)), mode=m)
theano.printing.debugprint(f) assert [node.op for node in f.maker.env.toposort()] == [
assert [node.op for node in f.maker.env.toposort()] == [tensor.neg, sigmoid_inplace] tensor.neg, sigmoid_inplace]
# tests inv_1_plus_exp # tests inv_1_plus_exp
f = theano.function([x], 1 - T.fill(x,1.0) / (1+T.exp(-x)), mode=m) f = theano.function([x], 1 - T.fill(x, 1.0) / (1 + T.exp(-x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [tensor.neg, assert [node.op for node in f.maker.env.toposort()] == [tensor.neg,
sigmoid_inplace] sigmoid_inplace]
...@@ -107,23 +168,19 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -107,23 +168,19 @@ class T_sigmoid_opts(unittest.TestCase):
x, y = tensor.vectors('x', 'y') x, y = tensor.vectors('x', 'y')
f = theano.function([x], sigmoid(-x) * tensor.exp(x), mode=m) f = theano.function([x], sigmoid(-x) * tensor.exp(x), mode=m)
theano.printing.debugprint(f)
match(f, [sigmoid]) match(f, [sigmoid])
f = theano.function([x], sigmoid(x) * tensor.exp(-x), mode=m) f = theano.function([x], sigmoid(x) * tensor.exp(-x), mode=m)
theano.printing.debugprint(f)
match(f, [tensor.neg, sigmoid]) match(f, [tensor.neg, sigmoid])
f = theano.function([x], -(-(-(sigmoid(x)))) * tensor.exp(-x), mode=m) f = theano.function([x], -(-(-(sigmoid(x)))) * tensor.exp(-x), mode=m)
theano.printing.debugprint(f)
match(f, [tensor.neg, sigmoid, tensor.neg]) match(f, [tensor.neg, sigmoid, tensor.neg])
f = theano.function( f = theano.function(
[x, y], [x, y],
(sigmoid(x) * sigmoid(-y) * -tensor.exp(-x) * tensor.exp(x * y) * (sigmoid(x) * sigmoid(-y) * -tensor.exp(-x) *
tensor.exp(y)), tensor.exp(x * y) * tensor.exp(y)),
mode=m) mode=m)
theano.printing.debugprint(f)
match(f, [sigmoid, tensor.mul, tensor.neg, tensor.exp, sigmoid, match(f, [sigmoid, tensor.mul, tensor.neg, tensor.exp, sigmoid,
tensor.mul, tensor.neg]) tensor.mul, tensor.neg])
...@@ -136,6 +193,7 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -136,6 +193,7 @@ class T_sigmoid_opts(unittest.TestCase):
""" """
x, y, z, t = tensor.vectors('x', 'y', 'z', 't') x, y, z, t = tensor.vectors('x', 'y', 'z', 't')
exp = tensor.exp exp = tensor.exp
def ok(expr1, expr2): def ok(expr1, expr2):
trees = [parse_mul_tree(e) for e in (expr1, expr2)] trees = [parse_mul_tree(e) for e in (expr1, expr2)]
perform_sigm_times_exp(trees[0]) perform_sigm_times_exp(trees[0])
...@@ -170,32 +228,36 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -170,32 +228,36 @@ class T_sigmoid_opts(unittest.TestCase):
class T_softplus_opts(unittest.TestCase): class T_softplus_opts(unittest.TestCase):
def setUp(self): def setUp(self):
if theano.config.mode == 'FAST_COMPILE': if theano.config.mode == 'FAST_COMPILE':
m = theano.compile.mode.get_mode('FAST_RUN').excluding('local_elemwise_fusion') m = theano.compile.mode.get_mode('FAST_RUN').excluding(
'local_elemwise_fusion')
else: else:
m = theano.compile.mode.get_default_mode().excluding('local_elemwise_fusion') m = theano.compile.mode.get_default_mode().excluding(
'local_elemwise_fusion')
self.m = m self.m = m
utt.seed_rng() utt.seed_rng()
def test_logsigm_to_softplus(self): def test_logsigm_to_softplus(self):
x = T.vector() x = T.vector()
out = T.log(sigmoid(x)) out = T.log(sigmoid(x))
f = theano.function([x],out,mode=self.m) f = theano.function([x], out, mode=self.m)
topo = f.maker.env.toposort() topo = f.maker.env.toposort()
print topo assert len(topo) == 3
assert len(topo)==3
assert isinstance(topo[0].op.scalar_op, theano.scalar.Neg) assert isinstance(topo[0].op.scalar_op, theano.scalar.Neg)
assert isinstance(topo[1].op.scalar_op, theano.tensor.nnet.sigm.ScalarSoftplus) assert isinstance(topo[1].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg) assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54).astype(config.floatX)) f(numpy.random.rand(54).astype(config.floatX))
def test_log1msigm_to_softplus(self): def test_log1msigm_to_softplus(self):
x = T.vector() x = T.vector()
out = T.log(1-sigmoid(x)) out = T.log(1 - sigmoid(x))
f = theano.function([x],out,mode=self.m) f = theano.function([x], out, mode=self.m)
topo = f.maker.env.toposort() topo = f.maker.env.toposort()
assert len(topo)==2 assert len(topo) == 2
assert isinstance(topo[0].op.scalar_op, theano.tensor.nnet.sigm.ScalarSoftplus) assert isinstance(topo[0].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[1].op.scalar_op, theano.scalar.Neg) assert isinstance(topo[1].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54).astype(config.floatX)) f(numpy.random.rand(54).astype(config.floatX))
...@@ -206,11 +268,12 @@ class T_softplus_opts(unittest.TestCase): ...@@ -206,11 +268,12 @@ class T_softplus_opts(unittest.TestCase):
x = T.vector() x = T.vector()
out = T.log(1+T.exp(x)) out = T.log(1 + T.exp(x))
f = theano.function([x],out,mode=self.m) f = theano.function([x], out, mode=self.m)
topo = f.maker.env.toposort() topo = f.maker.env.toposort()
assert len(topo)==1 assert len(topo) == 1
assert isinstance(topo[0].op.scalar_op,theano.tensor.nnet.sigm.ScalarSoftplus) assert isinstance(topo[0].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus)
f(numpy.random.rand(54).astype(config.floatX)) f(numpy.random.rand(54).astype(config.floatX))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论