提交 cc54c4ef authored 作者: James Bergstra's avatar James Bergstra

pep8 MakeTester in the test suite

上级 1451d214
...@@ -194,23 +194,26 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -194,23 +194,26 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
node = safe_make_node(self.op, *inputrs) node = safe_make_node(self.op, *inputrs)
except Exception: except Exception:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while making a node with inputs %s" \ err_msg = ("Test %s::%s: Error occurred while"
% (self.op, testname, inputs) " making a node with inputs %s") % (
self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc_value.args = exc_value.args + (err_msg, )
raise type, exc_value, traceback raise type(exc_value, traceback)
try: try:
f = inplace_func(inputrs, node.outputs, mode=mode) f = inplace_func(inputrs, node.outputs, mode=mode)
except Exception: except Exception:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while trying to make a Function" \ err_msg = ("Test %s::%s: Error occurred while"
% (self.op, testname) " trying to make a Function") % (self.op, testname)
exc_value.args = exc_value.args + (err_msg, ) exc_value.args = exc_value.args + (err_msg, )
raise type, exc_value, traceback raise type(exc_value, traceback)
if isinstance(self.expected, dict) and testname in self.expected: if (isinstance(self.expected, dict)
and testname in self.expected):
expecteds = self.expected[testname] expecteds = self.expected[testname]
#with numpy version, when we print a number and read it back, we don't get exactly the same result # with numpy version, when we print a number and read it
#So we accept rounding error in that case. # back, we don't get exactly the same result #So we accept
# rounding error in that case.
eps = 5e-9 eps = 5e-9
else: else:
expecteds = self.expected(*inputs) expecteds = self.expected(*inputs)
...@@ -224,27 +227,38 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -224,27 +227,38 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
variables = f(*inputs) variables = f(*inputs)
except Exception: except Exception:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while calling the Function on the inputs %s" \ err_msg = ("Test %s::%s: Error occurred while calling"
% (self.op, testname, inputs) " the Function on the inputs %s") % (
self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc_value.args = exc_value.args + (err_msg, )
raise type, exc_value, traceback raise type(exc_value, traceback)
if not isinstance(expecteds, (list, tuple)): if not isinstance(expecteds, (list, tuple)):
expecteds = (expecteds, ) expecteds = (expecteds, )
for i, (variable, expected) in enumerate(izip(variables, expecteds)): for i, (variable, expected) in enumerate(
if variable.dtype != expected.dtype or variable.shape != expected.shape or \ izip(variables, expecteds)):
numpy.any(numpy.abs(variable - expected) > eps): if (variable.dtype != expected.dtype
self.fail("Test %s::%s: Output %s gave the wrong value. With inputs %s, expected %s, got %s. numpy.allclose return %s %s" or variable.shape != expected.shape
% (self.op, testname, i, inputs, expected, or numpy.any(abs(variable - expected) > eps):
variable, self.fail(("Test %s::%s: Output %s gave the wrong"
numpy.allclose(variable, expected, atol=eps), " value. With inputs %s, expected %s, got %s."
numpy.allclose(variable, expected))) " numpy.allclose return %s %s") % (
self.op,
testname,
i,
inputs,
expected,
variable,
numpy.allclose(variable, expected, atol=eps),
numpy.allclose(variable, expected)))
for description, check in self.checks.items(): for description, check in self.checks.items():
if not check(inputs, variables): if not check(inputs, variables):
self.fail("Test %s::%s: Failed check: %s (inputs were %s, outputs were %s)" self.fail(("Test %s::%s: Failed check: %s (inputs"
% (self.op, testname, description, inputs, variables)) " were %s, outputs were %s)") % (
self.op, testname, description,
inputs, variables))
def test_bad_build(self): def test_bad_build(self):
if skip: if skip:
...@@ -252,8 +266,10 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -252,8 +266,10 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
for testname, inputs in self.bad_build.items(): for testname, inputs in self.bad_build.items():
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]
self.assertRaises(Exception, safe_make_node, self.op, *inputrs) self.assertRaises(Exception,
# The old error string was ("Test %s::%s: %s was successfully instantiated on the following bad inputs: %s" safe_make_node, self.op, *inputrs)
# The old error string was ("Test %s::%s: %s was successfully
# instantiated on the following bad inputs: %s"
# % (self.op, testname, node, inputs)) # % (self.op, testname, node, inputs))
def test_bad_runtime(self): def test_bad_runtime(self):
...@@ -266,23 +282,25 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -266,23 +282,25 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
node = safe_make_node(self.op, *inputrs) node = safe_make_node(self.op, *inputrs)
except Exception: except Exception:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while trying to make a node with inputs %s" \ err_msg = ("Test %s::%s: Error occurred while trying"
% (self.op, testname, inputs) " to make a node with inputs %s") % (
self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc_value.args = exc_value.args + (err_msg, )
raise type, exc_value, traceback raise type(exc_value, traceback)
try: try:
f = inplace_func(inputrs, node.outputs, mode=mode) f = inplace_func(inputrs, node.outputs, mode=mode)
except Exception: except Exception:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while trying to make a Function" \ err_msg = ("Test %s::%s: Error occurred while trying"
% (self.op, testname) " to make a Function") % (self.op, testname)
exc_value.args = exc_value.args + (err_msg, ) exc_value.args = exc_value.args + (err_msg, )
raise type, exc_value, traceback raise type(exc_value, traceback)
# Add tester return a ValueError. Should we catch only this one? # Add tester return a ValueError. Should we catch only this
# TODO: test that only this one is raised and catch only this one # one?
# or the subset that get raised. # TODO: test that only this one is raised and catch only this
# one or the subset that get raised.
self.assertRaises(Exception, f, *inputs) self.assertRaises(Exception, f, *inputs)
def test_grad(self): def test_grad(self):
...@@ -296,13 +314,16 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -296,13 +314,16 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
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:
utt.verify_grad(self.op, inputs, mode=self.mode, rel_tol=_grad_rtol) utt.verify_grad(self.op, inputs,
mode=self.mode,
rel_tol=_grad_rtol)
except Exception: except Exception:
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"
% (self.op, testname, inputs) " computing the gradient on the following"
" inputs: %s" ) % (self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc_value.args = exc_value.args + (err_msg, )
raise type, exc_value, traceback raise type(exc_value, traceback)
finally: finally:
config.warn.sum_div_dimshuffle_bug = backup config.warn.sum_div_dimshuffle_bug = backup
...@@ -310,9 +331,19 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -310,9 +331,19 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
return Checker return Checker
rand = lambda *shape: 2 * numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX) - 1 def rand(*shape):
randint = lambda *shape: numpy.random.random_integers(-5, 5, shape) r = numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX)
randcomplex = lambda *shape: numpy.complex128(2 * numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX) - 1) return r * 2 - 1
def randint(*shape):
return numpy.random.random_integers(-5, 5, shape)
# XXX: this so-called complex random array as all-zero imaginary parts
def randcomplex(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype=config.floatX)
return numpy.complex128(2 * r - 1)
def randint_nonzero(*shape): def randint_nonzero(*shape):
...@@ -371,7 +402,8 @@ def makeBroadcastTester(op, expected, checks={}, name=None, **kwargs): ...@@ -371,7 +402,8 @@ def makeBroadcastTester(op, expected, checks={}, name=None, **kwargs):
def inplace_check(inputs, outputs): def inplace_check(inputs, outputs):
# this used to be inputs[0] is output[0] # this used to be inputs[0] is output[0]
# I changed it so that it was easier to satisfy by the DebugMode # I changed it so that it was easier to satisfy by the
# DebugMode
return numpy.all(inputs[0] == outputs[0]) return numpy.all(inputs[0] == outputs[0])
checks = dict(checks, inplace_check=inplace_check) checks = dict(checks, inplace_check=inplace_check)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论