提交 adaf2584 authored 作者: Frederic Bastien's avatar Frederic Bastien

pep8

上级 45aade79
......@@ -16,14 +16,18 @@ from theano.tensor.nnet.sigm import (
class T_sigmoid(unittest.TestCase):
def setUp(self):
utt.seed_rng()
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):
def setUp(self):
utt.seed_rng()
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):
......@@ -54,47 +58,47 @@ class T_sigmoid_opts(unittest.TestCase):
data = numpy.random.rand(54).astype(config.floatX)
# 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)
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)
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)
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)
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
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]
f(data)
f = theano.function([x], T.fill(x,1.0) / (2+T.exp(-x)), mode=m)
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)
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)
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
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,
theano.tensor.inplace.neg_inplace]
f(data)
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,
theano.tensor.inplace.neg_inplace]
f(data)
f = theano.function([x], T.fill(x,-1.0) / (2+T.exp(-x)), mode=m)
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)
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)
......@@ -103,27 +107,33 @@ class T_sigmoid_opts(unittest.TestCase):
# (-1)(exp(x)) / (1+exp(x))(1+exp(-x))
# = (-1)/(1+exp(-x)) * exp(x)/(1+exp(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)) /
((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.1)*T.exp(x)) / ((1+T.exp(x))*(1+T.exp(-x))), mode=m)
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)
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)
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)
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)
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)
......@@ -136,11 +146,12 @@ class T_sigmoid_opts(unittest.TestCase):
x = T.fmatrix()
# tests exp_over_1_plus_exp
f = theano.function([x], 1 - T.exp(x)/(1+T.exp(x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] == [tensor.neg, sigmoid_inplace]
f = theano.function([x], 1 - T.exp(x) / (1 + T.exp(x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] == [
tensor.neg, sigmoid_inplace]
# 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)
assert [node.op for node in f.maker.env.toposort()] == [tensor.neg,
sigmoid_inplace]
......@@ -167,8 +178,8 @@ class T_sigmoid_opts(unittest.TestCase):
f = theano.function(
[x, y],
(sigmoid(x) * sigmoid(-y) * -tensor.exp(-x) * tensor.exp(x * y) *
tensor.exp(y)),
(sigmoid(x) * sigmoid(-y) * -tensor.exp(-x) *
tensor.exp(x * y) * tensor.exp(y)),
mode=m)
match(f, [sigmoid, tensor.mul, tensor.neg, tensor.exp, sigmoid,
tensor.mul, tensor.neg])
......@@ -182,6 +193,7 @@ class T_sigmoid_opts(unittest.TestCase):
"""
x, y, z, t = tensor.vectors('x', 'y', 'z', 't')
exp = tensor.exp
def ok(expr1, expr2):
trees = [parse_mul_tree(e) for e in (expr1, expr2)]
perform_sigm_times_exp(trees[0])
......@@ -216,46 +228,52 @@ class T_sigmoid_opts(unittest.TestCase):
class T_softplus_opts(unittest.TestCase):
def setUp(self):
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:
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
utt.seed_rng()
def test_logsigm_to_softplus(self):
x = T.vector()
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()
assert len(topo)==3
assert len(topo) == 3
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)
f(numpy.random.rand(54).astype(config.floatX))
def test_log1msigm_to_softplus(self):
x = T.vector()
out = T.log(1-sigmoid(x))
f = theano.function([x],out,mode=self.m)
out = T.log(1 - sigmoid(x))
f = theano.function([x], out, mode=self.m)
topo = f.maker.env.toposort()
assert len(topo)==2
assert isinstance(topo[0].op.scalar_op, theano.tensor.nnet.sigm.ScalarSoftplus)
assert len(topo) == 2
assert isinstance(topo[0].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[1].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54).astype(config.floatX))
def test_log1pexp_to_softplus(self):
m = theano.config.mode
if m == 'FAST_COMPILE':
m = 'FAST_RUN'
x = T.vector()
out = T.log(1+T.exp(x))
f = theano.function([x],out,mode=self.m)
out = T.log(1 + T.exp(x))
f = theano.function([x], out, mode=self.m)
topo = f.maker.env.toposort()
assert len(topo)==1
assert isinstance(topo[0].op.scalar_op,theano.tensor.nnet.sigm.ScalarSoftplus)
assert len(topo) == 1
assert isinstance(topo[0].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus)
f(numpy.random.rand(54).astype(config.floatX))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论