Removed dependency between tensor/basic.py and unittest_tools (which was getting

removed for easy_install). Unittests should now call unittest_tools.verify_grad instead of tensor.basic.verify_grad. The unittest version is simply a wrapper function which takes care of first seeding the random number gen. Updated the documentation accordingly
上级 867931a9
...@@ -393,6 +393,12 @@ Here is an example showing how to use verify_grad: ...@@ -393,6 +393,12 @@ Here is an example showing how to use verify_grad:
>>> # ... >>> # ...
>>> tensor.verify_grad(Flatten(), [a_val]) >>> tensor.verify_grad(Flatten(), [a_val])
.. note::
Although ``verify_grad`` is defined in ``theano.tensor.basic``, unittests
should use the version of ``verify_grad`` defined in ``theano.tests.unittest_tools``.
This is simply a wrapper function which takes care of seeding the random
number generator appropriately before calling ``theano.tensor.basic.verify_grad``
makeTester and makeBroadcastTester makeTester and makeBroadcastTester
================================== ==================================
......
...@@ -11,7 +11,7 @@ from theano import gof ...@@ -11,7 +11,7 @@ from theano import gof
from theano.sparse.basic import _is_dense, _is_sparse, _is_dense_variable, _is_sparse_variable from theano.sparse.basic import _is_dense, _is_sparse, _is_dense_variable, _is_sparse_variable
from theano.sparse.basic import _mtypes, _mtype_to_str from theano.sparse.basic import _mtypes, _mtype_to_str
from theano.tests import unittest_tools from theano.tests import unittest_tools as utt
def eval_outputs(outputs): def eval_outputs(outputs):
...@@ -19,7 +19,7 @@ def eval_outputs(outputs): ...@@ -19,7 +19,7 @@ def eval_outputs(outputs):
class T_transpose(unittest.TestCase): class T_transpose(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test_transpose_csc(self): def test_transpose_csc(self):
sp = sparse.csc_matrix(sparse.eye(5,3)) sp = sparse.csc_matrix(sparse.eye(5,3))
...@@ -126,7 +126,7 @@ class T_Add(unittest.TestCase): ...@@ -126,7 +126,7 @@ class T_Add(unittest.TestCase):
class T_conversion(unittest.TestCase): class T_conversion(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
a = tensor.as_tensor_variable(numpy.random.rand(5)) a = tensor.as_tensor_variable(numpy.random.rand(5))
...@@ -157,7 +157,7 @@ class T_conversion(unittest.TestCase): ...@@ -157,7 +157,7 @@ class T_conversion(unittest.TestCase):
import scipy.sparse as sp import scipy.sparse as sp
class test_structureddot(unittest.TestCase): class test_structureddot(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test_structuredot(self): def test_structuredot(self):
bsize = 2 bsize = 2
...@@ -193,7 +193,7 @@ class test_structureddot(unittest.TestCase): ...@@ -193,7 +193,7 @@ class test_structureddot(unittest.TestCase):
assert _is_dense(c) assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(outvals == c)
tensor.verify_grad(buildgraphCSC, [kernvals,imvals]) utt.verify_grad(buildgraphCSC, [kernvals,imvals])
## ##
# Test compressed-sparse row matrices ### # Test compressed-sparse row matrices ###
...@@ -215,7 +215,7 @@ class test_structureddot(unittest.TestCase): ...@@ -215,7 +215,7 @@ class test_structureddot(unittest.TestCase):
assert _is_dense(c) assert _is_dense(c)
assert numpy.all(outvals == c) assert numpy.all(outvals == c)
tensor.verify_grad( buildgraphCSR, [kernvals,imvals]) utt.verify_grad( buildgraphCSR, [kernvals,imvals])
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -20,7 +20,6 @@ from ..gof.python25 import partial ...@@ -20,7 +20,6 @@ from ..gof.python25 import partial
from .. import compile, printing from .. import compile, printing
from ..printing import pprint, Print from ..printing import pprint, Print
from ..tests import unittest_tools
### set up the external interface ### set up the external interface
from elemwise import Elemwise, DimShuffle, CAReduce, Sum from elemwise import Elemwise, DimShuffle, CAReduce, Sum
......
...@@ -5,86 +5,86 @@ from theano import tensor as T ...@@ -5,86 +5,86 @@ from theano import tensor as T
from theano import gof from theano import gof
import test_basic as TT import test_basic as TT
import numpy import numpy
from theano.tests import unittest_tools from theano.tests import unittest_tools as utt
from theano.tensor.nnet import * from theano.tensor.nnet import *
class T_sigmoid(unittest.TestCase): class T_sigmoid(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test_elemwise(self): def test_elemwise(self):
TT.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):
unittest_tools.seed_rng() utt.seed_rng()
def test_elemwise(self): def test_elemwise(self):
TT.verify_grad(softplus, [numpy.random.rand(3,4)]) utt.verify_grad(softplus, [numpy.random.rand(3,4)])
class T_Softmax(unittest.TestCase): class T_Softmax(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
def f(a): def f(a):
return softmax(a)[:,0] return softmax(a)[:,0]
TT.verify_grad(f, [numpy.random.rand(3,4)]) utt.verify_grad(f, [numpy.random.rand(3,4)])
def test1(self): def test1(self):
def f(a): def f(a):
return softmax(a)[:,1] return softmax(a)[:,1]
TT.verify_grad(f, [numpy.random.rand(3,4)]) utt.verify_grad(f, [numpy.random.rand(3,4)])
def test2(self): def test2(self):
def f(a): def f(a):
return softmax(a)[:,2] return softmax(a)[:,2]
TT.verify_grad(f, [numpy.random.rand(3,4)]) utt.verify_grad(f, [numpy.random.rand(3,4)])
def test3(self): def test3(self):
def f(a): def f(a):
return softmax(a)[:,3] return softmax(a)[:,3]
TT.verify_grad(f, [numpy.random.rand(3,4)]) utt.verify_grad(f, [numpy.random.rand(3,4)])
class T_SoftmaxWithBias(unittest.TestCase): class T_SoftmaxWithBias(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,0] return softmax_with_bias(a, b)[:,0]
TT.verify_grad(f, [numpy.random.rand(3,4), utt.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test1(self): def test1(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,1] return softmax_with_bias(a, b)[:,1]
TT.verify_grad(f, [numpy.random.rand(3,4), utt.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test2(self): def test2(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,2] return softmax_with_bias(a, b)[:,2]
TT.verify_grad(f, [numpy.random.rand(3,4), utt.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test3(self): def test3(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,3] return softmax_with_bias(a, b)[:,3]
TT.verify_grad(f, [numpy.random.rand(3,4), utt.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
class T_CrossentropySoftmax1Hot(unittest.TestCase): class T_CrossentropySoftmax1Hot(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
y_idx = [0,1,3] y_idx = [0,1,3]
def f(a, b): def f(a, b):
return crossentropy_softmax_1hot_with_bias(a, b, y_idx)[0] return crossentropy_softmax_1hot_with_bias(a, b, y_idx)[0]
TT.verify_grad(f, [numpy.random.rand(3,4), utt.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test1(self): def test1(self):
y_idx = [0,1,3] y_idx = [0,1,3]
def f(a): def f(a):
return crossentropy_softmax_1hot(a, y_idx)[0] return crossentropy_softmax_1hot(a, y_idx)[0]
TT.verify_grad(f, [numpy.random.rand(3,4)]) utt.verify_grad(f, [numpy.random.rand(3,4)])
class T_prepend(unittest.TestCase): class T_prepend(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
"""basic functionality""" """basic functionality"""
x=tensor.matrix('x') x=tensor.matrix('x')
...@@ -110,7 +110,7 @@ class T_prepend(unittest.TestCase): ...@@ -110,7 +110,7 @@ class T_prepend(unittest.TestCase):
class T_solve(unittest.TestCase): class T_solve(unittest.TestCase):
def setUp(self): def setUp(self):
self.rng = numpy.random.RandomState(unittest_tools.fetch_seed(666)) self.rng = numpy.random.RandomState(utt.fetch_seed(666))
def test0(self): def test0(self):
A=self.rng.randn(5,5) A=self.rng.randn(5,5)
......
...@@ -8,11 +8,11 @@ import test_basic as TT ...@@ -8,11 +8,11 @@ import test_basic as TT
import random import random
import numpy.random import numpy.random
from theano.tests import unittest_tools from theano.tests import unittest_tools as utt
class T_XlogX(unittest.TestCase): class T_XlogX(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
x = as_tensor_variable([1, 0]) x = as_tensor_variable([1, 0])
...@@ -23,7 +23,7 @@ class T_XlogX(unittest.TestCase): ...@@ -23,7 +23,7 @@ class T_XlogX(unittest.TestCase):
# class Dummy(object): # class Dummy(object):
# def make_node(self, a): # def make_node(self, a):
# return [xlogx(a)[:,2]] # return [xlogx(a)[:,2]]
TT.verify_grad(xlogx, [numpy.random.rand(3,4)]) utt.verify_grad(xlogx, [numpy.random.rand(3,4)])
if __name__ == '__main__': if __name__ == '__main__':
......
import unittest import unittest
import numpy import numpy
import theano.tensor as T
import os, sys import os, sys
...@@ -40,3 +41,14 @@ def seed_rng(pseed=None): ...@@ -40,3 +41,14 @@ def seed_rng(pseed=None):
'instead of seed %i given as parameter' % (seed, pseed) 'instead of seed %i given as parameter' % (seed, pseed)
numpy.random.seed(seed) numpy.random.seed(seed)
return seed return seed
def verify_grad(op, pt, n_tests=2, rng=None, eps=1.0e-7, tol=0.0001):
"""
Wrapper for tensor/basic.py:verify_grad
Takes care of seeding the random number generator if None is given
"""
if rng is None:
seed_rng()
rng = numpy.random
T.verify_grad(op, pt, n_tests, rng, eps, tol)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论