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
......
...@@ -16,10 +16,10 @@ from theano import gof ...@@ -16,10 +16,10 @@ from theano import gof
from theano.tensor.elemwise import DimShuffle from theano.tensor.elemwise import DimShuffle
from theano.compile.mode import default_mode from theano.compile.mode import default_mode
from theano import function from theano import function
from theano.tests import unittest_tools from theano.tests import unittest_tools as utt
### seed random number generator so that unittests are deterministic ### ### seed random number generator so that unittests are deterministic ###
unittest_tools.seed_rng() utt.seed_rng()
def inplace_func(inputs, outputs, mode=default_mode): def inplace_func(inputs, outputs, mode=default_mode):
return function(inputs, outputs, mode=mode, accept_inplace=True) return function(inputs, outputs, mode=mode, accept_inplace=True)
...@@ -157,7 +157,7 @@ def makeTester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_r ...@@ -157,7 +157,7 @@ def makeTester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_r
inputs = [copy(input) for input in inputs] inputs = [copy(input) for input in inputs]
inputrs = [value(input) for input in inputs] inputrs = [value(input) for input in inputs]
try: try:
verify_grad(self.op, inputs) utt.verify_grad(self.op, inputs)
except: except:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while computing the gradient on the following inputs: %s" \ err_msg = "Test %s::%s: Error occurred while computing the gradient on the following inputs: %s" \
...@@ -599,7 +599,7 @@ class T_Cast(unittest.TestCase): ...@@ -599,7 +599,7 @@ class T_Cast(unittest.TestCase):
class T_max_and_argmax(unittest.TestCase): class T_max_and_argmax(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
MaxAndArgmax.debug = 0 MaxAndArgmax.debug = 0
def test0(self): def test0(self):
...@@ -670,7 +670,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -670,7 +670,7 @@ class T_max_and_argmax(unittest.TestCase):
class T_subtensor(unittest.TestCase): class T_subtensor(unittest.TestCase):
def setUp(self): def setUp(self):
Subtensor.debug = False Subtensor.debug = False
unittest_tools.seed_rng() utt.seed_rng()
def test0_err_invalid(self): def test0_err_invalid(self):
#it is impossible to retrieve a view of a 0-d tensor #it is impossible to retrieve a view of a 0-d tensor
...@@ -941,7 +941,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -941,7 +941,7 @@ class T_Join_and_Split(unittest.TestCase):
want = numpy.array([[1, 2, 3, 7], [4, 5, 6, 8]], dtype='float32') want = numpy.array([[1, 2, 3, 7], [4, 5, 6, 8]], dtype='float32')
self.failUnless((eval_outputs([s]) == want).all()) self.failUnless((eval_outputs([s]) == want).all())
verify_grad(lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3) utt.verify_grad(lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3)
def test_join_matrix1_using_vertical_stack(self): def test_join_matrix1_using_vertical_stack(self):
a = as_tensor_variable(numpy.array([[1, 2, 3], [4, 5, 6]])) a = as_tensor_variable(numpy.array([[1, 2, 3], [4, 5, 6]]))
...@@ -963,7 +963,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -963,7 +963,7 @@ class T_Join_and_Split(unittest.TestCase):
want = numpy.array([[1, 2, 3, 7, 3, 2, 1], [4, 5, 6, 8, 6, 5, 4]], dtype='float32') want = numpy.array([[1, 2, 3, 7, 3, 2, 1], [4, 5, 6, 8, 6, 5, 4]], dtype='float32')
self.failUnless((eval_outputs([s]) == want).all()) self.failUnless((eval_outputs([s]) == want).all())
verify_grad(lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3) utt.verify_grad(lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3)
def test_join_matrixV(self): def test_join_matrixV(self):
"""variable join axis""" """variable join axis"""
...@@ -983,8 +983,8 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -983,8 +983,8 @@ class T_Join_and_Split(unittest.TestCase):
got = f(1) got = f(1)
self.failUnless((got == want).all(), (got, want)) self.failUnless((got == want).all(), (got, want))
verify_grad(lambda a, b: join(0,a,b), [v, 2*v]) utt.verify_grad(lambda a, b: join(0,a,b), [v, 2*v])
verify_grad(lambda a, b: join(1,a,b), [v, 2*v]) utt.verify_grad(lambda a, b: join(1,a,b), [v, 2*v])
def test_vector_len(self): def test_vector_len(self):
x = lscalar('x') x = lscalar('x')
...@@ -1090,7 +1090,7 @@ class test_bitwise(unittest.TestCase): ...@@ -1090,7 +1090,7 @@ class test_bitwise(unittest.TestCase):
class T_add(unittest.TestCase): class T_add(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test_complex_all_ops(self): def test_complex_all_ops(self):
for nbits in (64, 128): for nbits in (64, 128):
...@@ -1107,22 +1107,22 @@ class T_add(unittest.TestCase): ...@@ -1107,22 +1107,22 @@ class T_add(unittest.TestCase):
self.failUnless(a.type.values_eq_approx(fn(a.data, b.data), f(a.data, b.data))) self.failUnless(a.type.values_eq_approx(fn(a.data, b.data), f(a.data, b.data)))
def test_grad_scalar_l(self): def test_grad_scalar_l(self):
verify_grad(add, [numpy.asarray([3.0]), numpy.random.rand(3)]) utt.verify_grad(add, [numpy.asarray([3.0]), numpy.random.rand(3)])
def test_grad_scalar_r(self): def test_grad_scalar_r(self):
verify_grad(add, [numpy.random.rand(3), numpy.asarray([3.0])]) utt.verify_grad(add, [numpy.random.rand(3), numpy.asarray([3.0])])
def test_grad_row(self): def test_grad_row(self):
verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) utt.verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
def test_grad_col(self): def test_grad_col(self):
verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) utt.verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
class T_exp(unittest.TestCase): class T_exp(unittest.TestCase):
def test_grad_0(self): def test_grad_0(self):
verify_grad(exp, [ utt.verify_grad(exp, [
numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ], numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ],
[ 2.04832468, 0.50791564, -1.58892269]])]) [ 2.04832468, 0.50791564, -1.58892269]])])
def test_grad_1(self): def test_grad_1(self):
verify_grad(inplace.exp_inplace, [ utt.verify_grad(inplace.exp_inplace, [
numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ], numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ],
[ 2.04832468, 0.50791564, -1.58892269]])]) [ 2.04832468, 0.50791564, -1.58892269]])])
...@@ -1139,8 +1139,8 @@ class T_exp(unittest.TestCase): ...@@ -1139,8 +1139,8 @@ class T_exp(unittest.TestCase):
# check_eq(self, t, abs(t), -d, abs(-d)) # check_eq(self, t, abs(t), -d, abs(-d))
# def test_grad(self): # def test_grad(self):
# verify_grad(Abs, [numpy.ones(())]) # utt.verify_grad(Abs, [numpy.ones(())])
# verify_grad(Abs, [numpy.ones(3)]) # utt.verify_grad(Abs, [numpy.ones(3)])
# class AbsBadGrad(Abs): # class AbsBadGrad(Abs):
# def grad(self, (x, ), (gz, )): # def grad(self, (x, ), (gz, )):
...@@ -1148,9 +1148,9 @@ class T_exp(unittest.TestCase): ...@@ -1148,9 +1148,9 @@ class T_exp(unittest.TestCase):
# def test_badgrad(self): # def test_badgrad(self):
# try: # try:
# verify_grad(T_abs.AbsBadGrad, [numpy.ones(())]) # utt.verify_grad(T_abs.AbsBadGrad, [numpy.ones(())])
# except Exception, e: # except Exception, e:
# self.failUnless(str(e) == verify_grad.E_grad, str(e)) # self.failUnless(str(e) == utt.verify_grad.E_grad, str(e))
# return # return
# self.fail() # self.fail()
...@@ -1187,7 +1187,7 @@ class T_exp(unittest.TestCase): ...@@ -1187,7 +1187,7 @@ class T_exp(unittest.TestCase):
# class T_mul(unittest.TestCase): # class T_mul(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # utt.seed_rng()
# def test_elemwise(self): # def test_elemwise(self):
# a = as_tensor_variable(0.0) # a = as_tensor_variable(0.0)
...@@ -1219,18 +1219,18 @@ class T_exp(unittest.TestCase): ...@@ -1219,18 +1219,18 @@ class T_exp(unittest.TestCase):
# check_eq2_both(self, [a1,a3], mul(a1,a3), [r1, r3], r1*r3) # check_eq2_both(self, [a1,a3], mul(a1,a3), [r1, r3], r1*r3)
# def test_grad_elemwise(self): # def test_grad_elemwise(self):
# verify_grad(Mul, [numpy.random.rand(3,4), numpy.random.rand(3,4)]) # utt.verify_grad(Mul, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
# def test_grad_scalar_l(self): # def test_grad_scalar_l(self):
# verify_grad(Mul, [numpy.asarray([3.0]), numpy.random.rand(3)]) # utt.verify_grad(Mul, [numpy.asarray([3.0]), numpy.random.rand(3)])
# def test_grad_scalar_r(self): # def test_grad_scalar_r(self):
# verify_grad(Mul, [numpy.random.rand(3), numpy.asarray([3.0])]) # utt.verify_grad(Mul, [numpy.random.rand(3), numpy.asarray([3.0])])
# def test_grad_row(self): # def test_grad_row(self):
# verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) # utt.verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
# def test_grad_row2(self): # def test_grad_row2(self):
# op = lambda x, y: Mul(x, DimShuffle(y, ['x', 0]).out) # op = lambda x, y: Mul(x, DimShuffle(y, ['x', 0]).out)
# verify_grad(op, [numpy.random.rand(3, 5), numpy.random.rand(5)]) # utt.verify_grad(op, [numpy.random.rand(3, 5), numpy.random.rand(5)])
# def test_grad_col(self): # def test_grad_col(self):
# verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) # utt.verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
# def test_wrong_shapes(self): # def test_wrong_shapes(self):
# a = as_tensor_variable(numpy.ones(3)) # a = as_tensor_variable(numpy.ones(3))
...@@ -1250,28 +1250,28 @@ class T_exp(unittest.TestCase): ...@@ -1250,28 +1250,28 @@ class T_exp(unittest.TestCase):
# class T_div(unittest.TestCase): # class T_div(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # utt.seed_rng()
# def test_grad_e(self): # def test_grad_e(self):
# verify_grad(Div, [numpy.random.rand(3), numpy.ones(3)]) # utt.verify_grad(Div, [numpy.random.rand(3), numpy.ones(3)])
# verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(3,5)+0.1]) # utt.verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(3,5)+0.1])
# verify_grad(Div, [numpy.ones(()), numpy.ones(())]) # utt.verify_grad(Div, [numpy.ones(()), numpy.ones(())])
# def test_grad_sl(self): # def test_grad_sl(self):
# verify_grad(Div, [numpy.ones((3, 5)), numpy.ones((1, 1))]) # utt.verify_grad(Div, [numpy.ones((3, 5)), numpy.ones((1, 1))])
# verify_grad(Div, [numpy.random.rand(3), numpy.ones((1, ))]) # utt.verify_grad(Div, [numpy.random.rand(3), numpy.ones((1, ))])
# verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(1,1)]) # utt.verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(1,1)])
# class T_log2(unittest.TestCase): # class T_log2(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # utt.seed_rng()
# def test0(self): # def test0(self):
# verify_grad(Log2, [numpy.random.rand(3,1)+0.0001]) # utt.verify_grad(Log2, [numpy.random.rand(3,1)+0.0001])
# class T_log(unittest.TestCase): # class T_log(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # utt.seed_rng()
# def test0(self): # def test0(self):
# verify_grad(Log, [numpy.random.rand(3,1)+0.0001]) # utt.verify_grad(Log, [numpy.random.rand(3,1)+0.0001])
# def test1(self): # def test1(self):
# a = as_tensor_variable(numpy.ones(2)) # a = as_tensor_variable(numpy.ones(2))
# b = as_tensor_variable(numpy.ones(2)) # b = as_tensor_variable(numpy.ones(2))
...@@ -1281,23 +1281,23 @@ class T_exp(unittest.TestCase): ...@@ -1281,23 +1281,23 @@ class T_exp(unittest.TestCase):
# class T_pow(unittest.TestCase): # class T_pow(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # utt.seed_rng()
# def test_elemwise(self): # def test_elemwise(self):
# verify_grad(Div, [numpy.random.rand(3,4), numpy.random.rand(3,4)+0.1]) # utt.verify_grad(Div, [numpy.random.rand(3,4), numpy.random.rand(3,4)+0.1])
# verify_grad(Pow, [numpy.random.rand(3,4), numpy.random.rand(3,4)]) # utt.verify_grad(Pow, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
# def test_scalar_l(self): # def test_scalar_l(self):
# verify_grad(Pow, [numpy.asarray([3.0]), numpy.random.rand(3)]) # utt.verify_grad(Pow, [numpy.asarray([3.0]), numpy.random.rand(3)])
# def test_scalar_r(self): # def test_scalar_r(self):
# verify_grad(Pow, [numpy.random.rand(3), numpy.asarray([3.0])]) # utt.verify_grad(Pow, [numpy.random.rand(3), numpy.asarray([3.0])])
# def test_row(self): # def test_row(self):
# verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) # utt.verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
# def test_col(self): # def test_col(self):
# verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) # utt.verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
class test_matinv(unittest.TestCase): class test_matinv(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def mat_reciprocal(self,dim): def mat_reciprocal(self,dim):
# symbolic program # symbolic program
...@@ -1306,7 +1306,7 @@ class test_matinv(unittest.TestCase): ...@@ -1306,7 +1306,7 @@ class test_matinv(unittest.TestCase):
# Note that TensorType's constructor does not actually allocate any memory. # Note that TensorType's constructor does not actually allocate any memory.
# TODO: Make TensorType syntax more explicit, and maybe give shape or number of dimensions. # TODO: Make TensorType syntax more explicit, and maybe give shape or number of dimensions.
unittest_tools.seed_rng() utt.seed_rng()
a, b = matrices('ab') a, b = matrices('ab')
ab = a*b ab = a*b
...@@ -1337,7 +1337,7 @@ class test_matinv(unittest.TestCase): ...@@ -1337,7 +1337,7 @@ class test_matinv(unittest.TestCase):
"""Matrix reciprocal by gradient descent""" """Matrix reciprocal by gradient descent"""
ssd0,ssd = self.mat_reciprocal(3) ssd0,ssd = self.mat_reciprocal(3)
unittest_tools.seed_rng() utt.seed_rng()
# hand-coded numpy implementation for verification # hand-coded numpy implementation for verification
x = numpy.random.rand(3,3)+0.1 x = numpy.random.rand(3,3)+0.1
w = numpy.random.rand(3,3) w = numpy.random.rand(3,3)
...@@ -1351,7 +1351,7 @@ class test_matinv(unittest.TestCase): ...@@ -1351,7 +1351,7 @@ class test_matinv(unittest.TestCase):
class t_dot(unittest.TestCase): class t_dot(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
@staticmethod @staticmethod
def rand(*args): def rand(*args):
return numpy.random.rand(*args) return numpy.random.rand(*args)
...@@ -1411,13 +1411,13 @@ class t_dot(unittest.TestCase): ...@@ -1411,13 +1411,13 @@ class t_dot(unittest.TestCase):
#def test_align_3_3(self): self.not_aligned(self.rand(5,4,3), self.rand(6,7,8)) #def test_align_3_3(self): self.not_aligned(self.rand(5,4,3), self.rand(6,7,8))
def test_grad(self): def test_grad(self):
#verify_grad(dot, [self.rand(2,3,4), self.rand(4)]) #utt.verify_grad(dot, [self.rand(2,3,4), self.rand(4)])
verify_grad(dot, [self.rand(2,3), self.rand(3,2)]) utt.verify_grad(dot, [self.rand(2,3), self.rand(3,2)])
verify_grad(dot, [self.rand(2), self.rand(2,3)]) utt.verify_grad(dot, [self.rand(2), self.rand(2,3)])
verify_grad(dot, [self.rand(3,2), self.rand(2)]) utt.verify_grad(dot, [self.rand(3,2), self.rand(2)])
verify_grad(dot, [self.rand(2), self.rand(2)]) utt.verify_grad(dot, [self.rand(2), self.rand(2)])
#verify_grad(dot, [self.rand(), self.rand(2)]) #utt.verify_grad(dot, [self.rand(), self.rand(2)])
#verify_grad(dot, [self.rand(), self.rand(2,5)]) #utt.verify_grad(dot, [self.rand(), self.rand(2,5)])
class T_tensorfromscalar(unittest.TestCase): class T_tensorfromscalar(unittest.TestCase):
def test0(self): def test0(self):
...@@ -1464,7 +1464,7 @@ class T_tensorfromscalar(unittest.TestCase): ...@@ -1464,7 +1464,7 @@ class T_tensorfromscalar(unittest.TestCase):
# class T_tensor(unittest.TestCase): # class T_tensor(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # utt.seed_rng()
# def test0(self): # allocate from a scalar float # def test0(self): # allocate from a scalar float
# t = _tensor(1.0) # t = _tensor(1.0)
# self.failUnless(isinstance(t, TensorType)) # self.failUnless(isinstance(t, TensorType))
...@@ -1635,7 +1635,7 @@ class test_grad(unittest.TestCase): ...@@ -1635,7 +1635,7 @@ class test_grad(unittest.TestCase):
class T_op_cache(unittest.TestCase): class T_op_cache(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
"""trigger bug in ticket #162""" """trigger bug in ticket #162"""
lr = constant(0.011) lr = constant(0.011)
...@@ -1679,7 +1679,7 @@ def test_reshape(): ...@@ -1679,7 +1679,7 @@ def test_reshape():
assert numpy.all(a_val == a_val_copy) assert numpy.all(a_val == a_val_copy)
# verify gradient # verify gradient
tensor.verify_grad(Reshape(2), [a_val,numpy.asarray([2,3], dtype='float64')]) utt.verify_grad(Reshape(2), [a_val,numpy.asarray([2,3], dtype='float64')])
def test_flatten_outdimNone(): def test_flatten_outdimNone():
...@@ -1695,7 +1695,7 @@ def test_flatten_outdimNone(): ...@@ -1695,7 +1695,7 @@ def test_flatten_outdimNone():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
tensor.verify_grad(Flatten(), [a_val]) utt.verify_grad(Flatten(), [a_val])
def test_flatten_scalar(): def test_flatten_scalar():
a = dscalar() a = dscalar()
...@@ -1707,7 +1707,7 @@ def test_flatten_scalar(): ...@@ -1707,7 +1707,7 @@ def test_flatten_scalar():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
#tensor.verify_grad(Flatten(), [a_val]) #TODO: fix verify_grd to work on scalars #utt.verify_grad(Flatten(), [a_val]) #TODO: fix verify_grd to work on scalars
def test_flatten_outdim1(): def test_flatten_outdim1():
a = dmatrix() a = dmatrix()
...@@ -1719,7 +1719,7 @@ def test_flatten_outdim1(): ...@@ -1719,7 +1719,7 @@ def test_flatten_outdim1():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
tensor.verify_grad(Flatten(1), [a_val]) utt.verify_grad(Flatten(1), [a_val])
def test_flatten_outdim2(): def test_flatten_outdim2():
a = dmatrix() a = dmatrix()
...@@ -1730,7 +1730,7 @@ def test_flatten_outdim2(): ...@@ -1730,7 +1730,7 @@ def test_flatten_outdim2():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==a_val) assert numpy.all(f(a_val)==a_val)
tensor.verify_grad(Flatten(2), [a_val]) utt.verify_grad(Flatten(2), [a_val])
def test_flatten_outdim2_of_3(): def test_flatten_outdim2_of_3():
a = TensorType('float64', (False, False, False))() a = TensorType('float64', (False, False, False))()
...@@ -1742,7 +1742,7 @@ def test_flatten_outdim2_of_3(): ...@@ -1742,7 +1742,7 @@ def test_flatten_outdim2_of_3():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
tensor.verify_grad(Flatten(2), [a_val]) utt.verify_grad(Flatten(2), [a_val])
def test_flatten_outdim_invalid(): def test_flatten_outdim_invalid():
a = dmatrix() a = dmatrix()
...@@ -1765,7 +1765,7 @@ def test_tile(): ...@@ -1765,7 +1765,7 @@ def test_tile():
class test_tensordot(unittest.TestCase): class test_tensordot(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() utt.seed_rng()
def test0(self): def test0(self):
...@@ -1779,7 +1779,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1779,7 +1779,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(5); bval = numpy.random.rand(5);
self.failUnless(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.tensordot(aval,bval,axes) == \
f1(aval,bval)) f1(aval,bval))
tensor.verify_grad(TensorDot(axes), [aval,bval]) utt.verify_grad(TensorDot(axes), [aval,bval])
# test matrix-vector # test matrix-vector
bmat = dmatrix() bmat = dmatrix()
...@@ -1790,7 +1790,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1790,7 +1790,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(8,5); bval = numpy.random.rand(8,5);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f2(aval,bval))) f2(aval,bval)))
tensor.verify_grad(TensorDot(axes), [aval,bval]) utt.verify_grad(TensorDot(axes), [aval,bval])
# test matrix-matrix # test matrix-matrix
amat = dmatrix() amat = dmatrix()
...@@ -1801,7 +1801,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1801,7 +1801,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(7,9); bval = numpy.random.rand(7,9);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f3(aval,bval))) f3(aval,bval)))
tensor.verify_grad(TensorDot(axes), [aval,bval]) utt.verify_grad(TensorDot(axes), [aval,bval])
# test ndarray-matrix, sum over one dim of matrix # test ndarray-matrix, sum over one dim of matrix
atens = TensorType('float64', broadcastable=(False,)*4)() atens = TensorType('float64', broadcastable=(False,)*4)()
...@@ -1812,7 +1812,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1812,7 +1812,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(2,3); bval = numpy.random.rand(2,3);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f4(aval,bval))) f4(aval,bval)))
tensor.verify_grad(TensorDot(axes), [aval,bval]) utt.verify_grad(TensorDot(axes), [aval,bval])
# test ndarray-ndarray # test ndarray-ndarray
atens = TensorType('float64', broadcastable=(False,)*4)() atens = TensorType('float64', broadcastable=(False,)*4)()
...@@ -1824,14 +1824,14 @@ class test_tensordot(unittest.TestCase): ...@@ -1824,14 +1824,14 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(3,4,2); bval = numpy.random.rand(3,4,2);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f5(aval,bval))) f5(aval,bval)))
tensor.verify_grad(TensorDot(axes), [aval,bval]) utt.verify_grad(TensorDot(axes), [aval,bval])
axes = (axes[1],axes[0]) axes = (axes[1],axes[0])
c = tensordot(axes)(btens, atens) c = tensordot(axes)(btens, atens)
f6 = inplace_func([btens,atens],c) f6 = inplace_func([btens,atens],c)
self.failUnless(numpy.all(numpy.tensordot(bval,aval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(bval,aval,axes) == \
f6(bval,aval))) f6(bval,aval)))
tensor.verify_grad(TensorDot(axes), [bval,aval]) utt.verify_grad(TensorDot(axes), [bval,aval])
def test_smallest_stack(): def test_smallest_stack():
sx, sy = dscalar(), dscalar() sx, sy = dscalar(), dscalar()
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论