提交 c63fe0ea authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Use assertTrue (and assert*) instead of failUnless (and fail*) in tests.

fail* functions are deprecated in Python 2.7. Also remove reference to these deprecated functions from the doc.
上级 b53baf0a
...@@ -47,7 +47,7 @@ example: ...@@ -47,7 +47,7 @@ example:
# test passes cleanly # test passes cleanly
def test1(self): def test1(self):
self.failUnless(2+2 == 5) self.assertTrue(2+2 == 5)
# raises an exception, causes test to fail # raises an exception, causes test to fail
def test2(self): def test2(self):
...@@ -221,13 +221,13 @@ Example: ...@@ -221,13 +221,13 @@ Example:
c = T.dot(a,b) c = T.dot(a,b)
f = theano.function([a,b],[c]) f = theano.function([a,b],[c])
cmp = f(self.avals,self.bvals) == numpy.dot(self.avals,self.bvals) cmp = f(self.avals,self.bvals) == numpy.dot(self.avals,self.bvals)
self.failUnless(numpy.all(cmp)) self.assertTrue(numpy.all(cmp))
Avoid hard-coding variables, as in the following case: Avoid hard-coding variables, as in the following case:
.. code-block:: python .. code-block:: python
self.failUnless(numpy.all(f(self.avals,self.bvals)==numpy.array([[25,25,30,28],[21,18,14,25]]))) self.assertTrue(numpy.all(f(self.avals,self.bvals)==numpy.array([[25,25,30,28],[21,18,14,25]])))
This makes the test case less manageable and forces the user to update This makes the test case less manageable and forces the user to update
the variables each time the input is changed or possibly when the the variables each time the input is changed or possibly when the
...@@ -238,15 +238,15 @@ idea. ...@@ -238,15 +238,15 @@ idea.
Here is a list of useful functions, as defined by TestCase: Here is a list of useful functions, as defined by TestCase:
* checking the state of boolean variables: assert, failUnless, * checking the state of boolean variables: assert,
assertTrue, failIf, assertFalse assertTrue, assertFalse
* checking for (in)equality constraints: assertEqual, failUnlessEqual, * checking for (in)equality constraints: assertEqual,
assertNotEqual, failIfEqual assertNotEqual
* checking for (in)equality constraints up to a given precision (very * checking for (in)equality constraints up to a given precision (very
useful in theano): assertAlmostEqual, failUnlessAlmostEqual, useful in theano): assertAlmostEqual,
assertNotAlmostEqual, failIfAlmostEqual assertNotAlmostEqual
Checking for errors Checking for errors
...@@ -270,11 +270,11 @@ Example: ...@@ -270,11 +270,11 @@ Example:
b = T.dmatrix() b = T.dmatrix()
c = T.dot(a,b) # we expect this to fail c = T.dot(a,b) # we expect this to fail
# above should fail as dot operates on 2D tensors only # above should fail as dot operates on 2D tensors only
self.failUnlessRaises(TypeError, func) self.assertRaises(TypeError, func)
Useful functions, as defined by TestCase: Useful function, as defined by TestCase:
* assertRaises, failUnlessRaises * assertRaises
Test Cases and Theano Modes Test Cases and Theano Modes
......
...@@ -126,11 +126,11 @@ Guillaume can you make sure to hit these points: ...@@ -126,11 +126,11 @@ Guillaume can you make sure to hit these points:
* What is the right eq function to use? * What is the right eq function to use?
* There are a lot of tests that define their own epsilon, but this should be standardized. e.g. in test_elemwise.py ``self.failUnless((numpy.abs(f(xv) - zv) < 1e-10).all())`` * There are a lot of tests that define their own epsilon, but this should be standardized. e.g. in test_elemwise.py ``self.assertTrue((numpy.abs(f(xv) - zv) < 1e-10).all())``
* If the expected variable of a test is that an Exception is thrown, how do we correctly detect and handle that? * If the expected variable of a test is that an Exception is thrown, how do we correctly detect and handle that?
nosetests has ``failUnlessRaises`` nosetests has ``assertRaises``
* Convention is that all test files must start with ``test_``, not * Convention is that all test files must start with ``test_``, not
``_test_``, so rename all that use the old convention? ``_test_``, so rename all that use the old convention?
......
...@@ -475,15 +475,15 @@ class Test_check_isfinite(unittest.TestCase): ...@@ -475,15 +475,15 @@ class Test_check_isfinite(unittest.TestCase):
# if TensorType.filter_checks_isfinite were true, these would raise ValueError # if TensorType.filter_checks_isfinite were true, these would raise ValueError
# if not, DebugMode will check internally, and raise InvalidValueError # if not, DebugMode will check internally, and raise InvalidValueError
# passing an invalid value as an input should trigger ValueError # passing an invalid value as an input should trigger ValueError
self.failUnlessRaises(debugmode.InvalidValueError, f, self.assertRaises(debugmode.InvalidValueError, f,
numpy.log([3, -4, 5]).astype(config.floatX)) numpy.log([3, -4, 5]).astype(config.floatX))
self.failUnlessRaises(debugmode.InvalidValueError, f, self.assertRaises(debugmode.InvalidValueError, f,
(numpy.asarray([0, 1.0, 0])/0).astype(config.floatX)) (numpy.asarray([0, 1.0, 0])/0).astype(config.floatX))
self.failUnlessRaises(debugmode.InvalidValueError, f, self.assertRaises(debugmode.InvalidValueError, f,
(numpy.asarray([1.0, 1.0, 1.0])/0).astype(config.floatX)) (numpy.asarray([1.0, 1.0, 1.0])/0).astype(config.floatX))
# generating an invalid value internally should trigger InvalidValueError # generating an invalid value internally should trigger InvalidValueError
self.failUnlessRaises(debugmode.InvalidValueError, g, self.assertRaises(debugmode.InvalidValueError, g,
numpy.asarray([3,-4,5], dtype=config.floatX)) numpy.asarray([3,-4,5], dtype=config.floatX))
# this should disable the exception # this should disable the exception
......
...@@ -52,7 +52,7 @@ class TestNnet(unittest.TestCase): ...@@ -52,7 +52,7 @@ class TestNnet(unittest.TestCase):
mean_cost += cost mean_cost += cost
mean_cost /= float(len(data)) mean_cost /= float(len(data))
print 'Mean cost at epoch %s: %s' % (epoch, mean_cost) print 'Mean cost at epoch %s: %s' % (epoch, mean_cost)
self.failUnless(abs(mean_cost - 0.20588975452) < 1e-6) self.assertTrue(abs(mean_cost - 0.20588975452) < 1e-6)
# Just call functions to make sure they do not crash. # Just call functions to make sure they do not crash.
out = nnet.compute_output(input) out = nnet.compute_output(input)
out = nnet.output_from_hidden(numpy.ones(10)) out = nnet.output_from_hidden(numpy.ones(10))
......
...@@ -27,23 +27,23 @@ class Test_pfunc(unittest.TestCase): ...@@ -27,23 +27,23 @@ class Test_pfunc(unittest.TestCase):
b = shared(1) b = shared(1)
f1 = pfunc([a], a+b) f1 = pfunc([a], a+b)
f2 = pfunc([Param(a, default=44)], a + b, updates={b: b + 1}) f2 = pfunc([Param(a, default=44)], a + b, updates={b: b + 1})
self.failUnless(b.get_value() == 1) self.assertTrue(b.get_value() == 1)
self.failUnless(f1(3) == 4) self.assertTrue(f1(3) == 4)
self.failUnless(f2(3) == 4) self.assertTrue(f2(3) == 4)
self.failUnless(b.get_value() == 2) self.assertTrue(b.get_value() == 2)
self.failUnless(f1(3) == 5) self.assertTrue(f1(3) == 5)
b.set_value(0) b.set_value(0)
self.failUnless(f1(3) == 3) self.assertTrue(f1(3) == 3)
# Example #2. # Example #2.
a = tensor.lscalar() a = tensor.lscalar()
b = shared(7) b = shared(7)
f1 = pfunc([a], a + b) f1 = pfunc([a], a + b)
f2 = pfunc([a], a * b) f2 = pfunc([a], a * b)
self.failUnless(f1(5) == 12) self.assertTrue(f1(5) == 12)
b.set_value(8) b.set_value(8)
self.failUnless(f1(5) == 13) self.assertTrue(f1(5) == 13)
self.failUnless(f2(4) == 32) self.assertTrue(f2(4) == 32)
def test_shared(self): def test_shared(self):
...@@ -317,25 +317,25 @@ class Test_pfunc(unittest.TestCase): ...@@ -317,25 +317,25 @@ class Test_pfunc(unittest.TestCase):
x = shared(0) x = shared(0)
assign = pfunc([], [], updates = {x: 3}) assign = pfunc([], [], updates = {x: 3})
assign() assign()
self.failUnless(x.get_value() == 3) self.assertTrue(x.get_value() == 3)
# Basic increment function. # Basic increment function.
x.set_value(0) x.set_value(0)
inc = pfunc([], [], updates = {x: x + 1}) inc = pfunc([], [], updates = {x: x + 1})
inc() inc()
self.failUnless(x.get_value() == 1) self.assertTrue(x.get_value() == 1)
# Increment by a constant value. # Increment by a constant value.
x.set_value(-1) x.set_value(-1)
y = shared(2) y = shared(2)
inc_by_y = pfunc([], [], updates = {x: x + y}) inc_by_y = pfunc([], [], updates = {x: x + y})
inc_by_y() inc_by_y()
self.failUnless(x.get_value() == 1) self.assertTrue(x.get_value() == 1)
def test_duplicate_updates(self): def test_duplicate_updates(self):
x, y = dmatrices('x', 'y') x, y = dmatrices('x', 'y')
z = shared(numpy.ones((2,3))) z = shared(numpy.ones((2,3)))
self.failUnlessRaises(ValueError, theano.function, [x,y], [z], updates=[(z, z+x+y), (z, z-x)]) self.assertRaises(ValueError, theano.function, [x,y], [z], updates=[(z, z+x+y), (z, z-x)])
def test_givens(self): def test_givens(self):
x = shared(0) x = shared(0)
...@@ -419,9 +419,9 @@ class Test_pfunc(unittest.TestCase): ...@@ -419,9 +419,9 @@ class Test_pfunc(unittest.TestCase):
print x.get_value() print x.get_value()
assert x.get_value() == 6 assert x.get_value() == 6
self.failUnlessRaises(TypeError, pfunc, [], [x], no_default_updates=(x)) self.assertRaises(TypeError, pfunc, [], [x], no_default_updates=(x))
self.failUnlessRaises(TypeError, pfunc, [], [x], no_default_updates=x) self.assertRaises(TypeError, pfunc, [], [x], no_default_updates=x)
self.failUnlessRaises(TypeError, pfunc, [], [x], no_default_updates='canard') self.assertRaises(TypeError, pfunc, [], [x], no_default_updates='canard')
# Mix explicit updates and no_default_updates # Mix explicit updates and no_default_updates
g1 = pfunc([], [x], updates=[(x,x-1)], no_default_updates=True) g1 = pfunc([], [x], updates=[(x,x-1)], no_default_updates=True)
...@@ -582,7 +582,7 @@ class Test_pfunc(unittest.TestCase): ...@@ -582,7 +582,7 @@ class Test_pfunc(unittest.TestCase):
assert y.get_value() == 2 assert y.get_value() == 2
# a is needed as input if y.default_update is used # a is needed as input if y.default_update is used
self.failUnlessRaises(TypeError, pfunc, [], x) self.assertRaises(TypeError, pfunc, [], x)
def test_default_updates_partial_graph(self): def test_default_updates_partial_graph(self):
a = shared(0) a = shared(0)
......
...@@ -34,7 +34,7 @@ class Test_SharedVariable(unittest.TestCase): ...@@ -34,7 +34,7 @@ class Test_SharedVariable(unittest.TestCase):
assert shared([]).type == generic assert shared([]).type == generic
def badfunc(): def badfunc():
shared(7, bad_kw=False) shared(7, bad_kw=False)
self.failUnlessRaises(TypeError, badfunc) self.assertRaises(TypeError, badfunc)
def test_strict_generic(self): def test_strict_generic(self):
...@@ -119,38 +119,38 @@ class Test_SharedVariable(unittest.TestCase): ...@@ -119,38 +119,38 @@ class Test_SharedVariable(unittest.TestCase):
b = shared(numpy.int64(7), strict=True) b = shared(numpy.int64(7), strict=True)
assert b.type == theano.tensor.lscalar assert b.type == theano.tensor.lscalar
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.int32(7), strict=True) b = shared(numpy.int32(7), strict=True)
assert b.type == theano.tensor.iscalar assert b.type == theano.tensor.iscalar
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.int16(7), strict=True) b = shared(numpy.int16(7), strict=True)
assert b.type == theano.tensor.wscalar assert b.type == theano.tensor.wscalar
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.int8(7), strict=True) b = shared(numpy.int8(7), strict=True)
assert b.type == theano.tensor.bscalar assert b.type == theano.tensor.bscalar
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.float64(7.234), strict=True) b = shared(numpy.float64(7.234), strict=True)
assert b.type == theano.tensor.dscalar assert b.type == theano.tensor.dscalar
self.failUnlessRaises(TypeError, f, b, 8) self.assertRaises(TypeError, f, b, 8)
b = shared(numpy.float32(7.234), strict=True) b = shared(numpy.float32(7.234), strict=True)
assert b.type == theano.tensor.fscalar assert b.type == theano.tensor.fscalar
self.failUnlessRaises(TypeError, f, b, 8) self.assertRaises(TypeError, f, b, 8)
b = shared(numpy.float(7.234), strict=True) b = shared(numpy.float(7.234), strict=True)
assert b.type == theano.tensor.dscalar assert b.type == theano.tensor.dscalar
self.failUnlessRaises(TypeError, f, b, 8) self.assertRaises(TypeError, f, b, 8)
b = shared(7.234, strict=True) b = shared(7.234, strict=True)
assert b.type == theano.tensor.dscalar assert b.type == theano.tensor.dscalar
self.failUnlessRaises(TypeError, f, b, 8) self.assertRaises(TypeError, f, b, 8)
c = shared(numpy.zeros((5,5), dtype='float32')) c = shared(numpy.zeros((5,5), dtype='float32'))
self.failUnlessRaises(TypeError, f, b, numpy.random.rand(5,5)) self.assertRaises(TypeError, f, b, numpy.random.rand(5,5))
...@@ -160,40 +160,40 @@ class Test_SharedVariable(unittest.TestCase): ...@@ -160,40 +160,40 @@ class Test_SharedVariable(unittest.TestCase):
b = shared(numpy.int64([7]), strict=True) b = shared(numpy.int64([7]), strict=True)
assert b.type == theano.tensor.lvector assert b.type == theano.tensor.lvector
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.int32([7]), strict=True) b = shared(numpy.int32([7]), strict=True)
assert b.type == theano.tensor.ivector assert b.type == theano.tensor.ivector
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.int16([7]), strict=True) b = shared(numpy.int16([7]), strict=True)
assert b.type == theano.tensor.wvector assert b.type == theano.tensor.wvector
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.int8([7]), strict=True) b = shared(numpy.int8([7]), strict=True)
assert b.type == theano.tensor.bvector assert b.type == theano.tensor.bvector
self.failUnlessRaises(TypeError, f, b, 8.23) self.assertRaises(TypeError, f, b, 8.23)
b = shared(numpy.float64([7.234]), strict=True) b = shared(numpy.float64([7.234]), strict=True)
assert b.type == theano.tensor.dvector assert b.type == theano.tensor.dvector
self.failUnlessRaises(TypeError, f, b, 8) self.assertRaises(TypeError, f, b, 8)
b = shared(numpy.float32([7.234]), strict=True) b = shared(numpy.float32([7.234]), strict=True)
assert b.type == theano.tensor.fvector assert b.type == theano.tensor.fvector
self.failUnlessRaises(TypeError, f, b, 8) self.assertRaises(TypeError, f, b, 8)
#numpy.float([7.234]) don't work #numpy.float([7.234]) don't work
# b = shared(numpy.float([7.234]), strict=True) # b = shared(numpy.float([7.234]), strict=True)
# assert b.type == theano.tensor.dvector # assert b.type == theano.tensor.dvector
# self.failUnlessRaises(TypeError, f, b, 8) # self.assertRaises(TypeError, f, b, 8)
#This generate a generic type. Should we cast? I don't think. #This generate a generic type. Should we cast? I don't think.
# b = shared([7.234], strict=True) # b = shared([7.234], strict=True)
# assert b.type == theano.tensor.dvector # assert b.type == theano.tensor.dvector
# self.failUnlessRaises(TypeError, f, b, 8) # self.assertRaises(TypeError, f, b, 8)
c = shared(numpy.zeros((5,5), dtype='float32')) c = shared(numpy.zeros((5,5), dtype='float32'))
self.failUnlessRaises(TypeError, f, b, numpy.random.rand(5,5)) self.assertRaises(TypeError, f, b, numpy.random.rand(5,5))
...@@ -252,7 +252,7 @@ class Test_SharedVariable(unittest.TestCase): ...@@ -252,7 +252,7 @@ class Test_SharedVariable(unittest.TestCase):
assert b.get_value()==8 assert b.get_value()==8
c = shared(numpy.zeros((5,5), dtype='float32'), allow_downcast=True) c = shared(numpy.zeros((5,5), dtype='float32'), allow_downcast=True)
self.failUnlessRaises(TypeError, f, b, numpy.random.rand(5,5)) self.assertRaises(TypeError, f, b, numpy.random.rand(5,5))
...@@ -306,4 +306,4 @@ class Test_SharedVariable(unittest.TestCase): ...@@ -306,4 +306,4 @@ class Test_SharedVariable(unittest.TestCase):
assert b.get_value() == 8 assert b.get_value() == 8
c = shared(numpy.zeros((5,5), dtype='float32'), allow_downcast=True) c = shared(numpy.zeros((5,5), dtype='float32'), allow_downcast=True)
self.failUnlessRaises(TypeError, f, b, numpy.random.rand(5,5)) self.assertRaises(TypeError, f, b, numpy.random.rand(5,5))
...@@ -59,7 +59,7 @@ class T_solve(unittest.TestCase): ...@@ -59,7 +59,7 @@ class T_solve(unittest.TestCase):
x=scipy.linalg.solve(A,b) x=scipy.linalg.solve(A,b)
Ax = numpy.dot(A,x) Ax = numpy.dot(A,x)
are = tensor.numeric_grad.abs_rel_err(Ax, b) are = tensor.numeric_grad.abs_rel_err(Ax, b)
self.failUnless(numpy.all(are < 1.0e-5), (are, Ax, b)) self.assertTrue(numpy.all(are < 1.0e-5), (are, Ax, b))
#print A,b #print A,b
#print numpy.dot(A,x) #print numpy.dot(A,x)
...@@ -49,7 +49,7 @@ class test_ScalarOps(unittest.TestCase): ...@@ -49,7 +49,7 @@ class test_ScalarOps(unittest.TestCase):
(1,2), (-1,2), (1,-2), (-1,-2), (1,2), (-1,2), (1,-2), (-1,-2),
(5,3), (-5,3), (5,-3), (-5,-3) (5,3), (-5,3), (5,-3), (-5,-3)
): ):
self.failUnless(fn(a,b) == a%b, (a,)) self.assertTrue(fn(a,b) == a%b, (a,))
class test_composite(unittest.TestCase): class test_composite(unittest.TestCase):
...@@ -106,72 +106,72 @@ class test_logical(unittest.TestCase): ...@@ -106,72 +106,72 @@ class test_logical(unittest.TestCase):
x, y, z = inputs() x, y, z = inputs()
fn = gof.DualLinker().accept(Env([x,y], [x > y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x > y])).make_function()
for a,b in ((3.,9), (3,0.9), (3,3)): for a,b in ((3.,9), (3,0.9), (3,3)):
self.failUnless(fn(a,b) == (a>b)) self.assertTrue(fn(a,b) == (a>b))
def test_lt(self): def test_lt(self):
x, y, z = inputs() x, y, z = inputs()
fn = gof.DualLinker().accept(Env([x,y], [x < y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x < y])).make_function()
for a,b in ((3.,9), (3,0.9), (3,3)): for a,b in ((3.,9), (3,0.9), (3,3)):
self.failUnless(fn(a,b) == (a<b)) self.assertTrue(fn(a,b) == (a<b))
def test_le(self): def test_le(self):
x, y, z = inputs() x, y, z = inputs()
fn = gof.DualLinker().accept(Env([x,y], [x <= y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x <= y])).make_function()
for a,b in ((3.,9), (3,0.9), (3,3)): for a,b in ((3.,9), (3,0.9), (3,3)):
self.failUnless(fn(a,b) == (a<=b)) self.assertTrue(fn(a,b) == (a<=b))
def test_ge(self): def test_ge(self):
x, y, z = inputs() x, y, z = inputs()
fn = gof.DualLinker().accept(Env([x,y], [x >= y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x >= y])).make_function()
for a,b in ((3.,9), (3,0.9), (3,3)): for a,b in ((3.,9), (3,0.9), (3,3)):
self.failUnless(fn(a,b) == (a>=b)) self.assertTrue(fn(a,b) == (a>=b))
def test_eq(self): def test_eq(self):
x, y, z = inputs() x, y, z = inputs()
fn = gof.DualLinker().accept(Env([x,y], [eq(x,y)])).make_function() fn = gof.DualLinker().accept(Env([x,y], [eq(x,y)])).make_function()
for a,b in ((3.,9), (3,0.9), (3,3)): for a,b in ((3.,9), (3,0.9), (3,3)):
self.failUnless(fn(a,b) == (a==b)) self.assertTrue(fn(a,b) == (a==b))
def test_neq(self): def test_neq(self):
x, y, z = inputs() x, y, z = inputs()
fn = gof.DualLinker().accept(Env([x,y], [neq(x,y)])).make_function() fn = gof.DualLinker().accept(Env([x,y], [neq(x,y)])).make_function()
for a,b in ((3.,9), (3,0.9), (3,3)): for a,b in ((3.,9), (3,0.9), (3,3)):
self.failUnless(fn(a,b) == (a!=b)) self.assertTrue(fn(a,b) == (a!=b))
def test_or(self): def test_or(self):
x, y, z = ints('xyz') x, y, z = ints('xyz')
fn = gof.DualLinker().accept(Env([x,y], [x|y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x|y])).make_function()
for a,b in ((0,1), (0,0), (1,0), (1,1)): for a,b in ((0,1), (0,0), (1,0), (1,1)):
self.failUnless(fn(a,b) == (a|b), (a,b)) self.assertTrue(fn(a,b) == (a|b), (a,b))
def test_xor(self): def test_xor(self):
x, y, z = ints('xyz') x, y, z = ints('xyz')
fn = gof.DualLinker().accept(Env([x,y], [x^y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x^y])).make_function()
for a,b in ((0,1), (0,0), (1,0), (1,1)): for a,b in ((0,1), (0,0), (1,0), (1,1)):
self.failUnless(fn(a,b) == (a ^ b), (a,b)) self.assertTrue(fn(a,b) == (a ^ b), (a,b))
def test_and(self): def test_and(self):
x, y, z = ints('xyz') x, y, z = ints('xyz')
fn = gof.DualLinker().accept(Env([x,y], [and_(x, y)])).make_function() fn = gof.DualLinker().accept(Env([x,y], [and_(x, y)])).make_function()
for a,b in ((0,1), (0,0), (1,0), (1,1)): for a,b in ((0,1), (0,0), (1,0), (1,1)):
self.failUnless(fn(a,b) == (a & b), (a,b)) self.assertTrue(fn(a,b) == (a & b), (a,b))
x, y, z = ints('xyz') x, y, z = ints('xyz')
fn = gof.DualLinker().accept(Env([x,y], [x & y])).make_function() fn = gof.DualLinker().accept(Env([x,y], [x & y])).make_function()
for a,b in ((0,1), (0,0), (1,0), (1,1)): for a,b in ((0,1), (0,0), (1,0), (1,1)):
self.failUnless(fn(a,b) == (a & b), (a,b)) self.assertTrue(fn(a,b) == (a & b), (a,b))
def test_not(self): def test_not(self):
x, y, z = ints('xyz') x, y, z = ints('xyz')
fn = gof.DualLinker().accept(Env([x,y], [invert(x)])).make_function() fn = gof.DualLinker().accept(Env([x,y], [invert(x)])).make_function()
for a,b in ((0,1), (0,0), (1,0), (1,1)): for a,b in ((0,1), (0,0), (1,0), (1,1)):
self.failUnless(fn(a,b) == ~a, (a,)) self.assertTrue(fn(a,b) == ~a, (a,))
x, y, z = ints('xyz') x, y, z = ints('xyz')
fn = gof.DualLinker().accept(Env([x,y], [~x])).make_function() fn = gof.DualLinker().accept(Env([x,y], [~x])).make_function()
for a,b in ((0,1), (0,0), (1,0), (1,1)): for a,b in ((0,1), (0,0), (1,0), (1,1)):
self.failUnless(fn(a,b) == ~a, (a,)) self.assertTrue(fn(a,b) == ~a, (a,))
class test_div(unittest.TestCase): class test_div(unittest.TestCase):
......
...@@ -93,103 +93,103 @@ class test_true_dot(unittest.TestCase): ...@@ -93,103 +93,103 @@ class test_true_dot(unittest.TestCase):
x = as_sparse_variable(mtype((500,3))) x = as_sparse_variable(mtype((500,3)))
x.data[(10, 1)] = 1 x.data[(10, 1)] = 1
x.data[(20, 2)] = 2 x.data[(20, 2)] = 2
self.failUnless(_is_sparse_variable(x)) self.assertTrue(_is_sparse_variable(x))
xT = x.T xT = x.T
self.failUnless(_is_sparse_variable(xT)) self.assertTrue(_is_sparse_variable(xT))
zop = true_dot(x,xT) zop = true_dot(x,xT)
self.failUnless(_is_sparse_variable(zop)) self.assertTrue(_is_sparse_variable(zop))
z = eval_outputs([zop]) z = eval_outputs([zop])
self.failUnless(_is_sparse(z)) self.assertTrue(_is_sparse(z))
self.failUnless(z.shape == (500,500)) self.assertTrue(z.shape == (500,500))
self.failUnless(type(z) is mtype) self.assertTrue(type(z) is mtype)
w = mtype((500,500)) w = mtype((500,500))
w[(10, 10)] = 1 w[(10, 10)] = 1
w[(20, 20)] = 4 w[(20, 20)] = 4
self.failUnless(z.shape == w.shape) self.assertTrue(z.shape == w.shape)
self.failUnless(type(z) == type(w)) self.assertTrue(type(z) == type(w))
self.failUnless(z.dtype == w.dtype) self.assertTrue(z.dtype == w.dtype)
#self.failUnless(z == w) #self.assertTrue(z == w)
self.failUnless(abs(z-w).nnz == 0) self.assertTrue(abs(z-w).nnz == 0)
z = z.todense() z = z.todense()
w = w.todense() w = w.todense()
self.failUnless((z == w).all() == True) self.assertTrue((z == w).all() == True)
def test_basicSD(self): def test_basicSD(self):
for mtype in _mtypes: for mtype in _mtypes:
x = as_sparse_variable(mtype((500,3))) x = as_sparse_variable(mtype((500,3)))
x.data[(10, 1)] = 1 x.data[(10, 1)] = 1
x.data[(20, 2)] = 2 x.data[(20, 2)] = 2
self.failUnless(_is_sparse_variable(x)) self.assertTrue(_is_sparse_variable(x))
y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]]) y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]])
self.failUnless(_is_dense_variable(y)) self.assertTrue(_is_dense_variable(y))
zop = true_dot(x,y) zop = true_dot(x,y)
self.failUnless(_is_sparse_variable(zop)) self.assertTrue(_is_sparse_variable(zop))
z = eval_outputs([zop]) z = eval_outputs([zop])
self.failUnless(_is_sparse(z)) self.assertTrue(_is_sparse(z))
self.failUnless(z.shape == (500,2)) self.assertTrue(z.shape == (500,2))
self.failUnless(type(z) is mtype) self.assertTrue(type(z) is mtype)
w = mtype((500,2)) w = mtype((500,2))
w[(10, 0)] = 3. w[(10, 0)] = 3.
w[(20, 0)] = 4 w[(20, 0)] = 4
w[(10, 1)] = 4 w[(10, 1)] = 4
w[(20, 1)] = 2 w[(20, 1)] = 2
self.failUnless(z.shape == w.shape) self.assertTrue(z.shape == w.shape)
self.failUnless(type(z) == type(w)) self.assertTrue(type(z) == type(w))
self.failUnless(z.dtype == w.dtype) self.assertTrue(z.dtype == w.dtype)
#self.failUnless(z == w) #self.assertTrue(z == w)
self.failUnless(abs(z-w).nnz == 0) self.assertTrue(abs(z-w).nnz == 0)
z = z.todense() z = z.todense()
w = w.todense() w = w.todense()
self.failUnless((z == w).all() == True) self.assertTrue((z == w).all() == True)
def test_basicDS(self): def test_basicDS(self):
for mtype in _mtypes: for mtype in _mtypes:
x = as_sparse_variable(mtype((500,3))) x = as_sparse_variable(mtype((500,3)))
x.data[(10, 1)] = 1 x.data[(10, 1)] = 1
x.data[(20, 2)] = 2 x.data[(20, 2)] = 2
self.failUnless(_is_sparse_variable(x)) self.assertTrue(_is_sparse_variable(x))
y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]]) y = tensor.as_tensor_variable([[1., 2], [3, 4], [2, 1]])
self.failUnless(_is_dense_variable(y)) self.assertTrue(_is_dense_variable(y))
x.data = x.data.T x.data = x.data.T
y.data = y.data.T y.data = y.data.T
zop = true_dot(y, x) zop = true_dot(y, x)
zop = transpose(true_dot(y, x)) zop = transpose(true_dot(y, x))
self.failUnless(_is_sparse_variable(zop)) self.assertTrue(_is_sparse_variable(zop))
z = eval_outputs([zop]) z = eval_outputs([zop])
self.failUnless(_is_sparse(z)) self.assertTrue(_is_sparse(z))
self.failUnless(z.shape == (500,2)) self.assertTrue(z.shape == (500,2))
# self.failUnless(type(z) is mtype) # self.assertTrue(type(z) is mtype)
w = mtype((500,2)) w = mtype((500,2))
w[(10, 0)] = 3. w[(10, 0)] = 3.
w[(20, 0)] = 4 w[(20, 0)] = 4
w[(10, 1)] = 4 w[(10, 1)] = 4
w[(20, 1)] = 2 w[(20, 1)] = 2
self.failUnless(z.shape == w.shape) self.assertTrue(z.shape == w.shape)
# Type should switch from csr to csc and vice-versa, so don't perform this test # Type should switch from csr to csc and vice-versa, so don't perform this test
#self.failUnless(type(z) == type(w)) #self.assertTrue(type(z) == type(w))
self.failUnless(z.dtype == w.dtype) self.assertTrue(z.dtype == w.dtype)
# Type should switch from csr to csc and vice-versa, so don't perform this test # Type should switch from csr to csc and vice-versa, so don't perform this test
#self.failUnless(z == w) #self.assertTrue(z == w)
self.failUnless(abs(z-w).nnz == 0) self.assertTrue(abs(z-w).nnz == 0)
z = z.todense() z = z.todense()
w = w.todense() w = w.todense()
self.failUnless((z == w).all() == True) self.assertTrue((z == w).all() == True)
def test_graph_bprop0(self): def test_graph_bprop0(self):
for mtype in _mtypes: for mtype in _mtypes:
...@@ -213,8 +213,8 @@ class test_true_dot(unittest.TestCase): ...@@ -213,8 +213,8 @@ class test_true_dot(unittest.TestCase):
w = w - (lr * gw) w = w - (lr * gw)
print loss print loss
self.failUnless(origloss > loss) self.assertTrue(origloss > loss)
self.failUnless('1.05191241115' == str(loss)) self.assertTrue('1.05191241115' == str(loss))
def test_graph_bprop_rand(self): def test_graph_bprop_rand(self):
for i in range(10): for i in range(10):
...@@ -239,5 +239,5 @@ class test_true_dot(unittest.TestCase): ...@@ -239,5 +239,5 @@ class test_true_dot(unittest.TestCase):
y, loss, gw = trainfn(x, w) y, loss, gw = trainfn(x, w)
w = w - (lr * gw) w = w - (lr * gw)
self.failUnless(origloss > loss) self.assertTrue(origloss > loss)
...@@ -90,7 +90,7 @@ class TestConv2D(unittest.TestCase): ...@@ -90,7 +90,7 @@ class TestConv2D(unittest.TestCase):
icol:icol+N_filter_shape[3]]*filter2d[::-1,::-1] icol:icol+N_filter_shape[3]]*filter2d[::-1,::-1]
).sum() ).sum()
self.failUnless(_allclose(theano_output, ref_output)) self.assertTrue(_allclose(theano_output, ref_output))
############# TEST GRADIENT ############ ############# TEST GRADIENT ############
if verify_grad: if verify_grad:
...@@ -222,7 +222,7 @@ class TestConv2D(unittest.TestCase): ...@@ -222,7 +222,7 @@ class TestConv2D(unittest.TestCase):
""" """
def f(): def f():
self.validate((3,2,8,8), (4,3,5,5), 'valid') self.validate((3,2,8,8), (4,3,5,5), 'valid')
self.failUnlessRaises(AssertionError, f) self.assertRaises(AssertionError, f)
def test_missing_info(self): def test_missing_info(self):
""" """
...@@ -246,7 +246,7 @@ class TestConv2D(unittest.TestCase): ...@@ -246,7 +246,7 @@ class TestConv2D(unittest.TestCase):
self.validate((3,2,5,5), (4,2,8,8), 'full') self.validate((3,2,5,5), (4,2,8,8), 'full')
def f(): def f():
self.validate((3,2,5,5), (4,2,8,8), 'valid') self.validate((3,2,5,5), (4,2,8,8), 'valid')
self.failUnlessRaises(Exception, f) self.assertRaises(Exception, f)
def test_wrong_input(self): def test_wrong_input(self):
""" """
......
...@@ -213,8 +213,8 @@ class T_prepend(unittest.TestCase): ...@@ -213,8 +213,8 @@ class T_prepend(unittest.TestCase):
f=theano.function([x],[y]) f=theano.function([x],[y])
m=numpy.random.rand(3,5) m=numpy.random.rand(3,5)
my = f(m) my = f(m)
self.failUnless(my.shape == (3, 6), my.shape) self.assertTrue(my.shape == (3, 6), my.shape)
self.failUnless(numpy.all( my[:,0] == 4.0)) self.assertTrue(numpy.all( my[:,0] == 4.0))
class T_prepend(unittest.TestCase): class T_prepend(unittest.TestCase):
...@@ -225,8 +225,8 @@ class T_prepend(unittest.TestCase): ...@@ -225,8 +225,8 @@ class T_prepend(unittest.TestCase):
f=theano.function([x],y) f=theano.function([x],y)
m=numpy.ones((3,5),dtype="float32") m=numpy.ones((3,5),dtype="float32")
my = f(m) my = f(m)
self.failUnless(my.shape == (3, 6)) self.assertTrue(my.shape == (3, 6))
self.failUnless(numpy.all(my[:,0] == 5.0)) self.assertTrue(numpy.all(my[:,0] == 5.0))
class T_CrossentropyCategorical1Hot(unittest.TestCase): class T_CrossentropyCategorical1Hot(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -66,7 +66,7 @@ class TestSignalConv2D(unittest.TestCase): ...@@ -66,7 +66,7 @@ class TestSignalConv2D(unittest.TestCase):
).sum() ).sum()
self.failUnless(_allclose(theano_output4d[b,k,:,:], output2d)) self.assertTrue(_allclose(theano_output4d[b,k,:,:], output2d))
############# TEST GRADIENT ############ ############# TEST GRADIENT ############
if verify_grad: if verify_grad:
......
...@@ -67,11 +67,11 @@ class t_gemm(TestCase): ...@@ -67,11 +67,11 @@ class t_gemm(TestCase):
#print z_orig, z_after, z, type(z_orig), type(z_after), type(z) #print z_orig, z_after, z, type(z_orig), type(z_after), type(z)
#_approx_eq.debug = 1 #_approx_eq.debug = 1
self.failUnless(_approx_eq(z_after, z)) self.assertTrue(_approx_eq(z_after, z))
if a == 0.0 and b == 1.0: if a == 0.0 and b == 1.0:
return return
else: else:
self.failIf(numpy.all(z_orig == z)) self.assertFalse(numpy.all(z_orig == z))
cmp_linker(copy(z), a, x, y, b, 'c|py') cmp_linker(copy(z), a, x, y, b, 'c|py')
cmp_linker(copy(z), a, x, y, b, 'py') cmp_linker(copy(z), a, x, y, b, 'py')
...@@ -100,7 +100,7 @@ class t_gemm(TestCase): ...@@ -100,7 +100,7 @@ class t_gemm(TestCase):
try: try:
self.cmp(2., 1.0, [3,2,1.], [[1],[2],[3.]], 1.0) self.cmp(2., 1.0, [3,2,1.], [[1],[2],[3.]], 1.0)
except ValueError, e: except ValueError, e:
self.failUnless(e[0] == Gemm.E_rank) self.assertTrue(e[0] == Gemm.E_rank)
return return
self.fail() self.fail()
def test4(self): def test4(self):
...@@ -217,11 +217,11 @@ class t_gemm(TestCase): ...@@ -217,11 +217,11 @@ class t_gemm(TestCase):
#f(z, a, x, y, b) #f(z, a, x, y, b)
f = inplace_func([], gemm_inplace(tz,ta,tx,ty,tb), mode = compile.Mode(optimizer = None, linker=l)) f = inplace_func([], gemm_inplace(tz,ta,tx,ty,tb), mode = compile.Mode(optimizer = None, linker=l))
f() f()
self.failUnless(_approx_eq(z_after, tz.get_value(borrow=True)), (z_orig, z_after, z, z_after - z)) self.assertTrue(_approx_eq(z_after, tz.get_value(borrow=True)), (z_orig, z_after, z, z_after - z))
f() f()
self.failUnless(_approx_eq(z_after, tz.get_value(borrow=True)), (z_orig, z_after, z, z_after - z)) self.assertTrue(_approx_eq(z_after, tz.get_value(borrow=True)), (z_orig, z_after, z, z_after - z))
f() f()
self.failUnless(_approx_eq(z_after, tz.get_value(borrow=True)), (z_orig, z_after, z, z_after - z)) self.assertTrue(_approx_eq(z_after, tz.get_value(borrow=True)), (z_orig, z_after, z, z_after - z))
#tz.value *= 0 # clear z's value #tz.value *= 0 # clear z's value
y_T = ty.get_value(borrow=True).T y_T = ty.get_value(borrow=True).T
...@@ -230,7 +230,7 @@ class t_gemm(TestCase): ...@@ -230,7 +230,7 @@ class t_gemm(TestCase):
f() f()
# test that the transposed version of multiplication gives same answer # test that the transposed version of multiplication gives same answer
self.failUnless(_approx_eq(z_after, tz.get_value(borrow=True).T)) self.assertTrue(_approx_eq(z_after, tz.get_value(borrow=True).T))
t(C,A,B) t(C,A,B)
t(C.T, A, B) t(C.T, A, B)
...@@ -275,17 +275,17 @@ class t_as_scalar(TestCase): ...@@ -275,17 +275,17 @@ class t_as_scalar(TestCase):
d_b = T.DimShuffle([True, True, True], [0,2,1])(b) d_b = T.DimShuffle([True, True, True], [0,2,1])(b)
d_a2 = T.DimShuffle([], ['x', 'x', 'x'])(a) d_a2 = T.DimShuffle([], ['x', 'x', 'x'])(a)
self.failUnless(_as_scalar(a) == a) self.assertTrue(_as_scalar(a) == a)
self.failUnless(_as_scalar(b) != b) self.assertTrue(_as_scalar(b) != b)
self.failUnless(_as_scalar(d_a) != d_a) self.assertTrue(_as_scalar(d_a) != d_a)
self.failUnless(_as_scalar(d_b) != d_b) self.assertTrue(_as_scalar(d_b) != d_b)
self.failUnless(_as_scalar(d_a2) != d_a2) self.assertTrue(_as_scalar(d_a2) != d_a2)
def test1(self): def test1(self):
"""Test that it fails on nonscalar constants""" """Test that it fails on nonscalar constants"""
a = T.constant(numpy.ones(5)) a = T.constant(numpy.ones(5))
self.failUnless(None == _as_scalar(a)) self.assertTrue(None == _as_scalar(a))
self.failUnless(None == _as_scalar(T.DimShuffle([False], [0,'x'])(a))) self.assertTrue(None == _as_scalar(T.DimShuffle([False], [0,'x'])(a)))
def test2(self): def test2(self):
"""Test that it works on scalar variables""" """Test that it works on scalar variables"""
...@@ -293,20 +293,20 @@ class t_as_scalar(TestCase): ...@@ -293,20 +293,20 @@ class t_as_scalar(TestCase):
d_a = T.DimShuffle([], [])(a) d_a = T.DimShuffle([], [])(a)
d_a2 = T.DimShuffle([], ['x', 'x'])(a) d_a2 = T.DimShuffle([], ['x', 'x'])(a)
self.failUnless(_as_scalar(a) is a) self.assertTrue(_as_scalar(a) is a)
self.failUnless(_as_scalar(d_a) is a) self.assertTrue(_as_scalar(d_a) is a)
self.failUnless(_as_scalar(d_a2) is a) self.assertTrue(_as_scalar(d_a2) is a)
def test3(self): def test3(self):
"""Test that it fails on nonscalar variables""" """Test that it fails on nonscalar variables"""
a = T.dmatrix() a = T.dmatrix()
self.failUnless(None == _as_scalar(a)) self.assertTrue(None == _as_scalar(a))
self.failUnless(None == _as_scalar(T.DimShuffle([False, False], [0,'x', 1])(a))) self.assertTrue(None == _as_scalar(T.DimShuffle([False, False], [0,'x', 1])(a)))
class T_real_matrix(TestCase): class T_real_matrix(TestCase):
def test0(self): def test0(self):
self.failUnless(_is_real_matrix(T.DimShuffle([False,False], [1, 0])(T.dmatrix()))) self.assertTrue(_is_real_matrix(T.DimShuffle([False,False], [1, 0])(T.dmatrix())))
self.failUnless(not _is_real_matrix(T.DimShuffle([False], ['x', 0])(T.dvector()))) self.assertTrue(not _is_real_matrix(T.DimShuffle([False], ['x', 0])(T.dvector())))
def fail(msg): def fail(msg):
print 'FAIL', msg print 'FAIL', msg
......
...@@ -36,7 +36,7 @@ class test_casting(unittest.TestCase): ...@@ -36,7 +36,7 @@ class test_casting(unittest.TestCase):
f = function([compile.In(x, strict = True)], y) f = function([compile.In(x, strict = True)], y)
a = numpy.arange(10, dtype = type1) a = numpy.arange(10, dtype = type1)
b = f(a) b = f(a)
self.failUnless(numpy.all(b == numpy.arange(10, dtype = type2))) self.assertTrue(numpy.all(b == numpy.arange(10, dtype = type2)))
def test_convert_to_complex(self): def test_convert_to_complex(self):
a = value(numpy.ones(3, dtype='complex64')+0.5j) a = value(numpy.ones(3, dtype='complex64')+0.5j)
......
...@@ -29,7 +29,7 @@ class TestRealImag(unittest.TestCase): ...@@ -29,7 +29,7 @@ class TestRealImag(unittest.TestCase):
def test_cast(self): def test_cast(self):
x= zvector() x= zvector()
self.failUnlessRaises(TypeError, cast, x, 'int32') self.assertRaises(TypeError, cast, x, 'int32')
def test_complex(self): def test_complex(self):
rng = numpy.random.RandomState(2333) rng = numpy.random.RandomState(2333)
......
...@@ -81,7 +81,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -81,7 +81,7 @@ class test_Broadcast(unittest.TestCase):
yv = numpy.asarray(numpy.random.rand(*ysh)) yv = numpy.asarray(numpy.random.rand(*ysh))
zv = xv + yv zv = xv + yv
self.failUnless((f(xv, yv) == zv).all()) self.assertTrue((f(xv, yv) == zv).all())
#test CAReduce.infer_shape #test CAReduce.infer_shape
#the Shape op don't implement c_code! #the Shape op don't implement c_code!
...@@ -111,7 +111,7 @@ class test_Broadcast(unittest.TestCase): ...@@ -111,7 +111,7 @@ class test_Broadcast(unittest.TestCase):
f(xv, yv) f(xv, yv)
self.failUnless((xv == zv).all()) self.assertTrue((xv == zv).all())
#test CAReduce.infer_shape #test CAReduce.infer_shape
#the Shape op don't implement c_code! #the Shape op don't implement c_code!
if isinstance(linker,gof.PerformLinker): if isinstance(linker,gof.PerformLinker):
...@@ -238,7 +238,7 @@ class test_CAReduce(unittest.TestCase): ...@@ -238,7 +238,7 @@ class test_CAReduce(unittest.TestCase):
else: else:
self.fail() self.fail()
else: else:
self.failUnless((numpy.abs(f(xv) - zv) < 1e-10).all()) self.assertTrue((numpy.abs(f(xv) - zv) < 1e-10).all())
#test CAReduce.infer_shape #test CAReduce.infer_shape
......
...@@ -51,7 +51,7 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -51,7 +51,7 @@ class Test_inc_subtensor(unittest.TestCase):
else: else:
expected_result[:,:val_sl2_end] += val_inc expected_result[:,:val_sl2_end] += val_inc
self.failUnless(numpy.array_equal(result, expected_result)) self.assertTrue(numpy.array_equal(result, expected_result))
return return
def test_grad(self): def test_grad(self):
......
...@@ -48,7 +48,7 @@ class Test_incsubtensor(unittest.TestCase): ...@@ -48,7 +48,7 @@ class Test_incsubtensor(unittest.TestCase):
else: else:
expected_result[:,:val_sl2_end] += val_inc expected_result[:,:val_sl2_end] += val_inc
self.failUnless(numpy.array_equal(result, expected_result)) self.assertTrue(numpy.array_equal(result, expected_result))
return return
def test_grad(self): def test_grad(self):
......
...@@ -49,35 +49,35 @@ class test_dimshuffle_lift(unittest.TestCase): ...@@ -49,35 +49,35 @@ class test_dimshuffle_lift(unittest.TestCase):
x, y, z = inputs() x, y, z = inputs()
e = ds(ds(x, (1, 0)), (1, 0)) e = ds(ds(x, (1, 0)), (1, 0))
g = Env([x], [e]) g = Env([x], [e])
self.failUnless(str(g) == "[DimShuffle{1,0}(DimShuffle{1,0}(x))]") self.assertTrue(str(g) == "[DimShuffle{1,0}(DimShuffle{1,0}(x))]")
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.failUnless(str(g) == "[x]") self.assertTrue(str(g) == "[x]")
def test_merge2(self): def test_merge2(self):
x, y, z = inputs() x, y, z = inputs()
e = ds(ds(x, (1, 'x', 0)), (2, 0, 'x', 1)) e = ds(ds(x, (1, 'x', 0)), (2, 0, 'x', 1))
g = Env([x], [e]) g = Env([x], [e])
self.failUnless(str(g) == "[DimShuffle{2,0,x,1}(DimShuffle{1,x,0}(x))]", str(g)) self.assertTrue(str(g) == "[DimShuffle{2,0,x,1}(DimShuffle{1,x,0}(x))]", str(g))
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.failUnless(str(g) == "[DimShuffle{0,1,x,x}(x)]", str(g)) self.assertTrue(str(g) == "[DimShuffle{0,1,x,x}(x)]", str(g))
def test_elim3(self): def test_elim3(self):
x, y, z = inputs() x, y, z = inputs()
e = ds(ds(ds(x, (0, 'x', 1)), (2, 0, 'x', 1)), (1, 0)) e = ds(ds(ds(x, (0, 'x', 1)), (2, 0, 'x', 1)), (1, 0))
g = Env([x], [e]) g = Env([x], [e])
self.failUnless(str(g) == "[DimShuffle{1,0}(DimShuffle{2,0,x,1}(DimShuffle{0,x,1}(x)))]", str(g)) self.assertTrue(str(g) == "[DimShuffle{1,0}(DimShuffle{2,0,x,1}(DimShuffle{0,x,1}(x)))]", str(g))
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.failUnless(str(g) == "[x]", str(g)) self.assertTrue(str(g) == "[x]", str(g))
def test_lift(self): def test_lift(self):
x, y, z = inputs([False]*1, [False]*2, [False]*3) x, y, z = inputs([False]*1, [False]*2, [False]*3)
e = x + y + z e = x + y + z
g = Env([x, y, z], [e]) g = Env([x, y, z], [e])
self.failUnless(str(g) == ("[Elemwise{add,no_inplace}(" self.assertTrue(str(g) == ("[Elemwise{add,no_inplace}("
"InplaceDimShuffle{x,0,1}(Elemwise{add,no_inplace}" "InplaceDimShuffle{x,0,1}(Elemwise{add,no_inplace}"
"(InplaceDimShuffle{x,0}(x), y)), z)]"), str(g)) "(InplaceDimShuffle{x,0}(x), y)), z)]"), str(g))
dimshuffle_lift.optimize(g) dimshuffle_lift.optimize(g)
self.failUnless(str(g) == ("[Elemwise{add,no_inplace}(Elemwise" self.assertTrue(str(g) == ("[Elemwise{add,no_inplace}(Elemwise"
"{add,no_inplace}(InplaceDimShuffle{x,x,0}(x), InplaceDimShuffle" "{add,no_inplace}(InplaceDimShuffle{x,x,0}(x), InplaceDimShuffle"
"{x,0,1}(y)), z)]"), str(g)) "{x,0,1}(y)), z)]"), str(g))
...@@ -1699,7 +1699,7 @@ class test_assert(unittest.TestCase): ...@@ -1699,7 +1699,7 @@ class test_assert(unittest.TestCase):
y=T.scalar() y=T.scalar()
f = theano.function([x,y],theano.tensor.opt.assert_(x,T.eq(x,y))) f = theano.function([x,y],theano.tensor.opt.assert_(x,T.eq(x,y)))
f(1,1) f(1,1)
self.failUnlessRaises(AssertionError, f, 1,0) self.assertRaises(AssertionError, f, 1,0)
def test1(self): def test1(self):
#remove assert that are always true #remove assert that are always true
...@@ -1743,7 +1743,7 @@ class test_assert(unittest.TestCase): ...@@ -1743,7 +1743,7 @@ class test_assert(unittest.TestCase):
x=T.scalar() x=T.scalar()
y=T.scalar() y=T.scalar()
f = theano.function([x,y],theano.tensor.opt.assert_(x,y,0),mode=mode) f = theano.function([x,y],theano.tensor.opt.assert_(x,y,0),mode=mode)
self.failUnlessRaises(AssertionError, f, 1,0) self.assertRaises(AssertionError, f, 1,0)
topo=f.maker.env.toposort() topo=f.maker.env.toposort()
assert len(topo)==2 assert len(topo)==2
assert len(topo[0].inputs)==3 assert len(topo[0].inputs)==3
......
...@@ -18,7 +18,7 @@ class T_XlogX(unittest.TestCase): ...@@ -18,7 +18,7 @@ class T_XlogX(unittest.TestCase):
x = as_tensor_variable([1, 0]) x = as_tensor_variable([1, 0])
y = xlogx(x) y = xlogx(x)
f = theano.function([], [y]) f = theano.function([], [y])
self.failUnless(numpy.all(f() == numpy.asarray([0, 0.]))) self.assertTrue(numpy.all(f() == numpy.asarray([0, 0.])))
def test1(self): def test1(self):
# class Dummy(object): # class Dummy(object):
# def make_node(self, a): # def make_node(self, a):
...@@ -36,7 +36,7 @@ class T_XlogY0(unittest.TestCase): ...@@ -36,7 +36,7 @@ class T_XlogY0(unittest.TestCase):
y = as_tensor_variable([1, 0]) y = as_tensor_variable([1, 0])
z = xlogy0(x, y) z = xlogy0(x, y)
f = theano.function([], z) f = theano.function([], z)
self.failUnless(numpy.all(f() == numpy.asarray([0, 0.]))) self.assertTrue(numpy.all(f() == numpy.asarray([0, 0.])))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -30,7 +30,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -30,7 +30,7 @@ class test_grad_sources_inputs(unittest.TestCase):
try: try:
_grad_sources_inputs([(a.out, 1)], None) _grad_sources_inputs([(a.out, 1)], None)
except ValueError, e: except ValueError, e:
self.failUnless(e[0] is gradient._msg_retType) self.assertTrue(e[0] is gradient._msg_retType)
return return
self.fail() self.fail()
def test_retNone1_b(self): def test_retNone1_b(self):
...@@ -44,7 +44,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -44,7 +44,7 @@ class test_grad_sources_inputs(unittest.TestCase):
i = gof.generic() i = gof.generic()
a = retNone().make_node(i) a = retNone().make_node(i)
g = _grad_sources_inputs([(a.out, 1)], None) g = _grad_sources_inputs([(a.out, 1)], None)
self.failUnless(not i in g) self.assertTrue(not i in g)
def test_wrong_rval_len1(self): def test_wrong_rval_len1(self):
"""Test that it is not ok to return the wrong number of gradients""" """Test that it is not ok to return the wrong number of gradients"""
...@@ -63,7 +63,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -63,7 +63,7 @@ class test_grad_sources_inputs(unittest.TestCase):
try: try:
g = _grad_sources_inputs([(a2.out, 1)], None) g = _grad_sources_inputs([(a2.out, 1)], None)
except ValueError, e: except ValueError, e:
self.failUnless(e[0] is gradient._msg_badlen) self.assertTrue(e[0] is gradient._msg_badlen)
return return
self.fail() self.fail()
...@@ -95,7 +95,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -95,7 +95,7 @@ class test_grad_sources_inputs(unittest.TestCase):
return gval, return gval,
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
self.failUnless(g[a1.inputs[0]] is gval) self.assertTrue(g[a1.inputs[0]] is gval)
def test_1in_Nout(self): def test_1in_Nout(self):
"""Test grad is called correctly for a 1-to-many op""" """Test grad is called correctly for a 1-to-many op"""
...@@ -111,7 +111,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -111,7 +111,7 @@ class test_grad_sources_inputs(unittest.TestCase):
return gval, return gval,
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
self.failUnless(g[a1.inputs[0]] is gval) self.assertTrue(g[a1.inputs[0]] is gval)
def test_Nin_1out(self): def test_Nin_1out(self):
"""Test grad is called correctly for a many-to-1 op""" """Test grad is called correctly for a many-to-1 op"""
gval0 = gof.generic() gval0 = gof.generic()
...@@ -127,8 +127,8 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -127,8 +127,8 @@ class test_grad_sources_inputs(unittest.TestCase):
return (gval0, gval1) return (gval0, gval1)
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
self.failUnless(g[a1.inputs[0]] is gval0) self.assertTrue(g[a1.inputs[0]] is gval0)
self.failUnless(g[a1.inputs[1]] is gval1) self.assertTrue(g[a1.inputs[1]] is gval1)
def test_Nin_Nout(self): def test_Nin_Nout(self):
"""Test grad is called correctly for a many-to-many op""" """Test grad is called correctly for a many-to-many op"""
gval0 = gof.generic() gval0 = gof.generic()
...@@ -142,8 +142,8 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -142,8 +142,8 @@ class test_grad_sources_inputs(unittest.TestCase):
return gval0, gval1 return gval0, gval1
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], 1)], None)
self.failUnless(g[a1.inputs[0]] is gval0) self.assertTrue(g[a1.inputs[0]] is gval0)
self.failUnless(g[a1.inputs[1]] is gval1) self.assertTrue(g[a1.inputs[1]] is gval1)
def test_some_None_ograds(self): def test_some_None_ograds(self):
"""Test grad is called when some output gradients are None""" """Test grad is called when some output gradients are None"""
class O(gof.op.Op): class O(gof.op.Op):
...@@ -157,7 +157,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -157,7 +157,7 @@ class test_grad_sources_inputs(unittest.TestCase):
i = gof.generic() i = gof.generic()
a1 = O(self).make_node(i) a1 = O(self).make_node(i)
g = grad_sources_inputs([(a1.outputs[0], 1)], None, warn_type=False) g = grad_sources_inputs([(a1.outputs[0], 1)], None, warn_type=False)
self.failUnless(g[i] is 1) self.assertTrue(g[i] is 1)
def test_some_None_igrads(self): def test_some_None_igrads(self):
"""Test that traversal works properly when an op return some None""" """Test that traversal works properly when an op return some None"""
...@@ -179,12 +179,12 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -179,12 +179,12 @@ class test_grad_sources_inputs(unittest.TestCase):
a1 = O(self, True).make_node(i,j) a1 = O(self, True).make_node(i,j)
a2 = O(self, True).make_node(a1.outputs[1], k) a2 = O(self, True).make_node(a1.outputs[1], k)
g = grad_sources_inputs([(a2.outputs[0], 1)], None, warn_type=False) g = grad_sources_inputs([(a2.outputs[0], 1)], None, warn_type=False)
self.failUnless(g[i] is 1 and j not in g and k not in g) self.assertTrue(g[i] is 1 and j not in g and k not in g)
a1 = O(self, True).make_node(i,j) a1 = O(self, True).make_node(i,j)
a2 = O(self, True).make_node(k, a1.outputs[1]) a2 = O(self, True).make_node(k, a1.outputs[1])
g = _grad_sources_inputs([(a2.outputs[0], 1)], None) g = _grad_sources_inputs([(a2.outputs[0], 1)], None)
self.failUnless(g[k] is 1 and i not in g and j not in g) self.assertTrue(g[k] is 1 and i not in g and j not in g)
def test_inputs(self): def test_inputs(self):
"""Test that passing inputs shortens the traversal""" """Test that passing inputs shortens the traversal"""
...@@ -211,12 +211,12 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -211,12 +211,12 @@ class test_grad_sources_inputs(unittest.TestCase):
a2 = O(self, True).make_node(k,a1.outputs[1]) a2 = O(self, True).make_node(k,a1.outputs[1])
g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4), g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4),
(a1.outputs[0], 3), (a1.outputs[0], 3)], a1.outputs) (a1.outputs[0], 3), (a1.outputs[0], 3)], a1.outputs)
self.failUnless(g[a2.inputs[0]] == 1) self.assertTrue(g[a2.inputs[0]] == 1)
self.failUnless(g[a2.inputs[1]] == 5) self.assertTrue(g[a2.inputs[1]] == 5)
self.failUnless(g[a1.outputs[0]] == 6) self.assertTrue(g[a1.outputs[0]] == 6)
self.failUnless(g[a1.outputs[1]] == 5) self.assertTrue(g[a1.outputs[1]] == 5)
self.failUnless(a1.inputs[0] not in g) self.assertTrue(a1.inputs[0] not in g)
self.failUnless(a1.inputs[1] not in g) self.assertTrue(a1.inputs[1] not in g)
def test_multiple_sources(self): def test_multiple_sources(self):
"""Test that passing multiple sources works""" """Test that passing multiple sources works"""
...@@ -243,12 +243,12 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -243,12 +243,12 @@ class test_grad_sources_inputs(unittest.TestCase):
a2 = O(self,True).make_node(k,a1.outputs[1]) a2 = O(self,True).make_node(k,a1.outputs[1])
g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4), g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4),
(a1.outputs[0], 3), (a1.outputs[0], 3)], None) (a1.outputs[0], 3), (a1.outputs[0], 3)], None)
self.failUnless(g[a2.inputs[0]] == 1) self.assertTrue(g[a2.inputs[0]] == 1)
self.failUnless(g[a2.inputs[1]] == 5) self.assertTrue(g[a2.inputs[1]] == 5)
self.failUnless(g[a1.outputs[0]] == 6) self.assertTrue(g[a1.outputs[0]] == 6)
self.failUnless(g[a1.outputs[1]] == 5) self.assertTrue(g[a1.outputs[1]] == 5)
self.failUnless(g[a1.inputs[0]] == 6) self.assertTrue(g[a1.inputs[0]] == 6)
self.failUnless(g[a1.inputs[1]] == 11) self.assertTrue(g[a1.inputs[1]] == 11)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -106,7 +106,7 @@ class T_extending(unittest.TestCase): ...@@ -106,7 +106,7 @@ class T_extending(unittest.TestCase):
assert f(5.6, 6.7) == 37.519999999999996 assert f(5.6, 6.7) == 37.519999999999996
x = double('x') x = double('x')
self.failUnlessRaises(AttributeError, mul, x, 2) self.assertRaises(AttributeError, mul, x, 2)
def make_node(x, y): def make_node(x, y):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论