提交 7c17434d authored 作者: nouiz's avatar nouiz

Merge pull request #188 from jaberg/master

Fix failing tests in tensor module
...@@ -272,7 +272,8 @@ class CLinkerOp(CLinkerObject): ...@@ -272,7 +272,8 @@ class CLinkerOp(CLinkerObject):
- `MethodNotDefined`: Subclass does not implement this method - `MethodNotDefined`: Subclass does not implement this method
""" """
raise utils.MethodNotDefined("c_support_code_apply", type(self), self.__class__.__name__) raise utils.MethodNotDefined("c_support_code_apply",
type(self), self.__class__.__name__)
class PureOp(object): class PureOp(object):
......
"""
Tests for GPU convolution
"""
import sys import sys
import time import time
import unittest import unittest
......
...@@ -1206,12 +1206,15 @@ class Mod(BinaryScalarOp): ...@@ -1206,12 +1206,15 @@ class Mod(BinaryScalarOp):
def c_code(self, node, name, (x, y), (z, ), sub): def c_code(self, node, name, (x, y), (z, ), sub):
""" """
We want the result to have the same sign as python, not the other implementation of mod. We want the result to have the same sign as python, not the other
implementation of mod.
""" """
#raise NotImplementedError("Unlike Python, C's modulo returns negative modulo on negative dividend (to implement)") # raise NotImplementedError("Unlike Python, C's modulo returns negative
# modulo on negative dividend (to implement)")
t = node.inputs[0].type.upcast(*[ i.type for i in node.inputs[1:]]) t = node.inputs[0].type.upcast(*[ i.type for i in node.inputs[1:]])
if (str(t) in imap(str, discrete_types) or if (str(t) in imap(str, discrete_types) or
t in ['uint8','int8','uint16','int16','uint32','int32','uint64','int64'] or t in ['uint8','int8','uint16','int16'] or
t in ['uint32','int32','uint64','int64'] or
t in discrete_types): t in discrete_types):
# The above or's should not be needed anymore. However, for now we # The above or's should not be needed anymore. However, for now we
# keep them out of safety, and verify they are useless with an # keep them out of safety, and verify they are useless with an
...@@ -2097,6 +2100,16 @@ class Composite(ScalarOp): ...@@ -2097,6 +2100,16 @@ class Composite(ScalarOp):
return () return ()
return tuple(rval) return tuple(rval)
def c_support_code(self):
rval = []
for subnode in self.env.toposort():
try:
rval.append(subnode.op.c_support_code())
except gof.utils.MethodNotDefined:
pass
# remove duplicate code blocks
return "\n".join(sorted(set(rval)))
def c_support_code_apply(self, node, name): def c_support_code_apply(self, node, name):
rval = [] rval = []
for subnode, subnodename in zip(self.env.toposort(), self.nodenames): for subnode, subnodename in zip(self.env.toposort(), self.nodenames):
...@@ -2107,6 +2120,10 @@ class Composite(ScalarOp): ...@@ -2107,6 +2120,10 @@ class Composite(ScalarOp):
subnodename % dict(nodename=name))) subnodename % dict(nodename=name)))
except gof.utils.MethodNotDefined: except gof.utils.MethodNotDefined:
pass pass
# there should be no need to remove duplicate code blocks because
# each block should have been specialized for the given nodename.
# Any block that isn't specialized should be returned via
# c_support_code instead of c_support_code_apply.
return "\n".join(rval) return "\n".join(rval)
def __eq__(self, other): def __eq__(self, other):
......
...@@ -929,6 +929,9 @@ class Elemwise(Op): ...@@ -929,6 +929,9 @@ class Elemwise(Op):
def c_headers(self): def c_headers(self):
return ['<vector>', '<algorithm>'] return ['<vector>', '<algorithm>']
def c_support_code(self):
return self.scalar_op.c_support_code()
def c_support_code_apply(self, node, nodename): def c_support_code_apply(self, node, nodename):
support_code = self.scalar_op.c_support_code_apply(node, support_code = self.scalar_op.c_support_code_apply(node,
nodename + '_scalar_') nodename + '_scalar_')
......
...@@ -670,12 +670,15 @@ class GradientError(Exception): ...@@ -670,12 +670,15 @@ class GradientError(Exception):
def __str__(self): def __str__(self):
# args may have been inserted by e.g. makeTester
args_msg = ", ".join(str(a) for a in self.args)
return """GradientError: numeric gradient and analytic gradient exceed tolerance: return """GradientError: numeric gradient and analytic gradient exceed tolerance:
At position %i of argument %i, At position %i of argument %i,
abs. error = %f, abs. tolerance = %f abs. error = %f, abs. tolerance = %f
rel. error = %f, rel. tolerance = %f rel. error = %f, rel. tolerance = %f\nException args: %s
""" %(self.err_pos, self.arg, """ %(self.err_pos, self.arg,
self.abs_err, self.abs_tol, self.abs_err, self.abs_tol,
self.rel_err, self.rel_tol) self.rel_err, self.rel_tol,
args_msg)
verify_grad.E_grad = GradientError verify_grad.E_grad = GradientError
...@@ -42,6 +42,7 @@ try: ...@@ -42,6 +42,7 @@ try:
except ImportError: except ImportError:
if config.mode == "FAST_COMPILE": if config.mode == "FAST_COMPILE":
mode_no_scipy = "FAST_RUN" mode_no_scipy = "FAST_RUN"
floatX = config.floatX
### seed random number generator so that unittests are deterministic ### ### seed random number generator so that unittests are deterministic ###
utt.seed_rng() utt.seed_rng()
...@@ -192,25 +193,26 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -192,25 +193,26 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
try: try:
#node = self.op.make_node(*inputrs) #node = self.op.make_node(*inputrs)
node = safe_make_node(self.op, *inputrs) node = safe_make_node(self.op, *inputrs)
except Exception: except Exception, exc:
type, exc_value, traceback = sys.exc_info() err_msg = ("Test %s::%s: Error occurred while"
err_msg = "Test %s::%s: Error occurred while making a node with inputs %s" \ " making a node with inputs %s") % (
% (self.op, testname, inputs) self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc.args += (err_msg,)
raise type, exc_value, traceback raise
try: try:
f = inplace_func(inputrs, node.outputs, mode=mode) f = inplace_func(inputrs, node.outputs, mode=mode)
except Exception: except Exception, exc:
type, exc_value, traceback = sys.exc_info() err_msg = ("Test %s::%s: Error occurred while"
err_msg = "Test %s::%s: Error occurred while trying to make a Function" \ " trying to make a Function") % (self.op, testname)
% (self.op, testname) exc.args += (err_msg,)
exc_value.args = exc_value.args + (err_msg, ) raise
raise type, exc_value, traceback if (isinstance(self.expected, dict)
if isinstance(self.expected, dict) and testname in self.expected: 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)
...@@ -222,29 +224,39 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -222,29 +224,39 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
try: try:
variables = f(*inputs) variables = f(*inputs)
except Exception: except Exception, exc:
type, exc_value, traceback = sys.exc_info() err_msg = ("Test %s::%s: Error occurred while calling"
err_msg = "Test %s::%s: Error occurred while calling the Function on the inputs %s" \ " the Function on the inputs %s") % (
% (self.op, testname, inputs) self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc.args += (err_msg,)
raise type, exc_value, traceback raise
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)):
self.fail(("Test %s::%s: Output %s gave the wrong"
" value. With inputs %s, expected %s, got %s."
" numpy.allclose return %s %s") % (
self.op,
testname,
i,
inputs,
expected,
variable, variable,
numpy.allclose(variable, expected, atol=eps), numpy.allclose(variable, expected, atol=eps),
numpy.allclose(variable, expected))) 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 +264,10 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -252,8 +264,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):
...@@ -264,25 +278,25 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -264,25 +278,25 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={},
inputrs = [value(input) for input in inputs] inputrs = [value(input) for input in inputs]
try: try:
node = safe_make_node(self.op, *inputrs) node = safe_make_node(self.op, *inputrs)
except Exception: except Exception, exc:
type, exc_value, traceback = sys.exc_info() err_msg = ("Test %s::%s: Error occurred while trying"
err_msg = "Test %s::%s: Error occurred while trying to make a node with inputs %s" \ " to make a node with inputs %s") % (
% (self.op, testname, inputs) self.op, testname, inputs)
exc_value.args = exc_value.args + (err_msg, ) exc.args += (err_msg,)
raise type, exc_value, traceback raise
try: try:
f = inplace_func(inputrs, node.outputs, mode=mode) f = inplace_func(inputrs, node.outputs, mode=mode)
except Exception: except Exception, exc:
type, exc_value, traceback = sys.exc_info() err_msg = ("Test %s::%s: Error occurred while trying"
err_msg = "Test %s::%s: Error occurred while trying to make a Function" \ " to make a Function") % (self.op, testname)
% (self.op, testname) exc.args += (err_msg,)
exc_value.args = exc_value.args + (err_msg, ) raise
raise type, exc_value, traceback
# Add tester return a ValueError. Should we catch only this
# Add tester return a ValueError. Should we catch only this one? # one?
# TODO: test that only this one is raised and catch only this one # TODO: test that only this one is raised and catch only this
# or the subset that get raised. # 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 +310,15 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -296,13 +310,15 @@ 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,
except Exception: mode=self.mode,
type, exc_value, traceback = sys.exc_info() rel_tol=_grad_rtol)
err_msg = "Test %s::%s: Error occurred while computing the gradient on the following inputs: %s" \ except Exception, exc:
% (self.op, testname, inputs) err_msg = ("Test %s::%s: Error occurred while"
exc_value.args = exc_value.args + (err_msg, ) " computing the gradient on the following"
raise type, exc_value, traceback " inputs: %s" ) % (self.op, testname, inputs)
exc.args += (err_msg,)
raise
finally: finally:
config.warn.sum_div_dimshuffle_bug = backup config.warn.sum_div_dimshuffle_bug = backup
...@@ -310,9 +326,19 @@ def makeTester(name, op, expected, checks={}, good={}, bad_build={}, ...@@ -310,9 +326,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 +397,8 @@ def makeBroadcastTester(op, expected, checks={}, name=None, **kwargs): ...@@ -371,7 +397,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)
...@@ -626,37 +653,66 @@ PowInplaceTester = makeBroadcastTester(op = inplace.pow_inplace, ...@@ -626,37 +653,66 @@ PowInplaceTester = makeBroadcastTester(op = inplace.pow_inplace,
#Those are corner case when rounding. Their is many rounding algo. #Those are corner case when rounding. Their is many rounding algo.
#c round() fct and numpy round are not the same! #c round() fct and numpy round are not the same!
corner_case = numpy.asarray([-2.5, -2., -1.5, -1., -0.5, -.51, -.49, 0, 0.49, 0.5, 0.9, 1, 1.5, 2, 2.5], dtype=config.floatX) corner_case = numpy.asarray(
[-2.5, -2., -1.5, -1., -0.5, -.51, -.49, 0,
0.49, 0.5, 0.9, 1, 1.5, 2, 2.5],
dtype=floatX)
#we remove 0 here as the grad is not always computable numerically. #we remove 0 here as the grad is not always computable numerically.
corner_case_grad = numpy.asarray([-2.5, -2., -1.5, -1., -0.5, -.51, -.49, 0.49, 0.5, 0.9, 1, 1.5, 2, 2.5], dtype=config.floatX) corner_case_grad = numpy.asarray(
_good_broadcast_unary_normal_float = dict(normal = (rand_ranged(-5, 5, (2, 3)),), [-2.5, -2., -1.5, -1., -0.5, -.51, -.49,
corner_case = (corner_case,), 0.49, 0.5, 0.9, 1, 1.5, 2, 2.5],
complex = (randcomplex(2,3),), dtype=floatX)
empty = (numpy.asarray([]),))
_good_broadcast_unary_normal_float = dict(
_good_broadcast_unary_normal_float_no_empty = copy(_good_broadcast_unary_normal_float) normal=[rand_ranged(-5, 5, (2, 3))],
del _good_broadcast_unary_normal_float_no_empty['empty'] corner_case=[corner_case],
_good_broadcast_unary_normal_float_no_empty_no_complex = copy(_good_broadcast_unary_normal_float_no_empty) complex=[randcomplex(2,3)],
del _good_broadcast_unary_normal_float_no_empty_no_complex['complex'] empty=[numpy.asarray([])])
_good_broadcast_unary_normal_float_no_complex = copy(_good_broadcast_unary_normal_float)
del _good_broadcast_unary_normal_float_no_complex['complex'] def copymod(dct, without=[], **kwargs):
"""Return dct but with the keys named by args removed, and with
_good_broadcast_unary_normal = dict(normal = (numpy.asarray(rand_ranged(-5, 5, (2, 3)),dtype=config.floatX),), kwargs added.
integers = (randint_ranged(-5, 5, (2, 3)),), """
corner_case = (corner_case,), rval = copy(dct)
complex = (randcomplex(2,3),), for a in without:
empty = (numpy.asarray([]),)) if a in rval:
del rval[a]
_good_broadcast_unary_normal_no_complex = dict(normal = (numpy.asarray(rand_ranged(-5, 5, (2, 3)),dtype=config.floatX),), for kw, val in kwargs.items():
integers = (randint_ranged(-5, 5, (2, 3)),), dct[kw] = val
corner_case = (corner_case,), return rval
#complex = (randcomplex(2,3),),
empty = (numpy.asarray([]),)) _good_broadcast_unary_normal_float_no_empty = copymod(
_good_broadcast_unary_normal_float,
_grad_broadcast_unary_normal = dict(normal = (numpy.asarray(rand_ranged(-5, 5, (2, 3)),dtype=config.floatX),), without=['empty'])
corner_case = (corner_case_grad,),
#complex = (randcomplex(2,3),), _good_broadcast_unary_normal_float_no_empty_no_complex = copymod(
#empty = (numpy.asarray([]),) _good_broadcast_unary_normal_float_no_empty,
without=['complex'])
_good_broadcast_unary_normal_float_no_complex = copymod(
_good_broadcast_unary_normal_float,
without=['complex'])
_good_broadcast_unary_normal = dict(
normal=[numpy.asarray(rand_ranged(-5, 5, (2, 3)),dtype=config.floatX)],
integers=[randint_ranged(-5, 5, (2, 3))],
corner_case=[corner_case],
complex=[randcomplex(2,3)],
empty=[numpy.asarray([])],
)
_good_broadcast_unary_normal_no_complex = dict(
normal=[numpy.asarray(rand_ranged(-5, 5, (2, 3)), dtype=floatX)],
integers=[randint_ranged(-5, 5, (2, 3))],
corner_case=[corner_case],
empty=[numpy.asarray([])],
)
_grad_broadcast_unary_normal = dict(
normal=[numpy.asarray(rand_ranged(-5, 5, (2, 3)), dtype=floatX)],
corner_case = [corner_case_grad],
#empty = [numpy.asarray([])] # XXX: should this be included?
) )
...@@ -693,24 +749,44 @@ SgnInplaceTester = makeBroadcastTester(op = inplace.sgn_inplace, ...@@ -693,24 +749,44 @@ SgnInplaceTester = makeBroadcastTester(op = inplace.sgn_inplace,
good = _good_broadcast_unary_normal_no_complex, good = _good_broadcast_unary_normal_no_complex,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
inplace = True) inplace = True)
CeilTester = makeBroadcastTester(op = tensor.ceil,
expected = lambda a: numpy.asarray(numpy.ceil(a),a.dtype),
good = _good_broadcast_unary_normal_no_complex,
grad = _grad_broadcast_unary_normal)
CeilInplaceTester = makeBroadcastTester(op = inplace.ceil_inplace,
expected = lambda a: numpy.asarray(numpy.ceil(a),a.dtype),
good = _good_broadcast_unary_normal_no_complex,
grad = _grad_broadcast_unary_normal,
inplace = True)
FloorTester = makeBroadcastTester(op = tensor.floor,
expected = lambda a: numpy.asarray(numpy.floor(a),a.dtype), CeilTester = makeBroadcastTester(op=tensor.ceil,
good = _good_broadcast_unary_normal_no_complex, expected=lambda a: numpy.asarray(
grad = _grad_broadcast_unary_normal) numpy.ceil(a),
FloorInplaceTester = makeBroadcastTester(op = inplace.floor_inplace, a.dtype),
expected = lambda a: numpy.asarray(numpy.floor(a),a.dtype), good=_good_broadcast_unary_normal_no_complex,
good = _good_broadcast_unary_normal_no_complex, grad=copymod(_grad_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, without=['corner_case'],
# corner_case includes ints where ceil is not differentiable
extra=[numpy.asarray([-2.5, -1.5, -1.51, 0.49, .98, 1.02],
dtype=floatX)]))
CeilInplaceTester = makeBroadcastTester(op=inplace.ceil_inplace,
expected=lambda a: numpy.asarray(numpy.ceil(a), a.dtype),
good=_good_broadcast_unary_normal_no_complex,
# corner cases includes a lot of integers: points where Ceil is not
# continuous (not differentiable)
grad=copymod(_grad_broadcast_unary_normal,
without=['corner_case'],
# corner_case includes ints where ceil is not differentiable
extra=[numpy.asarray([-2.5, -1.5, -1.51, 0.49, .98, 1.02],
dtype=floatX)]),
inplace=True)
FloorTester = makeBroadcastTester(op=tensor.floor,
expected=lambda a: numpy.asarray(numpy.floor(a), a.dtype),
good=_good_broadcast_unary_normal_no_complex,
# XXX: why does grad of floor not give huge values at
# the integer points in the 'corner_case' in
# _grad_broadcast_unary_normal? It seems this test should fail,
# yet it does not...
grad=_grad_broadcast_unary_normal)
FloorInplaceTester = makeBroadcastTester(op=inplace.floor_inplace,
expected=lambda a: numpy.asarray(numpy.floor(a), a.dtype),
good=_good_broadcast_unary_normal_no_complex,
grad=_grad_broadcast_unary_normal,
inplace = True) inplace = True)
RoundHalfToEvenTester = makeBroadcastTester(op = tensor.round_half_to_even, RoundHalfToEvenTester = makeBroadcastTester(op = tensor.round_half_to_even,
...@@ -1255,24 +1331,6 @@ def _approx_eq(a,b,eps=1.0e-4): ...@@ -1255,24 +1331,6 @@ def _approx_eq(a,b,eps=1.0e-4):
return True return True
_approx_eq.debug = 0 _approx_eq.debug = 0
# def check_eq(self, node_in, node_out, arg_in, arg_out):
# fn = Function([node_in], node_out)
# self.assertTrue( numpy.all(fn(arg_in) == arg_out), (arg_in, arg_out))
# def check_eq2(self, inputs, output, args_in, arg_out):
# fn = Function(inputs, output)
# val = fn(*args_in)
# self.assertTrue( numpy.all(val == arg_out), (val, arg_out))
# def check_eq2_c(self, inputs, output, args_in, arg_out):
# fn = Function(inputs, [output], linker_cls = gof.CLinker)
# val = fn(*args_in)
# self.assertTrue( numpy.all(val == arg_out), (val, arg_out))
# def check_eq2_both(self, inputs, output, args_in, arg_out):
# fn = Function(inputs, [output], linker_cls = lambda env: gof.DualLinker(env, _numpy_checker))
# val = fn(*args_in)
# self.assertTrue( numpy.all(val == arg_out), (val, arg_out))
def test_tensor_values_eq_approx(): def test_tensor_values_eq_approx():
#test, inf, -inf and nan equal themself #test, inf, -inf and nan equal themself
...@@ -3214,173 +3272,6 @@ class T_mean(unittest.TestCase): ...@@ -3214,173 +3272,6 @@ class T_mean(unittest.TestCase):
data = numpy.asarray(numpy.random.rand(50), dtype=config.floatX) data = numpy.asarray(numpy.random.rand(50), dtype=config.floatX)
assert numpy.allclose(f(data), numpy.mean(data)) assert numpy.allclose(f(data), numpy.mean(data))
# class T_abs(unittest.TestCase):
# def test_impl(self):
# t = as_tensor_variable(1.0)
# check_eq(self, t, abs(t), 1.0, 1.0)
# check_eq(self, t, abs(t), -1.0, 1.0)
# for shape in (2,), (3,4):
# t = as_tensor_variable(numpy.ones(shape))
# d = numpy.random.rand(*shape)*2-1.0
# check_eq(self, t, abs(t), d, abs(d))
# check_eq(self, t, abs(t), -d, abs(-d))
# def test_grad(self):
# utt.verify_grad(Abs, [numpy.ones(())])
# utt.verify_grad(Abs, [numpy.ones(3)])
# class AbsBadGrad(Abs):
# def grad(self, (x, ), (gz, )):
# return mul(gz * sgn(x),0.9),
# def test_badgrad(self):
# try:
# utt.verify_grad(T_abs.AbsBadGrad, [numpy.ones(())])
# except Exception, e:
# self.assertTrue(str(e) == utt.verify_grad.E_grad, str(e))
# return
# self.fail()
# class T_fill(unittest.TestCase):
# def test0(self):
# t = fill(numpy.asarray([1,2,3]), 9)
# self.assertTrue(t.owner.__class__ == Fill)
# o = t.owner
# self.assertTrue(o.inputs[0].broadcastable == (0,))
# # self.assertTrue(o.inputs[0].dtype[0:3] == 'int')
# self.assertTrue(o.inputs[1].broadcastable == (1,))
# # self.assertTrue(o.inputs[1].dtype[0:3] == 'flo')
# self.assertTrue(o.outputs[0].broadcastable == (0,))
# # self.assertTrue(o.outputs[0].dtype[0:3] == 'flo')
# self.assertTrue(numpy.all(eval_outputs([t]) == [9,9,9]))
# def test1(self):
# x = as_tensor_variable(numpy.ones((4,5)))
# l = ones_like(x[:,0:1])
# r = ones_like(x[0:1,:])
# xx = x + dot(l,r)
# self.assertTrue(numpy.mean(eval_outputs([xx]) == 2.0))
# class T_sum(unittest.TestCase):
# def test_impl(self):
# t = as_tensor_variable(0.0)
# check_eq(self, t, Sum(t).out, 1.0, 1.0)
# check_eq(self, t, Sum(t).out, -1.0, -1.0)
# t = as_tensor_variable([0.0, 0.0])
# d = numpy.asarray([-0.4, 1.2])
# check_eq(self, t, Sum(t).out, d, numpy.sum(d))
# check_eq(self, t, Sum(t).out, -d, -numpy.sum(d))
# class T_mul(unittest.TestCase):
# def setUp(self):
# utt.seed_rng()
# def test_elemwise(self):
# a = as_tensor_variable(0.0)
# b = as_tensor_variable(0.0)
# check_eq2_both(self, [a,b], mul(a,b), [3.0, 4.0], 12.0)
# check_eq2_both(self, [a,b], mul(b,a), [-1.0,2.0], -2.0)
# a = as_tensor_variable(numpy.ones(2))
# b = as_tensor_variable(numpy.ones(2))
# aa = numpy.asarray([-0.5, 4.0])
# bb = numpy.asarray([-0.5, 2.0])
# check_eq2_both(self, [a,b], mul(a,b), [aa,bb], numpy.asarray([0.25, 8.0]))
# check_eq2_both(self, [a,b], mul(a,b), [bb,aa], numpy.asarray([0.25, 8.0]))
# def test_scalar(self):
# r = numpy.random.rand(2,3)
# a = as_tensor_variable(r)
# b = as_tensor_variable(2.0)
# check_eq2_both(self, [a,b], mul(a,b), [r, 2.0], r*2.0)
# check_eq2_both(self, [a,b], mul(a,b), [r, 4.0], r*4.0)
# self.assertTrue(b.data == 2.0)
# def test_rowcol(self):
# r1 = numpy.random.rand(3,5)
# r2 = numpy.random.rand(1,5)
# r3 = numpy.random.rand(3,1)
# a1, a2, a3 = as_tensor_variable(r1), as_tensor_variable(r2), as_tensor_variable(r3)
# check_eq2_both(self, [a1,a2], mul(a1,a2), [r1, r2], r1*r2)
# check_eq2_both(self, [a1,a3], mul(a1,a3), [r1, r3], r1*r3)
# def test_grad_elemwise(self):
# utt.verify_grad(Mul, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
# def test_grad_scalar_l(self):
# utt.verify_grad(Mul, [numpy.asarray([3.0]), numpy.random.rand(3)])
# def test_grad_scalar_r(self):
# utt.verify_grad(Mul, [numpy.random.rand(3), numpy.asarray([3.0])])
# def test_grad_row(self):
# utt.verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
# def test_grad_row2(self):
# op = lambda x, y: Mul(x, DimShuffle(y, ['x', 0]).out)
# utt.verify_grad(op, [numpy.random.rand(3, 5), numpy.random.rand(5)])
# def test_grad_col(self):
# utt.verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
# def test_wrong_shapes(self):
# a = as_tensor_variable(numpy.ones(3))
# b = as_tensor_variable(numpy.ones(4))
# try:
# check_eq2(self, [a,b], Mul(a,b).out,
# [numpy.ones(3), numpy.ones(4)], 1.0)
# self.fail()
# except ValueError, e:
# self.assertTrue('shape mismatch' in str(e))
# try:
# check_eq2_c(self, [a,b], Mul(a,b).out,
# [numpy.ones(3), numpy.ones(4)], 1.0)
# self.fail()
# except ValueError, e:
# pass
# class T_div(unittest.TestCase):
# def setUp(self):
# utt.seed_rng()
# def test_grad_e(self):
# utt.verify_grad(Div, [numpy.random.rand(3), numpy.ones(3)])
# utt.verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(3,5)+0.1])
# utt.verify_grad(Div, [numpy.ones(()), numpy.ones(())])
# def test_grad_sl(self):
# utt.verify_grad(Div, [numpy.ones((3, 5)), numpy.ones((1, 1))])
# utt.verify_grad(Div, [numpy.random.rand(3), numpy.ones((1, ))])
# utt.verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(1,1)])
# class T_log2(unittest.TestCase):
# def setUp(self):
# utt.seed_rng()
# def test0(self):
# utt.verify_grad(Log2, [numpy.random.rand(3,1)+0.0001])
# class T_log(unittest.TestCase):
# def setUp(self):
# utt.seed_rng()
# def test0(self):
# utt.verify_grad(Log, [numpy.random.rand(3,1)+0.0001])
# def test1(self):
# a = as_tensor_variable(numpy.ones(2))
# b = as_tensor_variable(numpy.ones(2))
# aa = numpy.asarray([0.5, 4.0])
# bb = numpy.asarray([0.5, 2.0])
# check_eq2(self, [a], log(a), [aa], numpy.log(numpy.asarray(aa)))
# class T_pow(unittest.TestCase):
# def setUp(self):
# utt.seed_rng()
# def test_elemwise(self):
# utt.verify_grad(Div, [numpy.random.rand(3,4), numpy.random.rand(3,4)+0.1])
# utt.verify_grad(Pow, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
# def test_scalar_l(self):
# utt.verify_grad(Pow, [numpy.asarray([3.0]), numpy.random.rand(3)])
# def test_scalar_r(self):
# utt.verify_grad(Pow, [numpy.random.rand(3), numpy.asarray([3.0])])
# def test_row(self):
# utt.verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
# def test_col(self):
# utt.verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
class test_matinv(unittest.TestCase): class test_matinv(unittest.TestCase):
...@@ -3646,140 +3537,6 @@ class T_scalarfromtensor(unittest.TestCase): ...@@ -3646,140 +3537,6 @@ class T_scalarfromtensor(unittest.TestCase):
self.assertTrue(isinstance(v, numpy.int64)) self.assertTrue(isinstance(v, numpy.int64))
self.assertTrue(v.shape == (),v.shape) self.assertTrue(v.shape == (),v.shape)
# def _tensor(data, broadcastable=None, name=None):
# """Return a TensorType containing given data"""
# data = numpy.asarray(data)
# if broadcastable is None:
# broadcastable = [s==1 for s in data.shape]
# elif broadcastable in [0, 1]:
# broadcastable = [broadcastable] * len(data.shape)
# rval = TensorType(data.dtype, broadcastable, name)
# rval.data = data # will raise if broadcastable was mis-specified
# return rval
# class T_tensor(unittest.TestCase):
# def setUp(self):
# utt.seed_rng()
# def test0(self): # allocate from a scalar float
# t = _tensor(1.0)
# self.assertTrue(isinstance(t, TensorType))
# self.assertTrue(t.dtype == 'float64')
# self.assertTrue(t.broadcastable == ())
# self.assertTrue(t.role == None)
# self.assertTrue(isinstance(t.data, numpy.ndarray))
# self.assertTrue(str(t.data.dtype) == 'float64')
# self.assertTrue(t.data == 1.0)
# def test0_int(self): # allocate from a scalar float
# t = _tensor(1)
# self.assertTrue(isinstance(t, TensorType))
# self.assertTrue(t.dtype == 'int64' or t.dtype == 'int32')
# def test1(self): # allocate from a vector of ints, not broadcastable
# t = _tensor(numpy.ones(5,dtype='int32'))
# self.assertTrue(isinstance(t, TensorType))
# self.assertTrue(t.dtype == 'int32')
# self.assertTrue(t.broadcastable == (0,))
# self.assertTrue(isinstance(t.data, numpy.ndarray))
# self.assertTrue(str(t.data.dtype) == 'int32')
# def test2(self): # allocate from a column matrix of complex with name
# t = _tensor(numpy.ones((5,1),dtype='complex64'),name='bart')
# self.assertTrue(isinstance(t, TensorType))
# self.assertTrue(t.dtype == 'complex64')
# self.assertTrue(t.broadcastable == (0,1))
# self.assertTrue(isinstance(t.data, numpy.ndarray))
# self.assertTrue(t.name == 'bart')
# def test2b(self): # allocate from a column matrix, not broadcastable
# t = _tensor(numpy.ones((5,1),dtype='complex64'),broadcastable=0)
# self.assertTrue(isinstance(t, TensorType))
# self.assertTrue(t.dtype == 'complex64')
# self.assertTrue(t.broadcastable == (0,0))
# self.assertTrue(isinstance(t.data, numpy.ndarray))
# f = Function([t], [t], linker_cls=gof.CLinker)
# self.assertTrue(numpy.all(t.data == f(t.data)))
# def test_data_normal(self): #test that assigning to .data works when it should
# t = _tensor(numpy.ones((5,1),dtype='complex64'), broadcastable=0)
# o27 = numpy.ones((2,7), dtype='complex64')
# t.data = o27
# lst = t._data
# self.assertTrue(t.data.shape == (2,7))
# self.assertTrue(t.data is o27)
# self.assertTrue(t._data is lst)
# def test_data_badrank0(self):
# t = _tensor(numpy.ones((5,1),dtype='complex64'), broadcastable=0)
# try:
# t.data = numpy.ones((2,7,1))
# self.fail()
# except ValueError, e:
# self.assertTrue(e[0] is TensorType.filter.E_rank)
# try:
# t.data = numpy.ones(1)
# self.fail()
# except ValueError, e:
# self.assertTrue(e[0] is TensorType.filter.E_rank)
# def test_data_badrank1(self):
# t = _tensor(numpy.ones((1,1),dtype='complex64'), broadcastable=1)
# try:
# t.data = numpy.ones((1,1,1))
# self.fail()
# except ValueError, e:
# self.assertTrue(e[0] is TensorType.filter.E_rank)
# try:
# t.data = numpy.ones(1)
# self.fail()
# except ValueError, e:
# self.assertTrue(e[0] is TensorType.filter.E_rank)
# def test_data_badshape0(self):
# t = _tensor(numpy.ones((1,1),dtype='complex64'), broadcastable=1)
# try:
# t.data = numpy.ones((1,2))
# self.fail()
# except ValueError, e:
# self.assertTrue(e[0] is TensorType.filter.E_shape)
# try:
# t.data = numpy.ones((0,1))
# self.fail()
# except ValueError, e:
# self.assertTrue(e[0] is TensorType.filter.E_shape)
# def test_cast0(self):
# t = TensorType('float32', [0])
# t.data = numpy.random.rand(4) > 0.5
# self.assertTrue(str(t.data.dtype) == t.dtype)
# class T_stdlib(unittest.TestCase):
# def test0(self):
# t = _tensor(1.0)
# tt = t.clone(False)
# self.assertTrue(t.dtype == tt.dtype)
# self.assertTrue(t.broadcastable is tt.broadcastable)
# self.assertTrue(tt.data is None)
# self.assertTrue(t.data == 1.0)
# def test0b(self):
# t = _tensor(1.0)
# tt = t.clone()
# self.assertTrue(t.dtype == tt.dtype)
# self.assertTrue(t.broadcastable is tt.broadcastable)
# self.assertTrue(tt.data is None)
# self.assertTrue(t.data == 1.0)
# def test1(self):
# t = _tensor(1.0)
# tt = t.clone(True)
# self.assertTrue(t.dtype == tt.dtype)
# self.assertTrue(t.broadcastable is tt.broadcastable)
# self.assertTrue(tt.data == 1.0)
# self.assertTrue(t.data == 1.0)
# self.assertTrue(t.data is not tt.data)
# def test1b(self):
# t = _tensor(1.0)
# tt = copy(t)
# self.assertTrue(t.dtype == tt.dtype)
# self.assertTrue(t.broadcastable is tt.broadcastable)
# self.assertTrue(tt.data == 1.0)
# self.assertTrue(t.data == 1.0)
# self.assertTrue(t.data is not tt.data)
class test_grad(unittest.TestCase): class test_grad(unittest.TestCase):
class O(gof.op.Op): class O(gof.op.Op):
...@@ -5228,15 +4985,23 @@ def test_mod(): ...@@ -5228,15 +4985,23 @@ def test_mod():
def test_mod_compile(): def test_mod_compile():
""" """
This test generate an Elemwise of Composite as: This test generate an Elemwise of Composite as:
Elemwise{Composite{Composite{Composite{Composite{mod,EQ},Switch},mul},add}} Elemwise{
Composite{
The c_code generated is not compiling as of 30 June 2010. I fix the compilation in the same commit. Composite{
Composite{
Composite{mod,EQ},
Switch},
mul},
add}}
The c_code generated is not compiling as of 30 June 2010. I fix the
compilation in the same commit.
""" """
x = tensor.vector() x = tensor.vector()
y = tensor.vector() y = tensor.vector()
shape = x.shape shape = x.shape
out = tensor.switch(tensor.eq(3%x.shape[0],0),y,y[:-1]) out = tensor.switch(tensor.eq(3 % x.shape[0], 0), y, y[:-1])
f = theano.function([x,y],out) f = theano.function([x,y],out)
...@@ -5378,10 +5143,10 @@ class test_size(unittest.TestCase): ...@@ -5378,10 +5143,10 @@ class test_size(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
if 1: if 0:
unittest.main() unittest.main()
else: else:
testcase = T_Join_and_Split testcase = FloorInplaceTester
suite = unittest.TestLoader() suite = unittest.TestLoader()
suite = suite.loadTestsFromTestCase(testcase) suite = suite.loadTestsFromTestCase(testcase)
......
import copy, sys import copy
import numpy, theano import sys
import numpy
import theano
from theano import tensor from theano import tensor
from theano.tensor.nnet import crossentropy_softmax_argmax_1hot_with_bias from theano.tensor.nnet import crossentropy_softmax_argmax_1hot_with_bias
def test_bug_2009_06_02_trac_387():
def test_bug_2009_06_02_trac_387():
y = tensor.lvector('y') y = tensor.lvector('y')
#f = theano.function([y], tensor.stack(y[0] / 2)) f = theano.function([y],
#f = theano.function([y], tensor.join(0,tensor.shape_padleft(y[0] / 2,1))) tensor.int_div(
f = theano.function([y], tensor.int_div(tensor.DimShuffle(y[0].broadcastable, ['x'])(y[0]), 2)) tensor.DimShuffle(y[0].broadcastable, ['x'])(y[0]), 2))
sys.stdout.flush() sys.stdout.flush()
print f(numpy.ones(1, dtype='int64') * 3) print f(numpy.ones(1, dtype='int64') * 3)
#z = tensor.lscalar('z') # XXX: there is no assert, nor comment that DEBUGMODE is to do the
#f = theano.function([z], tensor.DimShuffle([], ['x'])(z) / 2) # checking. What was the bug, and how is it being tested?
def test_bug_2009_07_17_borrowed_output(): def test_bug_2009_07_17_borrowed_output():
"""Regression test for a bug where output was borrowed by mistake.""" """Regression test for a bug where output was borrowed by mistake."""
...@@ -68,4 +71,3 @@ def test_bug_2009_07_17_borrowed_output(): ...@@ -68,4 +71,3 @@ def test_bug_2009_07_17_borrowed_output():
assert id_z != id_other assert id_z != id_other
# Just to be 100% sure, ensure that z was not altered. # Just to be 100% sure, ensure that z was not altered.
assert (z == z_backup).all() assert (z == z_backup).all()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论