提交 67185ec0 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Refactor tests into class, use self.assertRaises

上级 5f82bcfe
...@@ -32,11 +32,7 @@ class testgrad_sources_inputs(unittest.TestCase): ...@@ -32,11 +32,7 @@ class testgrad_sources_inputs(unittest.TestCase):
gz, = grads gz, = grads
pass pass
a = retNone().make_node() a = retNone().make_node()
try: self.assertRaises(TypeError, grad_sources_inputs, [(a.out, one)], None)
grad_sources_inputs([(a.out, one)], None)
except TypeError, e:
return
self.fail()
def test_wrong_rval_len1(self): def test_wrong_rval_len1(self):
"""Test that it is not ok to return the wrong number of gradient terms""" """Test that it is not ok to return the wrong number of gradient terms"""
...@@ -53,11 +49,8 @@ class testgrad_sources_inputs(unittest.TestCase): ...@@ -53,11 +49,8 @@ class testgrad_sources_inputs(unittest.TestCase):
a1 = retOne().make_node(i) a1 = retOne().make_node(i)
g = grad_sources_inputs([(a1.out, one)], None) g = grad_sources_inputs([(a1.out, one)], None)
a2 = retOne().make_node(i, j) a2 = retOne().make_node(i, j)
try: self.assertRaises(ValueError, grad_sources_inputs,
g = grad_sources_inputs([(a2.out, one)], None) [(a2.out, one)], None)
except ValueError, e:
return
self.fail()
def test_1in_1out(self): def test_1in_1out(self):
"""Test grad is called correctly for a 1-to-1 op""" """Test grad is called correctly for a 1-to-1 op"""
...@@ -132,29 +125,24 @@ class testgrad_sources_inputs(unittest.TestCase): ...@@ -132,29 +125,24 @@ class testgrad_sources_inputs(unittest.TestCase):
self.assertTrue(g[a1.inputs[1]] is gval1) self.assertTrue(g[a1.inputs[1]] is gval1)
def test_unimplemented_grad_func(): class test_grad(unittest.TestCase):
# tests that function compilation catches unimplemented grads in the graph
def test_unimplemented_grad_func(self):
# tests that function compilation catches unimplemented grads
# in the graph
a = theano.tensor.vector() a = theano.tensor.vector()
b = theano.gradient.grad_not_implemented(theano.tensor.add, 0, a) b = theano.gradient.grad_not_implemented(theano.tensor.add, 0, a)
try: self.assertRaises(TypeError, theano.function,
f = theano.function([a], b, on_unused_input='ignore') [a], b, on_unused_input='ignore')
assert 0
except TypeError:
pass
def test_undefined_grad_func(): def test_undefined_grad_func(self):
#tests that function compilation catches undefined grads in the graph #tests that function compilation catches undefined grads in the graph
a = theano.tensor.vector() a = theano.tensor.vector()
b = theano.gradient.grad_undefined(theano.tensor.add, 0, a) b = theano.gradient.grad_undefined(theano.tensor.add, 0, a)
try: self.assertRaises(TypeError, theano.function,
f = theano.function([a], b, on_unused_input='ignore') [a], b, on_unused_input='ignore')
assert 0
except TypeError:
pass
def test_unimplemented_grad_grad(): def test_unimplemented_grad_grad(self):
#tests that unimplemented grads are caught in the grad method #tests that unimplemented grads are caught in the grad method
class DummyOp(gof.Op): class DummyOp(gof.Op):
...@@ -162,19 +150,15 @@ def test_unimplemented_grad_grad(): ...@@ -162,19 +150,15 @@ def test_unimplemented_grad_grad():
return gof.Apply(self, [x], [x.type()]) return gof.Apply(self, [x], [x.type()])
def grad(self, inputs, output_grads): def grad(self, inputs, output_grads):
return [theano.gradient.grad_not_implemented(self, 0, inputs[0])] return [theano.gradient.grad_not_implemented(
self, 0, inputs[0])]
a = theano.tensor.scalar() a = theano.tensor.scalar()
b = DummyOp()(a) b = DummyOp()(a)
try: self.assertRaises(TypeError, theano.gradient.grad, b, a)
g = theano.gradient.grad(b, a)
assert False
except TypeError:
pass
def test_undefined_grad_grad(): def test_undefined_grad_grad(self):
#tests that undefined grads are caught in the grad method #tests that undefined grads are caught in the grad method
V = theano.tensor.TensorType(dtype=config.floatX, V = theano.tensor.TensorType(dtype=config.floatX,
...@@ -186,14 +170,9 @@ def test_undefined_grad_grad(): ...@@ -186,14 +170,9 @@ def test_undefined_grad_grad():
Z = conv3D(V, W, b, d) Z = conv3D(V, W, b, d)
try: self.assertRaises(TypeError, theano.gradient.grad, Z.sum(), d)
g = theano.gradient.grad(Z.sum(), d)
assert False
except TypeError:
pass
def test_grad_name(self):
def test_grad_name():
A = theano.tensor.matrix('A') A = theano.tensor.matrix('A')
x = theano.tensor.vector('x') x = theano.tensor.vector('x')
f = theano.tensor.dot(x, theano.tensor.dot(A, x)) f = theano.tensor.dot(x, theano.tensor.dot(A, x))
...@@ -201,8 +180,7 @@ def test_grad_name(): ...@@ -201,8 +180,7 @@ def test_grad_name():
g = theano.tensor.grad(f, x) g = theano.tensor.grad(f, x)
assert g.name == '(df/dx)' assert g.name == '(df/dx)'
def test_grad_duplicate_input(self):
def test_grad_duplicate_input():
#test that the grad works when a variable #test that the grad works when a variable
#appears in more than one place in a node's input list #appears in more than one place in a node's input list
...@@ -216,8 +194,7 @@ def test_grad_duplicate_input(): ...@@ -216,8 +194,7 @@ def test_grad_duplicate_input():
theano.tests.unittest_tools.verify_grad(output, [vx]) theano.tests.unittest_tools.verify_grad(output, [vx])
def test_grad_quadratic(self):
def test_grad_quadratic():
#test the gradient on a tiny graph #test the gradient on a tiny graph
...@@ -231,8 +208,7 @@ def test_grad_quadratic(): ...@@ -231,8 +208,7 @@ def test_grad_quadratic():
theano.tests.unittest_tools.verify_grad(cost, [vx, vA]) theano.tests.unittest_tools.verify_grad(cost, [vx, vA])
def test_grad_quadratic_vector(self):
def test_grad_quadratic_vector():
#test the gradient on a small graph #test the gradient on a small graph
...@@ -246,8 +222,7 @@ def test_grad_quadratic_vector(): ...@@ -246,8 +222,7 @@ def test_grad_quadratic_vector():
theano.tests.unittest_tools.verify_grad(output, [vx, vA]) theano.tests.unittest_tools.verify_grad(output, [vx, vA])
def test_grad_cubic(self):
def test_grad_cubic():
#test the gradient on a bigger graph #test the gradient on a bigger graph
...@@ -261,8 +236,7 @@ def test_grad_cubic(): ...@@ -261,8 +236,7 @@ def test_grad_cubic():
theano.tests.unittest_tools.verify_grad(cost, [vx, vA]) theano.tests.unittest_tools.verify_grad(cost, [vx, vA])
def test_grad_grad_quadratic(self):
def test_grad_grad_quadratic():
#test the gradient on a graph constructed using the gradient #test the gradient on a graph constructed using the gradient
...@@ -277,8 +251,7 @@ def test_grad_grad_quadratic(): ...@@ -277,8 +251,7 @@ def test_grad_grad_quadratic():
theano.tests.unittest_tools.verify_grad(output, [vx, vA]) theano.tests.unittest_tools.verify_grad(output, [vx, vA])
def test_grad_grad_cubic(self):
def test_grad_grad_cubic():
#test the gradient on a bigger graph constructed using the gradient #test the gradient on a bigger graph constructed using the gradient
...@@ -293,8 +266,7 @@ def test_grad_grad_cubic(): ...@@ -293,8 +266,7 @@ def test_grad_grad_cubic():
theano.tests.unittest_tools.verify_grad(output, [vx, vA]) theano.tests.unittest_tools.verify_grad(output, [vx, vA])
def test_grad_int(self):
def test_grad_int():
# tests that the gradient with respect to an integer # tests that the gradient with respect to an integer
# is the same as the gradient with respect to a float # is the same as the gradient with respect to a float
...@@ -311,7 +283,8 @@ def test_grad_int(): ...@@ -311,7 +283,8 @@ def test_grad_int():
int_func = make_grad_func(theano.tensor.imatrix()) int_func = make_grad_func(theano.tensor.imatrix())
#we have to use float64 as the float type to get the results to match #we have to use float64 as the float type to get the results to match
#using an integer for the input makes all the later functions use float64 #using an integer for the input makes all the later functions use
#float64
float_func = make_grad_func(theano.tensor.matrix(dtype='float64')) float_func = make_grad_func(theano.tensor.matrix(dtype='float64'))
m = 5 m = 5
...@@ -329,10 +302,10 @@ def test_grad_int(): ...@@ -329,10 +302,10 @@ def test_grad_int():
int_result = int_func(X, W, b) int_result = int_func(X, W, b)
float_result = float_func(np.cast[float_type](X), W, b) float_result = float_func(np.cast[float_type](X), W, b)
assert np.allclose(int_result, float_result), (int_result, float_result) assert np.allclose(int_result, float_result), (
int_result, float_result)
def test_grad_disconnected(): def test_grad_disconnected(self):
#tests corner cases of gradient for shape and alloc #tests corner cases of gradient for shape and alloc
...@@ -347,15 +320,15 @@ def test_grad_disconnected(): ...@@ -347,15 +320,15 @@ def test_grad_disconnected():
cost.name = 'cost' cost.name = 'cost'
#note that cost simplifies to be the same as "total" #note that cost simplifies to be the same as "total"
g = gradient.grad(cost, x, add_names=False) g = gradient.grad(cost, x, add_names=False)
#we still need to pass in x because it determines the shape of the output #we still need to pass in x because it determines the shape of
#the output
f = theano.function([x], g) f = theano.function([x], g)
rng = np.random.RandomState([2012, 9, 5]) rng = np.random.RandomState([2012, 9, 5])
x = np.cast[x.dtype](rng.randn(3)) x = np.cast[x.dtype](rng.randn(3))
g = f(x) g = f(x)
assert np.allclose(g, np.ones(x.shape, dtype=x.dtype)) assert np.allclose(g, np.ones(x.shape, dtype=x.dtype))
def test_disconnected_nan(self):
def test_disconnected_nan():
# test that connection_pattern can prevent getting NaN # test that connection_pattern can prevent getting NaN
...@@ -396,8 +369,7 @@ def test_disconnected_nan(): ...@@ -396,8 +369,7 @@ def test_disconnected_nan():
# If we made it to here without an exception, then the # If we made it to here without an exception, then the
# connection_pattern functionality worked correctly # connection_pattern functionality worked correctly
def test_sum_disconnected(self):
def test_sum_disconnected():
# Tests that we can add DisconnectedType to other terms correctly # Tests that we can add DisconnectedType to other terms correctly
x = theano.tensor.scalar() x = theano.tensor.scalar()
...@@ -408,5 +380,6 @@ def test_sum_disconnected(): ...@@ -408,5 +380,6 @@ def test_sum_disconnected():
# In an earlier version of theano, the above line would have failed # In an earlier version of theano, the above line would have failed
# while trying to add two DisconnectedTypes # while trying to add two DisconnectedTypes
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论