提交 7561c1c2 authored 作者: Vincent Michalski's avatar Vincent Michalski

added some more tests for nnet

上级 93b50c70
...@@ -143,9 +143,10 @@ class T_SoftmaxWithBias(utt.InferShapeTester): ...@@ -143,9 +143,10 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
a = theano.shared( a = theano.shared(
numpy.random.randn(3).astype(config.floatX)) numpy.random.randn(3).astype(config.floatX))
b = theano.shared(numpy.float32(numpy.random.randn())) b = theano.shared(numpy.float32(numpy.random.randn()))
sm = T.softmax(a + b) sm = T.nnet.softmax(a + b)
f = theano.function([], sm) f = theano.function([], sm)
self.assertTrue(hasattr(f.maker.fgraph.outputs[0].tag, 'trace')) self.assertTrue(hasattr(f.maker.fgraph.outputs[0].tag, 'trace'))
print('f.maker.fgraph.outputs[0]: {0}'.format(f.maker.fgraph.outputs[0], ))
def test_infer_shape(self): def test_infer_shape(self):
admat = matrix() admat = matrix()
...@@ -209,11 +210,11 @@ class T_LogSoftmax(utt.InferShapeTester): ...@@ -209,11 +210,11 @@ class T_LogSoftmax(utt.InferShapeTester):
# numerically stable log-softmax with crossentropy # numerically stable log-softmax with crossentropy
logsm = tensor.nnet.logsoftmax(x) logsm = tensor.nnet.logsoftmax(x)
sm2 = tensor.exp(logsm) # just used to show equivalence with sm sm2 = tensor.exp(logsm) # just used to show equivalence with sm
cm2 = -tensor.sum(y*logsm, axis=1) cm2 = -tensor.sum(y * logsm, axis=1)
grad = tensor.grad(cm2.mean(), x) grad = tensor.grad(cm2.mean(), x)
# create some inputs into a softmax that are large and labels # create some inputs into a softmax that are large and labels
a = numpy.exp(10*numpy.random.rand(5, 10).astype(theano.config.floatX)) a = numpy.exp(10 * numpy.random.rand(5, 10).astype(theano.config.floatX))
# create some one-hot coded labels # create some one-hot coded labels
b = numpy.eye(5, 10).astype(theano.config.floatX) b = numpy.eye(5, 10).astype(theano.config.floatX)
...@@ -266,7 +267,7 @@ class T_LogSoftmax(utt.InferShapeTester): ...@@ -266,7 +267,7 @@ class T_LogSoftmax(utt.InferShapeTester):
m.check_isfinite = False m.check_isfinite = False
# some inputs that are large to make the gradient explode in the non # some inputs that are large to make the gradient explode in the non
# optimized case # optimized case
a = numpy.exp(10*numpy.random.rand(5, 10).astype(theano.config.floatX)) a = numpy.exp(10 * numpy.random.rand(5, 10).astype(theano.config.floatX))
def myfunc(x): def myfunc(x):
sm = tensor.nnet.softmax(x) sm = tensor.nnet.softmax(x)
...@@ -1426,6 +1427,9 @@ class Test_softmax_opt: ...@@ -1426,6 +1427,9 @@ class Test_softmax_opt:
# test that function contains softmax and no div. # test that function contains softmax and no div.
f = theano.function([c], p_y, mode=self.mode) f = theano.function([c], p_y, mode=self.mode)
assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace')
f_ops = [n.op for n in f.maker.fgraph.toposort()] f_ops = [n.op for n in f.maker.fgraph.toposort()]
# print '--- f =' # print '--- f ='
# printing.debugprint(f) # printing.debugprint(f)
...@@ -1440,6 +1444,9 @@ class Test_softmax_opt: ...@@ -1440,6 +1444,9 @@ class Test_softmax_opt:
# test that function contains softmax and no div. # test that function contains softmax and no div.
f = theano.function([c], p_y, mode=self.mode) f = theano.function([c], p_y, mode=self.mode)
assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace')
f_ops = [n.op for n in f.maker.fgraph.toposort()] f_ops = [n.op for n in f.maker.fgraph.toposort()]
# print '--- f =' # print '--- f ='
# printing.debugprint(f) # printing.debugprint(f)
...@@ -1458,6 +1465,7 @@ class Test_softmax_opt: ...@@ -1458,6 +1465,7 @@ class Test_softmax_opt:
config.warn.sum_div_dimshuffle_bug = False config.warn.sum_div_dimshuffle_bug = False
try: try:
g = theano.function([c, w], T.grad((p_y * w).sum(), c)) g = theano.function([c, w], T.grad((p_y * w).sum(), c))
hasattr(g.maker.fgraph.outputs[0].tag, 'trace')
finally: finally:
config.warn.sum_div_dimshuffle_bug = backup config.warn.sum_div_dimshuffle_bug = backup
g_ops = [n.op for n in g.maker.fgraph.toposort()] g_ops = [n.op for n in g.maker.fgraph.toposort()]
...@@ -1485,6 +1493,7 @@ class Test_softmax_opt: ...@@ -1485,6 +1493,7 @@ class Test_softmax_opt:
config.warn.sum_div_dimshuffle_bug = False config.warn.sum_div_dimshuffle_bug = False
try: try:
g = theano.function([c], T.grad(p_y.sum(), c)) g = theano.function([c], T.grad(p_y.sum(), c))
hasattr(g.maker.fgraph.outputs[0].tag, 'trace')
finally: finally:
config.warn.sum_div_dimshuffle_bug = backup config.warn.sum_div_dimshuffle_bug = backup
# printing.debugprint(g) # printing.debugprint(g)
...@@ -1497,6 +1506,7 @@ class Test_softmax_opt: ...@@ -1497,6 +1506,7 @@ class Test_softmax_opt:
# test that function contains softmax and no div. # test that function contains softmax and no div.
f = theano.function([c], p_y) f = theano.function([c], p_y)
hasattr(f.maker.fgraph.outputs[0].tag, 'trace')
# printing.debugprint(f) # printing.debugprint(f)
# test that function contains softmax and no div. # test that function contains softmax and no div.
...@@ -1504,6 +1514,7 @@ class Test_softmax_opt: ...@@ -1504,6 +1514,7 @@ class Test_softmax_opt:
config.warn.sum_div_dimshuffle_bug = False config.warn.sum_div_dimshuffle_bug = False
try: try:
g = theano.function([c], T.grad(p_y.sum(), c)) g = theano.function([c], T.grad(p_y.sum(), c))
hasattr(g.maker.fgraph.outputs[0].tag, 'trace')
finally: finally:
config.warn.sum_div_dimshuffle_bug = backup config.warn.sum_div_dimshuffle_bug = backup
# printing.debugprint(g) # printing.debugprint(g)
...@@ -1543,6 +1554,7 @@ def test_stabilize_log_softmax(): ...@@ -1543,6 +1554,7 @@ def test_stabilize_log_softmax():
z = theano.tensor.log(y) z = theano.tensor.log(y)
f = theano.function([x], z, mode=mode) f = theano.function([x], z, mode=mode)
assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace')
# check that the softmax has been optimized out # check that the softmax has been optimized out
for node in f.maker.fgraph.toposort(): for node in f.maker.fgraph.toposort():
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论