* removed "testcase" param from verify_grad as it wasn't being used

* fixed a bug with verify_grad failing on the second iteration with inplace operation (missing p.copy()) * renamed make_restet and make_broadcast_restet to makeTester and makeBroadcastTestser (sorry OB. was about to document and I figured it would be clearer with the real names...)
上级 184dd72c
...@@ -222,3 +222,41 @@ Similarly, to provide a seed to numpy.random.RandomState, simply use: ...@@ -222,3 +222,41 @@ Similarly, to provide a seed to numpy.random.RandomState, simply use:
>>> rng = numpy.random.RandomState(unittest_tools.fetch_seed(1231)) >>> rng = numpy.random.RandomState(unittest_tools.fetch_seed(1231))
Note that the ability to change the seed from one nosetest to another, is incompatible with the method of hard-coding the baseline variables (against which we compare the theano outputs). These must then be determined "algorithmically". Although this represents more work, the test suite will be better because of it. Note that the ability to change the seed from one nosetest to another, is incompatible with the method of hard-coding the baseline variables (against which we compare the theano outputs). These must then be determined "algorithmically". Although this represents more work, the test suite will be better because of it.
Creating an Op UnitTest
=======================
A few tools have been developed to help automate the development of unitests
for Theano Ops.
Validating the Gradient
-----------------------
The ``verify_grad`` function can be used to validate that the ``grad``
function of your Op is properly implemented. ``verify_grad`` is based on the
Finite Difference Method where the derivative of function ``f`` at point ``x``
is approximated as:
.. math::
\frac{\partial{f}}{\partial{x}} = lim_{\Delta \rightarrow 0} \frac {f(x+\Delta) - f(x-\Delta)} {2\Delta}
``verify_grad`` performs the following steps:
* approximates the gradient numerically using the Finite Difference Method
* calculate the gradient using the symbolic expression provided in the ``grad`` function
* compares the two values. The tests passes if they are equal to within a certain tolerance.
>>> def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=1.0e-7, tol=0.0001,
>>> mode=compile.Mode(optimizer=None, linker='c&py')):
>>> """
>>> Raises an Exception if the analytic gradient and numerical gradient exceeds a certain tolerance.
>>> :param testcase: (obsolete) a reference to the unittest object calling
>>> :param pt: the list of numpy.ndarrays to use as inputs to the op
>>> :param op: something that behaves like an Op instance with a single output
>>> (can be a python function combining multiple ops)
>>> :param testcase: the thing to call `fail` on if things go awry.
...@@ -20,7 +20,7 @@ from ..gof.python25 import partial ...@@ -20,7 +20,7 @@ from ..gof.python25 import partial
from .. import compile, printing from .. import compile, printing
from ..printing import pprint, Print from ..printing import pprint, Print
from ..tests import unittest_tools
### set up the external interface ### set up the external interface
from elemwise import Elemwise, DimShuffle, CAReduce, Sum from elemwise import Elemwise, DimShuffle, CAReduce, Sum
...@@ -2327,29 +2327,35 @@ class numeric_grad: ...@@ -2327,29 +2327,35 @@ class numeric_grad:
errs.append(numpy.max(numeric_grad.abs_rel_err(a,b))) errs.append(numpy.max(numeric_grad.abs_rel_err(a,b)))
return numpy.max(errs) return numpy.max(errs)
# TODO: remove testcase parameter as it is not used... and useless (forces you to
# run your testcases from a unittest.TestCase class = not necessary) def verify_grad(op, pt, n_tests=2, rng=None, eps=1.0e-7, tol=0.0001):
def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=1.0e-7, tol=0.0001,
mode=compile.Mode(optimizer=None, linker='c&py')):
""" WRITEME """ WRITEME
testcase.failUnless(analytic gradient matches finite-diff gradient) Raises an Exception if the difference between the analytic gradient and
numerical gradient (computed through the Finite Difference Method) exceeds
the given tolerance.
:param pt: the list of numpy.ndarrays to use as inputs to the op
:param op: something that behaves like an Op instance with a single output :param op: something that behaves like an Op instance with a single output
(can be a python function combining multiple ops) (can be a python function combining multiple ops)
:param testcase: the thing to call `fail` on if things go awry. :param pt: the list of numpy.ndarrays to use as inputs to the op
:param n_tests: number of times to run the test
:param rng: random number generator from which to draw random samples
:param eps: stepsize used in the Finite Difference Method
:param tol: relative tolerance used as threshold for gradient comparison
""" """
pt = [numpy.array(p) for p in pt] pt = [numpy.array(p) for p in pt]
#print "PT", pt if rng is None:
rng = numpy.random
unittest_tools.seed_rng()
def function(inputs, output): def function(inputs, output):
f = compile.function(inputs, output, mode=mode, accept_inplace=True) f = compile.function(inputs, output, accept_inplace=True)
return f return f
for test_num in xrange(n_tests): for test_num in xrange(n_tests):
tensor_pt = [value(p.copy(), name='input %i'%i) for i,p in enumerate(pt)] tensor_pt = [value(p.copy(), name='input %i'%i) for i,p in enumerate(pt)]
#op can be either a function or an actual Op instance #op can be either a function or an actual Op instance
...@@ -2360,8 +2366,10 @@ def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=1.0e-7, tol=0 ...@@ -2360,8 +2366,10 @@ def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=1.0e-7, tol=0
# we could make loop over outputs making random projections R for each, # we could make loop over outputs making random projections R for each,
# but this doesn't handle the case where not all the outputs are # but this doesn't handle the case where not all the outputs are
# differentiable... so I leave this as TODO for now -JB. # differentiable... so I leave this as TODO for now -JB.
o_fn = function(tensor_pt, o_output) o_fn = function(tensor_pt, o_output)
o_fn_out = o_fn(*[p.copy() for p in pt]) o_fn_out = o_fn(*[p.copy() for p in pt])
random_projection = rng.rand(*o_fn_out.shape) random_projection = rng.rand(*o_fn_out.shape)
t_r = as_tensor_variable(random_projection) t_r = as_tensor_variable(random_projection)
...@@ -2375,15 +2383,13 @@ def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=1.0e-7, tol=0 ...@@ -2375,15 +2383,13 @@ def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=1.0e-7, tol=0
grad_fn = function(tensor_pt, symbolic_grad) grad_fn = function(tensor_pt, symbolic_grad)
analytic_grad = grad_fn(*pt) analytic_grad = grad_fn(*[p.copy() for p in pt])
if not isinstance(analytic_grad, (list, tuple)): if not isinstance(analytic_grad, (list, tuple)):
analytic_grad = [analytic_grad] analytic_grad = [analytic_grad]
max_err = num_grad.max_err(analytic_grad) max_err = num_grad.max_err(analytic_grad)
if max_err > tol: if max_err > tol:
#print 'analytic grad', analytic_grad
#print 'numeric grad', num_grad.gf
raise Exception(verify_grad.E_grad, (max_err, tol)) raise Exception(verify_grad.E_grad, (max_err, tol))
verify_grad.E_grad = 'gradient error exceeded tolerance' verify_grad.E_grad = 'gradient error exceeded tolerance'
......
...@@ -31,14 +31,6 @@ def eval_outputs(outputs): ...@@ -31,14 +31,6 @@ def eval_outputs(outputs):
return variables[0] return variables[0]
return variables return variables
_public_verify_grad = verify_grad
def verify_grad(*args, **kwargs):
assert 'mode' not in kwargs
kwargs['mode'] = default_mode
r = _public_verify_grad(*args, **kwargs)
return r
def _numpy_checker(x, y): def _numpy_checker(x, y):
""" """
Checks if x.data and y.data have the same contents. Checks if x.data and y.data have the same contents.
...@@ -56,7 +48,7 @@ def safe_make_node(op, *inputs): ...@@ -56,7 +48,7 @@ def safe_make_node(op, *inputs):
else: else:
return node.owner return node.owner
def make_restet(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_runtime = {}, grad = {}): def makeTester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_runtime = {}, grad = {}):
if grad is True: if grad is True:
grad = good grad = good
...@@ -165,7 +157,7 @@ def make_restet(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_ ...@@ -165,7 +157,7 @@ def make_restet(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_
inputs = [copy(input) for input in inputs] inputs = [copy(input) for input in inputs]
inputrs = [value(input) for input in inputs] inputrs = [value(input) for input in inputs]
try: try:
verify_grad(self, self.op, inputs) verify_grad(self.op, inputs)
except: except:
type, exc_value, traceback = sys.exc_info() type, exc_value, traceback = sys.exc_info()
err_msg = "Test %s::%s: Error occurred while computing the gradient on the following inputs: %s" \ err_msg = "Test %s::%s: Error occurred while computing the gradient on the following inputs: %s" \
...@@ -191,7 +183,7 @@ def randint_ranged(min, max, shape): ...@@ -191,7 +183,7 @@ def randint_ranged(min, max, shape):
return numpy.random.random_integers(min, max, shape) return numpy.random.random_integers(min, max, shape)
def make_broadcast_restet(op, expected, checks = {}, **kwargs): def makeBroadcastTester(op, expected, checks = {}, **kwargs):
name = str(op) + "Tester" name = str(op) + "Tester"
if kwargs.has_key('inplace'): if kwargs.has_key('inplace'):
if kwargs['inplace']: if kwargs['inplace']:
...@@ -203,7 +195,7 @@ def make_broadcast_restet(op, expected, checks = {}, **kwargs): ...@@ -203,7 +195,7 @@ def make_broadcast_restet(op, expected, checks = {}, **kwargs):
return numpy.all(inputs[0] == outputs[0]) return numpy.all(inputs[0] == outputs[0])
checks = dict(checks, inplace_check=inplace_check) #lambda inputs, outputs: numpy.all(inputs[0] == outputs[0])) checks = dict(checks, inplace_check=inplace_check) #lambda inputs, outputs: numpy.all(inputs[0] == outputs[0]))
del kwargs['inplace'] del kwargs['inplace']
return make_restet(name, op, expected, checks, **kwargs) return makeTester(name, op, expected, checks, **kwargs)
...@@ -227,7 +219,7 @@ _grad_broadcast_binary_normal = dict(same_shapes = (rand(2, 3), rand(2, 3)), ...@@ -227,7 +219,7 @@ _grad_broadcast_binary_normal = dict(same_shapes = (rand(2, 3), rand(2, 3)),
column = (rand(2, 3), rand(2, 1))) column = (rand(2, 3), rand(2, 1)))
AddTester = make_broadcast_restet(op = add, AddTester = makeBroadcastTester(op = add,
expected = lambda *inputs: reduce(lambda x, y: x + y, inputs), expected = lambda *inputs: reduce(lambda x, y: x + y, inputs),
good = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)), good = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)),
four_inputs_broadcast = (rand(2, 3), rand(1, 3), rand(2, 1), rand(1, 1)), four_inputs_broadcast = (rand(2, 3), rand(1, 3), rand(2, 1), rand(1, 1)),
...@@ -236,21 +228,21 @@ AddTester = make_broadcast_restet(op = add, ...@@ -236,21 +228,21 @@ AddTester = make_broadcast_restet(op = add,
bad_runtime = _bad_runtime_broadcast_binary_normal) bad_runtime = _bad_runtime_broadcast_binary_normal)
AddInplaceTester = make_broadcast_restet(op = inplace.add_inplace, AddInplaceTester = makeBroadcastTester(op = inplace.add_inplace,
expected = lambda x, y: x + y, expected = lambda x, y: x + y,
good = _good_broadcast_binary_normal, good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal, bad_build = _bad_build_broadcast_binary_normal,
bad_runtime = _bad_runtime_broadcast_binary_normal, bad_runtime = _bad_runtime_broadcast_binary_normal,
inplace = True) inplace = True)
SubTester = make_broadcast_restet(op = sub, SubTester = makeBroadcastTester(op = sub,
expected = lambda x, y: x - y, expected = lambda x, y: x - y,
good = _good_broadcast_binary_normal, good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal, bad_build = _bad_build_broadcast_binary_normal,
bad_runtime = _bad_runtime_broadcast_binary_normal, bad_runtime = _bad_runtime_broadcast_binary_normal,
grad = _grad_broadcast_binary_normal) grad = _grad_broadcast_binary_normal)
SubInplaceTester = make_broadcast_restet(op = inplace.sub_inplace, SubInplaceTester = makeBroadcastTester(op = inplace.sub_inplace,
expected = lambda x, y: x - y, expected = lambda x, y: x - y,
good = _good_broadcast_binary_normal, good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal, bad_build = _bad_build_broadcast_binary_normal,
...@@ -258,7 +250,7 @@ SubInplaceTester = make_broadcast_restet(op = inplace.sub_inplace, ...@@ -258,7 +250,7 @@ SubInplaceTester = make_broadcast_restet(op = inplace.sub_inplace,
grad = _grad_broadcast_binary_normal, grad = _grad_broadcast_binary_normal,
inplace = True) inplace = True)
MulTester = make_broadcast_restet(op = mul, MulTester = makeBroadcastTester(op = mul,
expected = lambda *inputs: reduce(lambda x, y: x * y, inputs), expected = lambda *inputs: reduce(lambda x, y: x * y, inputs),
good = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)), good = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)),
four_inputs_broadcast = (rand(2, 3), rand(1, 3), rand(2, 1), rand(1, 1)), four_inputs_broadcast = (rand(2, 3), rand(1, 3), rand(2, 1), rand(1, 1)),
...@@ -268,7 +260,7 @@ MulTester = make_broadcast_restet(op = mul, ...@@ -268,7 +260,7 @@ MulTester = make_broadcast_restet(op = mul,
grad = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)), grad = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)),
four_inputs_broadcast = (rand(2, 3), rand(1, 3), rand(2, 1), rand(1, 1)), four_inputs_broadcast = (rand(2, 3), rand(1, 3), rand(2, 1), rand(1, 1)),
**_grad_broadcast_binary_normal)) **_grad_broadcast_binary_normal))
MulInplaceTester = make_broadcast_restet(op = inplace.mul_inplace, MulInplaceTester = makeBroadcastTester(op = inplace.mul_inplace,
expected = lambda x, y: x * y, expected = lambda x, y: x * y,
good = _good_broadcast_binary_normal, good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal, bad_build = _bad_build_broadcast_binary_normal,
...@@ -276,7 +268,7 @@ MulInplaceTester = make_broadcast_restet(op = inplace.mul_inplace, ...@@ -276,7 +268,7 @@ MulInplaceTester = make_broadcast_restet(op = inplace.mul_inplace,
grad = _grad_broadcast_binary_normal, grad = _grad_broadcast_binary_normal,
inplace = True) inplace = True)
DivTester = make_broadcast_restet(op = div, DivTester = makeBroadcastTester(op = div,
expected = lambda x, y: x / y, expected = lambda x, y: x / y,
good = dict(same_shapes = (rand(2, 3), rand(2, 3)), good = dict(same_shapes = (rand(2, 3), rand(2, 3)),
scalar = (rand(2, 3), rand(1, 1)), scalar = (rand(2, 3), rand(1, 1)),
...@@ -294,7 +286,7 @@ DivTester = make_broadcast_restet(op = div, ...@@ -294,7 +286,7 @@ DivTester = make_broadcast_restet(op = div,
scalar = (rand(2, 3), rand(1, 1)), scalar = (rand(2, 3), rand(1, 1)),
row = (rand(2, 3), rand(1, 3)), row = (rand(2, 3), rand(1, 3)),
column = (rand(2, 3), rand(2, 1)))) column = (rand(2, 3), rand(2, 1))))
DivInplaceTester = make_broadcast_restet(op = inplace.div_inplace, DivInplaceTester = makeBroadcastTester(op = inplace.div_inplace,
expected = lambda x, y: x / y, expected = lambda x, y: x / y,
good = dict(same_shapes = (rand(2, 3), rand(2, 3)), good = dict(same_shapes = (rand(2, 3), rand(2, 3)),
scalar = (rand(2, 3), rand(1, 1)), scalar = (rand(2, 3), rand(1, 1)),
...@@ -309,7 +301,7 @@ DivInplaceTester = make_broadcast_restet(op = inplace.div_inplace, ...@@ -309,7 +301,7 @@ DivInplaceTester = make_broadcast_restet(op = inplace.div_inplace,
column = (rand(2, 3), rand(2, 1))), column = (rand(2, 3), rand(2, 1))),
inplace = True) inplace = True)
ModTester = make_broadcast_restet(op = mod, ModTester = makeBroadcastTester(op = mod,
expected = lambda x, y: x % y, expected = lambda x, y: x % y,
good = dict(same_shapes = (rand(2, 3), rand(2, 3)), good = dict(same_shapes = (rand(2, 3), rand(2, 3)),
scalar = (rand(2, 3), rand(1, 1)), scalar = (rand(2, 3), rand(1, 1)),
...@@ -324,7 +316,7 @@ ModTester = make_broadcast_restet(op = mod, ...@@ -324,7 +316,7 @@ ModTester = make_broadcast_restet(op = mod,
# dtype_mixup_1 = (rand(2, 3), randint_nonzero(2, 3)), # dtype_mixup_1 = (rand(2, 3), randint_nonzero(2, 3)),
# dtype_mixup_2 = (randint_nonzero(2, 3), rand(2, 3))), # dtype_mixup_2 = (randint_nonzero(2, 3), rand(2, 3))),
) )
ModInplaceTester = make_broadcast_restet(op = inplace.mod_inplace, ModInplaceTester = makeBroadcastTester(op = inplace.mod_inplace,
expected = lambda x, y: x % y, expected = lambda x, y: x % y,
good = dict(same_shapes = (rand(2, 3), rand(2, 3)), good = dict(same_shapes = (rand(2, 3), rand(2, 3)),
scalar = (rand(2, 3), rand(1, 1)), scalar = (rand(2, 3), rand(1, 1)),
...@@ -335,7 +327,7 @@ ModInplaceTester = make_broadcast_restet(op = inplace.mod_inplace, ...@@ -335,7 +327,7 @@ ModInplaceTester = make_broadcast_restet(op = inplace.mod_inplace,
), ),
inplace = True) inplace = True)
PowTester = make_broadcast_restet(op = pow, PowTester = makeBroadcastTester(op = pow,
expected = lambda x, y: x ** y, expected = lambda x, y: x ** y,
good = dict(same_shapes = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (2, 3))), good = dict(same_shapes = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (2, 3))),
scalar = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (1, 1))), scalar = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (1, 1))),
...@@ -347,7 +339,7 @@ PowTester = make_broadcast_restet(op = pow, ...@@ -347,7 +339,7 @@ PowTester = make_broadcast_restet(op = pow,
row = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (1, 3))), row = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (1, 3))),
column = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (2, 1)))) column = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (2, 1))))
) )
PowInplaceTester = make_broadcast_restet(op = inplace.pow_inplace, PowInplaceTester = makeBroadcastTester(op = inplace.pow_inplace,
expected = lambda x, y: x ** y, expected = lambda x, y: x ** y,
good = dict(same_shapes = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (2, 3))), good = dict(same_shapes = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (2, 3))),
scalar = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (1, 1))), scalar = (rand_ranged(1, 5, (2, 3)), rand_ranged(-3, 3, (1, 1))),
...@@ -368,49 +360,49 @@ _good_broadcast_unary_normal = dict(normal = (rand_ranged(-5, 5, (2, 3)),), ...@@ -368,49 +360,49 @@ _good_broadcast_unary_normal = dict(normal = (rand_ranged(-5, 5, (2, 3)),),
_grad_broadcast_unary_normal = dict(normal = (rand_ranged(-5, 5, (2, 3)),)) _grad_broadcast_unary_normal = dict(normal = (rand_ranged(-5, 5, (2, 3)),))
AbsTester = make_broadcast_restet(op = tensor.abs_, AbsTester = makeBroadcastTester(op = tensor.abs_,
expected = lambda x: abs(x), expected = lambda x: abs(x),
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
AbsInplaceTester = make_broadcast_restet(op = inplace.abs__inplace, AbsInplaceTester = makeBroadcastTester(op = inplace.abs__inplace,
expected = lambda x: numpy.abs(x), expected = lambda x: numpy.abs(x),
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
inplace = True) inplace = True)
NegTester = make_broadcast_restet(op = neg, NegTester = makeBroadcastTester(op = neg,
expected = lambda x: -x, expected = lambda x: -x,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
NegInplaceTester = make_broadcast_restet(op = inplace.neg_inplace, NegInplaceTester = makeBroadcastTester(op = inplace.neg_inplace,
expected = lambda x: -x, expected = lambda x: -x,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
inplace = True) inplace = True)
SgnTester = make_broadcast_restet(op = sgn, SgnTester = makeBroadcastTester(op = sgn,
expected = numpy.sign, expected = numpy.sign,
good = _good_broadcast_unary_normal) good = _good_broadcast_unary_normal)
SgnInplaceTester = make_broadcast_restet(op = inplace.sgn_inplace, SgnInplaceTester = makeBroadcastTester(op = inplace.sgn_inplace,
expected = numpy.sign, expected = numpy.sign,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
inplace = True) inplace = True)
SqrTester = make_broadcast_restet(op = sqr, SqrTester = makeBroadcastTester(op = sqr,
expected = numpy.square, expected = numpy.square,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
SqrInplaceTester = make_broadcast_restet(op = inplace.sqr_inplace, SqrInplaceTester = makeBroadcastTester(op = inplace.sqr_inplace,
expected = numpy.square, expected = numpy.square,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
inplace = True) inplace = True)
ExpTester = make_broadcast_restet(op = exp, ExpTester = makeBroadcastTester(op = exp,
expected = numpy.exp, expected = numpy.exp,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
ExpInplaceTester = make_broadcast_restet(op = inplace.exp_inplace, ExpInplaceTester = makeBroadcastTester(op = inplace.exp_inplace,
expected = numpy.exp, expected = numpy.exp,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
...@@ -422,31 +414,31 @@ _good_broadcast_unary_positive = dict(normal = (rand_ranged(0.001, 5, (2, 3)),), ...@@ -422,31 +414,31 @@ _good_broadcast_unary_positive = dict(normal = (rand_ranged(0.001, 5, (2, 3)),),
_grad_broadcast_unary_positive = dict(normal = (rand_ranged(0.001, 5, (2, 3)),)) _grad_broadcast_unary_positive = dict(normal = (rand_ranged(0.001, 5, (2, 3)),))
LogTester = make_broadcast_restet(op = log, LogTester = makeBroadcastTester(op = log,
expected = numpy.log, expected = numpy.log,
good = _good_broadcast_unary_positive, good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive) grad = _grad_broadcast_unary_positive)
LogInplaceTester = make_broadcast_restet(op = inplace.log_inplace, LogInplaceTester = makeBroadcastTester(op = inplace.log_inplace,
expected = numpy.log, expected = numpy.log,
good = _good_broadcast_unary_positive, good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive, grad = _grad_broadcast_unary_positive,
inplace = True) inplace = True)
Log2Tester = make_broadcast_restet(op = log2, Log2Tester = makeBroadcastTester(op = log2,
expected = numpy.log2, expected = numpy.log2,
good = _good_broadcast_unary_positive, good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive) grad = _grad_broadcast_unary_positive)
Log2InplaceTester = make_broadcast_restet(op = inplace.log2_inplace, Log2InplaceTester = makeBroadcastTester(op = inplace.log2_inplace,
expected = numpy.log2, expected = numpy.log2,
good = _good_broadcast_unary_positive, good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive, grad = _grad_broadcast_unary_positive,
inplace = True) inplace = True)
SqrtTester = make_broadcast_restet(op = sqrt, SqrtTester = makeBroadcastTester(op = sqrt,
expected = numpy.sqrt, expected = numpy.sqrt,
good = _good_broadcast_unary_positive, good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive) grad = _grad_broadcast_unary_positive)
SqrtInplaceTester = make_broadcast_restet(op = inplace.sqrt_inplace, SqrtInplaceTester = makeBroadcastTester(op = inplace.sqrt_inplace,
expected = numpy.sqrt, expected = numpy.sqrt,
good = _good_broadcast_unary_positive, good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive, grad = _grad_broadcast_unary_positive,
...@@ -460,33 +452,33 @@ _good_broadcast_unary_wide = dict(normal = (rand_ranged(-1000, 1000, (2, 3)),), ...@@ -460,33 +452,33 @@ _good_broadcast_unary_wide = dict(normal = (rand_ranged(-1000, 1000, (2, 3)),),
_grad_broadcast_unary_wide = dict(normal = (rand_ranged(-1000, 1000, (2, 3)),)) _grad_broadcast_unary_wide = dict(normal = (rand_ranged(-1000, 1000, (2, 3)),))
SinTester = make_broadcast_restet(op = sin, SinTester = makeBroadcastTester(op = sin,
expected = numpy.sin, expected = numpy.sin,
good = _good_broadcast_unary_wide, good = _good_broadcast_unary_wide,
grad = _grad_broadcast_unary_wide) grad = _grad_broadcast_unary_wide)
SinInplaceTester = make_broadcast_restet(op = inplace.sin_inplace, SinInplaceTester = makeBroadcastTester(op = inplace.sin_inplace,
expected = numpy.sin, expected = numpy.sin,
good = _good_broadcast_unary_wide, good = _good_broadcast_unary_wide,
grad = _grad_broadcast_unary_wide, grad = _grad_broadcast_unary_wide,
inplace = True) inplace = True)
CosTester = make_broadcast_restet(op = cos, CosTester = makeBroadcastTester(op = cos,
expected = numpy.cos, expected = numpy.cos,
good = _good_broadcast_unary_wide, good = _good_broadcast_unary_wide,
grad = _grad_broadcast_unary_wide) grad = _grad_broadcast_unary_wide)
CosInplaceTester = make_broadcast_restet(op = inplace.cos_inplace, CosInplaceTester = makeBroadcastTester(op = inplace.cos_inplace,
expected = numpy.cos, expected = numpy.cos,
good = _good_broadcast_unary_wide, good = _good_broadcast_unary_wide,
grad = _grad_broadcast_unary_wide, grad = _grad_broadcast_unary_wide,
inplace = True) inplace = True)
TanTester = make_broadcast_restet(op = tan, TanTester = makeBroadcastTester(op = tan,
expected = numpy.tan, expected = numpy.tan,
good = dict(normal = (rand_ranged(-3.14, 3.14, (2, 3)),), good = dict(normal = (rand_ranged(-3.14, 3.14, (2, 3)),),
shifted = (rand_ranged(3.15, 6.28, (2, 3)),)), shifted = (rand_ranged(3.15, 6.28, (2, 3)),)),
grad = dict(normal = (rand_ranged(-3.14, 3.14, (2, 3)),), grad = dict(normal = (rand_ranged(-3.14, 3.14, (2, 3)),),
shifted = (rand_ranged(3.15, 6.28, (2, 3)),))) shifted = (rand_ranged(3.15, 6.28, (2, 3)),)))
TanInplaceTester = make_broadcast_restet(op = inplace.tan_inplace, TanInplaceTester = makeBroadcastTester(op = inplace.tan_inplace,
expected = numpy.tan, expected = numpy.tan,
good = dict(normal = (rand_ranged(-3.14, 3.14, (2, 3)),), good = dict(normal = (rand_ranged(-3.14, 3.14, (2, 3)),),
shifted = (rand_ranged(3.15, 6.28, (2, 3)),)), shifted = (rand_ranged(3.15, 6.28, (2, 3)),)),
...@@ -495,31 +487,31 @@ TanInplaceTester = make_broadcast_restet(op = inplace.tan_inplace, ...@@ -495,31 +487,31 @@ TanInplaceTester = make_broadcast_restet(op = inplace.tan_inplace,
inplace = True) inplace = True)
CoshTester = make_broadcast_restet(op = cosh, CoshTester = makeBroadcastTester(op = cosh,
expected = numpy.cosh, expected = numpy.cosh,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
CoshInplaceTester = make_broadcast_restet(op = inplace.cosh_inplace, CoshInplaceTester = makeBroadcastTester(op = inplace.cosh_inplace,
expected = numpy.cosh, expected = numpy.cosh,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
inplace = True) inplace = True)
SinhTester = make_broadcast_restet(op = sinh, SinhTester = makeBroadcastTester(op = sinh,
expected = numpy.sinh, expected = numpy.sinh,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
SinhInplaceTester = make_broadcast_restet(op = inplace.sinh_inplace, SinhInplaceTester = makeBroadcastTester(op = inplace.sinh_inplace,
expected = numpy.sinh, expected = numpy.sinh,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
inplace = True) inplace = True)
TanhTester = make_broadcast_restet(op = tanh, TanhTester = makeBroadcastTester(op = tanh,
expected = numpy.tanh, expected = numpy.tanh,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal) grad = _grad_broadcast_unary_normal)
TanhInplaceTester = make_broadcast_restet(op = inplace.tanh_inplace, TanhInplaceTester = makeBroadcastTester(op = inplace.tanh_inplace,
expected = numpy.tanh, expected = numpy.tanh,
good = _good_broadcast_unary_normal, good = _good_broadcast_unary_normal,
grad = _grad_broadcast_unary_normal, grad = _grad_broadcast_unary_normal,
...@@ -527,7 +519,7 @@ TanhInplaceTester = make_broadcast_restet(op = inplace.tanh_inplace, ...@@ -527,7 +519,7 @@ TanhInplaceTester = make_broadcast_restet(op = inplace.tanh_inplace,
DotTester = make_restet(name = 'DotTester', DotTester = makeTester(name = 'DotTester',
op = dot, op = dot,
expected = lambda x, y: numpy.dot(x, y), expected = lambda x, y: numpy.dot(x, y),
checks = {}, checks = {},
...@@ -930,7 +922,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -930,7 +922,7 @@ class T_Join_and_Split(unittest.TestCase):
want = numpy.array([[1, 2, 3, 7], [4, 5, 6, 8]], dtype='float32') want = numpy.array([[1, 2, 3, 7], [4, 5, 6, 8]], dtype='float32')
self.failUnless((eval_outputs([s]) == want).all()) self.failUnless((eval_outputs([s]) == want).all())
verify_grad(self, lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3) verify_grad(lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3)
def test_join_matrix1_using_vertical_stack(self): def test_join_matrix1_using_vertical_stack(self):
a = as_tensor_variable(numpy.array([[1, 2, 3], [4, 5, 6]])) a = as_tensor_variable(numpy.array([[1, 2, 3], [4, 5, 6]]))
...@@ -952,7 +944,7 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -952,7 +944,7 @@ class T_Join_and_Split(unittest.TestCase):
want = numpy.array([[1, 2, 3, 7, 3, 2, 1], [4, 5, 6, 8, 6, 5, 4]], dtype='float32') want = numpy.array([[1, 2, 3, 7, 3, 2, 1], [4, 5, 6, 8, 6, 5, 4]], dtype='float32')
self.failUnless((eval_outputs([s]) == want).all()) self.failUnless((eval_outputs([s]) == want).all())
verify_grad(self, lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3) verify_grad(lambda a, b: join(1,a,b), [av, bv], eps=1.0e-4, tol=1.0e-3)
def test_join_matrixV(self): def test_join_matrixV(self):
"""variable join axis""" """variable join axis"""
...@@ -972,8 +964,8 @@ class T_Join_and_Split(unittest.TestCase): ...@@ -972,8 +964,8 @@ class T_Join_and_Split(unittest.TestCase):
got = f(1) got = f(1)
self.failUnless((got == want).all(), (got, want)) self.failUnless((got == want).all(), (got, want))
verify_grad(self, lambda a, b: join(0,a,b), [v, 2*v]) verify_grad(lambda a, b: join(0,a,b), [v, 2*v])
verify_grad(self, lambda a, b: join(1,a,b), [v, 2*v]) verify_grad(lambda a, b: join(1,a,b), [v, 2*v])
def test_vector_len(self): def test_vector_len(self):
x = lscalar('x') x = lscalar('x')
...@@ -1096,22 +1088,22 @@ class T_add(unittest.TestCase): ...@@ -1096,22 +1088,22 @@ class T_add(unittest.TestCase):
self.failUnless(a.type.values_eq_approx(fn(a.data, b.data), f(a.data, b.data))) self.failUnless(a.type.values_eq_approx(fn(a.data, b.data), f(a.data, b.data)))
def test_grad_scalar_l(self): def test_grad_scalar_l(self):
verify_grad(self, add, [numpy.asarray([3.0]), numpy.random.rand(3)]) verify_grad(add, [numpy.asarray([3.0]), numpy.random.rand(3)])
def test_grad_scalar_r(self): def test_grad_scalar_r(self):
verify_grad(self, add, [numpy.random.rand(3), numpy.asarray([3.0])]) verify_grad(add, [numpy.random.rand(3), numpy.asarray([3.0])])
def test_grad_row(self): def test_grad_row(self):
verify_grad(self, add, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
def test_grad_col(self): def test_grad_col(self):
verify_grad(self, add, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
class T_exp(unittest.TestCase): class T_exp(unittest.TestCase):
def test_grad_0(self): def test_grad_0(self):
verify_grad(self, exp, [ verify_grad(exp, [
numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ], numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ],
[ 2.04832468, 0.50791564, -1.58892269]])]) [ 2.04832468, 0.50791564, -1.58892269]])])
def test_grad_1(self): def test_grad_1(self):
verify_grad(self, inplace.exp_inplace, [ verify_grad(inplace.exp_inplace, [
numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ], numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ],
[ 2.04832468, 0.50791564, -1.58892269]])]) [ 2.04832468, 0.50791564, -1.58892269]])])
...@@ -1128,8 +1120,8 @@ class T_exp(unittest.TestCase): ...@@ -1128,8 +1120,8 @@ class T_exp(unittest.TestCase):
# check_eq(self, t, abs(t), -d, abs(-d)) # check_eq(self, t, abs(t), -d, abs(-d))
# def test_grad(self): # def test_grad(self):
# verify_grad(self, Abs, [numpy.ones(())]) # verify_grad(Abs, [numpy.ones(())])
# verify_grad(self, Abs, [numpy.ones(3)]) # verify_grad(Abs, [numpy.ones(3)])
# class AbsBadGrad(Abs): # class AbsBadGrad(Abs):
# def grad(self, (x, ), (gz, )): # def grad(self, (x, ), (gz, )):
...@@ -1137,7 +1129,7 @@ class T_exp(unittest.TestCase): ...@@ -1137,7 +1129,7 @@ class T_exp(unittest.TestCase):
# def test_badgrad(self): # def test_badgrad(self):
# try: # try:
# verify_grad(self, T_abs.AbsBadGrad, [numpy.ones(())]) # verify_grad(T_abs.AbsBadGrad, [numpy.ones(())])
# except Exception, e: # except Exception, e:
# self.failUnless(str(e) == verify_grad.E_grad, str(e)) # self.failUnless(str(e) == verify_grad.E_grad, str(e))
# return # return
...@@ -1208,18 +1200,18 @@ class T_exp(unittest.TestCase): ...@@ -1208,18 +1200,18 @@ class T_exp(unittest.TestCase):
# check_eq2_both(self, [a1,a3], mul(a1,a3), [r1, r3], r1*r3) # check_eq2_both(self, [a1,a3], mul(a1,a3), [r1, r3], r1*r3)
# def test_grad_elemwise(self): # def test_grad_elemwise(self):
# verify_grad(self, Mul, [numpy.random.rand(3,4), numpy.random.rand(3,4)]) # verify_grad(Mul, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
# def test_grad_scalar_l(self): # def test_grad_scalar_l(self):
# verify_grad(self, Mul, [numpy.asarray([3.0]), numpy.random.rand(3)]) # verify_grad(Mul, [numpy.asarray([3.0]), numpy.random.rand(3)])
# def test_grad_scalar_r(self): # def test_grad_scalar_r(self):
# verify_grad(self, Mul, [numpy.random.rand(3), numpy.asarray([3.0])]) # verify_grad(Mul, [numpy.random.rand(3), numpy.asarray([3.0])])
# def test_grad_row(self): # def test_grad_row(self):
# verify_grad(self, Mul, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) # verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
# def test_grad_row2(self): # def test_grad_row2(self):
# op = lambda x, y: Mul(x, DimShuffle(y, ['x', 0]).out) # op = lambda x, y: Mul(x, DimShuffle(y, ['x', 0]).out)
# verify_grad(self, op, [numpy.random.rand(3, 5), numpy.random.rand(5)]) # verify_grad(op, [numpy.random.rand(3, 5), numpy.random.rand(5)])
# def test_grad_col(self): # def test_grad_col(self):
# verify_grad(self, Mul, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) # verify_grad(Mul, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
# def test_wrong_shapes(self): # def test_wrong_shapes(self):
# a = as_tensor_variable(numpy.ones(3)) # a = as_tensor_variable(numpy.ones(3))
...@@ -1241,26 +1233,26 @@ class T_exp(unittest.TestCase): ...@@ -1241,26 +1233,26 @@ class T_exp(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # unittest_tools.seed_rng()
# def test_grad_e(self): # def test_grad_e(self):
# verify_grad(self, Div, [numpy.random.rand(3), numpy.ones(3)]) # verify_grad(Div, [numpy.random.rand(3), numpy.ones(3)])
# verify_grad(self, Div, [numpy.random.rand(3,5), numpy.random.rand(3,5)+0.1]) # verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(3,5)+0.1])
# verify_grad(self, Div, [numpy.ones(()), numpy.ones(())]) # verify_grad(Div, [numpy.ones(()), numpy.ones(())])
# def test_grad_sl(self): # def test_grad_sl(self):
# verify_grad(self, Div, [numpy.ones((3, 5)), numpy.ones((1, 1))]) # verify_grad(Div, [numpy.ones((3, 5)), numpy.ones((1, 1))])
# verify_grad(self, Div, [numpy.random.rand(3), numpy.ones((1, ))]) # verify_grad(Div, [numpy.random.rand(3), numpy.ones((1, ))])
# verify_grad(self, Div, [numpy.random.rand(3,5), numpy.random.rand(1,1)]) # verify_grad(Div, [numpy.random.rand(3,5), numpy.random.rand(1,1)])
# class T_log2(unittest.TestCase): # class T_log2(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # unittest_tools.seed_rng()
# def test0(self): # def test0(self):
# verify_grad(self, Log2, [numpy.random.rand(3,1)+0.0001]) # verify_grad(Log2, [numpy.random.rand(3,1)+0.0001])
# class T_log(unittest.TestCase): # class T_log(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # unittest_tools.seed_rng()
# def test0(self): # def test0(self):
# verify_grad(self, Log, [numpy.random.rand(3,1)+0.0001]) # verify_grad(Log, [numpy.random.rand(3,1)+0.0001])
# def test1(self): # def test1(self):
# a = as_tensor_variable(numpy.ones(2)) # a = as_tensor_variable(numpy.ones(2))
# b = as_tensor_variable(numpy.ones(2)) # b = as_tensor_variable(numpy.ones(2))
...@@ -1272,21 +1264,21 @@ class T_exp(unittest.TestCase): ...@@ -1272,21 +1264,21 @@ class T_exp(unittest.TestCase):
# def setUp(self): # def setUp(self):
# unittest_tools.seed_rng() # unittest_tools.seed_rng()
# def test_elemwise(self): # def test_elemwise(self):
# verify_grad(self, Div, [numpy.random.rand(3,4), numpy.random.rand(3,4)+0.1]) # verify_grad(Div, [numpy.random.rand(3,4), numpy.random.rand(3,4)+0.1])
# verify_grad(self, Pow, [numpy.random.rand(3,4), numpy.random.rand(3,4)]) # verify_grad(Pow, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
# def test_scalar_l(self): # def test_scalar_l(self):
# verify_grad(self, Pow, [numpy.asarray([3.0]), numpy.random.rand(3)]) # verify_grad(Pow, [numpy.asarray([3.0]), numpy.random.rand(3)])
# def test_scalar_r(self): # def test_scalar_r(self):
# verify_grad(self, Pow, [numpy.random.rand(3), numpy.asarray([3.0])]) # verify_grad(Pow, [numpy.random.rand(3), numpy.asarray([3.0])])
# def test_row(self): # def test_row(self):
# verify_grad(self, Pow, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)]) # verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(1, 5)])
# def test_col(self): # def test_col(self):
# verify_grad(self, Pow, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)]) # verify_grad(Pow, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
class test_matinv(unittest.TestCase): class test_matinv(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng(1) unittest_tools.seed_rng()
def mat_reciprocal(self,dim): def mat_reciprocal(self,dim):
# symbolic program # symbolic program
...@@ -1390,13 +1382,13 @@ class t_dot(unittest.TestCase): ...@@ -1390,13 +1382,13 @@ class t_dot(unittest.TestCase):
#def test_align_3_3(self): self.not_aligned(self.rand(5,4,3), self.rand(6,7,8)) #def test_align_3_3(self): self.not_aligned(self.rand(5,4,3), self.rand(6,7,8))
def test_grad(self): def test_grad(self):
#verify_grad(self, dot, [self.rand(2,3,4), self.rand(4)]) #verify_grad(dot, [self.rand(2,3,4), self.rand(4)])
verify_grad(self, dot, [self.rand(2,3), self.rand(3,2)]) verify_grad(dot, [self.rand(2,3), self.rand(3,2)])
verify_grad(self, dot, [self.rand(2), self.rand(2,3)]) verify_grad(dot, [self.rand(2), self.rand(2,3)])
verify_grad(self, dot, [self.rand(3,2), self.rand(2)]) verify_grad(dot, [self.rand(3,2), self.rand(2)])
verify_grad(self, dot, [self.rand(2), self.rand(2)]) verify_grad(dot, [self.rand(2), self.rand(2)])
#verify_grad(self, dot, [self.rand(), self.rand(2)]) #verify_grad(dot, [self.rand(), self.rand(2)])
#verify_grad(self, dot, [self.rand(), self.rand(2,5)]) #verify_grad(dot, [self.rand(), self.rand(2,5)])
class T_tensorfromscalar(unittest.TestCase): class T_tensorfromscalar(unittest.TestCase):
def test0(self): def test0(self):
...@@ -1658,7 +1650,7 @@ def test_reshape(): ...@@ -1658,7 +1650,7 @@ def test_reshape():
assert numpy.all(a_val == a_val_copy) assert numpy.all(a_val == a_val_copy)
# verify gradient # verify gradient
tensor.verify_grad(None, Reshape(2), [a_val,numpy.asarray([2,3], dtype='float64')]) tensor.verify_grad(Reshape(2), [a_val,numpy.asarray([2,3], dtype='float64')])
def test_flatten_outdimNone(): def test_flatten_outdimNone():
...@@ -1674,7 +1666,7 @@ def test_flatten_outdimNone(): ...@@ -1674,7 +1666,7 @@ def test_flatten_outdimNone():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
tensor.verify_grad(None, Flatten(), [a_val]) tensor.verify_grad(Flatten(), [a_val])
def test_flatten_scalar(): def test_flatten_scalar():
a = dscalar() a = dscalar()
...@@ -1686,7 +1678,7 @@ def test_flatten_scalar(): ...@@ -1686,7 +1678,7 @@ def test_flatten_scalar():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
#tensor.verify_grad(None, Flatten(), [a_val]) #TODO: fix verify_grd to work on scalars #tensor.verify_grad(Flatten(), [a_val]) #TODO: fix verify_grd to work on scalars
def test_flatten_outdim1(): def test_flatten_outdim1():
a = dmatrix() a = dmatrix()
...@@ -1698,7 +1690,7 @@ def test_flatten_outdim1(): ...@@ -1698,7 +1690,7 @@ def test_flatten_outdim1():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
tensor.verify_grad(None, Flatten(1), [a_val]) tensor.verify_grad(Flatten(1), [a_val])
def test_flatten_outdim2(): def test_flatten_outdim2():
a = dmatrix() a = dmatrix()
...@@ -1709,7 +1701,7 @@ def test_flatten_outdim2(): ...@@ -1709,7 +1701,7 @@ def test_flatten_outdim2():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==a_val) assert numpy.all(f(a_val)==a_val)
tensor.verify_grad(None, Flatten(2), [a_val]) tensor.verify_grad(Flatten(2), [a_val])
def test_flatten_outdim2_of_3(): def test_flatten_outdim2_of_3():
a = TensorType('float64', (False, False, False))() a = TensorType('float64', (False, False, False))()
...@@ -1721,7 +1713,7 @@ def test_flatten_outdim2_of_3(): ...@@ -1721,7 +1713,7 @@ def test_flatten_outdim2_of_3():
f = inplace_func([a], c) f = inplace_func([a], c)
assert numpy.all(f(a_val)==c_val) assert numpy.all(f(a_val)==c_val)
tensor.verify_grad(None, Flatten(2), [a_val]) tensor.verify_grad(Flatten(2), [a_val])
def test_flatten_outdim_invalid(): def test_flatten_outdim_invalid():
a = dmatrix() a = dmatrix()
...@@ -1758,7 +1750,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1758,7 +1750,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(5); bval = numpy.random.rand(5);
self.failUnless(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.tensordot(aval,bval,axes) == \
f1(aval,bval)) f1(aval,bval))
tensor.verify_grad(None, TensorDot(axes), [aval,bval]) tensor.verify_grad(TensorDot(axes), [aval,bval])
# test matrix-vector # test matrix-vector
bmat = dmatrix() bmat = dmatrix()
...@@ -1769,7 +1761,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1769,7 +1761,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(8,5); bval = numpy.random.rand(8,5);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f2(aval,bval))) f2(aval,bval)))
tensor.verify_grad(None, TensorDot(axes), [aval,bval]) tensor.verify_grad(TensorDot(axes), [aval,bval])
# test matrix-matrix # test matrix-matrix
amat = dmatrix() amat = dmatrix()
...@@ -1780,7 +1772,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1780,7 +1772,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(7,9); bval = numpy.random.rand(7,9);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f3(aval,bval))) f3(aval,bval)))
tensor.verify_grad(None, TensorDot(axes), [aval,bval]) tensor.verify_grad(TensorDot(axes), [aval,bval])
# test ndarray-matrix, sum over one dim of matrix # test ndarray-matrix, sum over one dim of matrix
atens = TensorType('float64', broadcastable=(False,)*4)() atens = TensorType('float64', broadcastable=(False,)*4)()
...@@ -1791,7 +1783,7 @@ class test_tensordot(unittest.TestCase): ...@@ -1791,7 +1783,7 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(2,3); bval = numpy.random.rand(2,3);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f4(aval,bval))) f4(aval,bval)))
tensor.verify_grad(None, TensorDot(axes), [aval,bval]) tensor.verify_grad(TensorDot(axes), [aval,bval])
# test ndarray-ndarray # test ndarray-ndarray
atens = TensorType('float64', broadcastable=(False,)*4)() atens = TensorType('float64', broadcastable=(False,)*4)()
...@@ -1803,14 +1795,14 @@ class test_tensordot(unittest.TestCase): ...@@ -1803,14 +1795,14 @@ class test_tensordot(unittest.TestCase):
bval = numpy.random.rand(3,4,2); bval = numpy.random.rand(3,4,2);
self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(aval,bval,axes) == \
f5(aval,bval))) f5(aval,bval)))
tensor.verify_grad(None, TensorDot(axes), [aval,bval]) tensor.verify_grad(TensorDot(axes), [aval,bval])
axes = (axes[1],axes[0]) axes = (axes[1],axes[0])
c = tensordot(axes)(btens, atens) c = tensordot(axes)(btens, atens)
f6 = inplace_func([btens,atens],c) f6 = inplace_func([btens,atens],c)
self.failUnless(numpy.all(numpy.tensordot(bval,aval,axes) == \ self.failUnless(numpy.all(numpy.tensordot(bval,aval,axes) == \
f6(bval,aval))) f6(bval,aval)))
tensor.verify_grad(None, TensorDot(axes), [bval,aval]) tensor.verify_grad(TensorDot(axes), [bval,aval])
def test_smallest_stack(): def test_smallest_stack():
sx, sy = dscalar(), dscalar() sx, sy = dscalar(), dscalar()
......
...@@ -14,13 +14,13 @@ class T_sigmoid(unittest.TestCase): ...@@ -14,13 +14,13 @@ class T_sigmoid(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() unittest_tools.seed_rng()
def test_elemwise(self): def test_elemwise(self):
TT.verify_grad(self, sigmoid, [numpy.random.rand(3,4)]) TT.verify_grad(sigmoid, [numpy.random.rand(3,4)])
class T_softplus(unittest.TestCase): class T_softplus(unittest.TestCase):
def setUp(self): def setUp(self):
unittest_tools.seed_rng() unittest_tools.seed_rng()
def test_elemwise(self): def test_elemwise(self):
TT.verify_grad(self, softplus, [numpy.random.rand(3,4)]) TT.verify_grad(softplus, [numpy.random.rand(3,4)])
class T_Softmax(unittest.TestCase): class T_Softmax(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -28,19 +28,19 @@ class T_Softmax(unittest.TestCase): ...@@ -28,19 +28,19 @@ class T_Softmax(unittest.TestCase):
def test0(self): def test0(self):
def f(a): def f(a):
return softmax(a)[:,0] return softmax(a)[:,0]
TT.verify_grad(self, f, [numpy.random.rand(3,4)]) TT.verify_grad(f, [numpy.random.rand(3,4)])
def test1(self): def test1(self):
def f(a): def f(a):
return softmax(a)[:,1] return softmax(a)[:,1]
TT.verify_grad(self, f, [numpy.random.rand(3,4)]) TT.verify_grad(f, [numpy.random.rand(3,4)])
def test2(self): def test2(self):
def f(a): def f(a):
return softmax(a)[:,2] return softmax(a)[:,2]
TT.verify_grad(self, f, [numpy.random.rand(3,4)]) TT.verify_grad(f, [numpy.random.rand(3,4)])
def test3(self): def test3(self):
def f(a): def f(a):
return softmax(a)[:,3] return softmax(a)[:,3]
TT.verify_grad(self, f, [numpy.random.rand(3,4)]) TT.verify_grad(f, [numpy.random.rand(3,4)])
class T_SoftmaxWithBias(unittest.TestCase): class T_SoftmaxWithBias(unittest.TestCase):
...@@ -49,22 +49,22 @@ class T_SoftmaxWithBias(unittest.TestCase): ...@@ -49,22 +49,22 @@ class T_SoftmaxWithBias(unittest.TestCase):
def test0(self): def test0(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,0] return softmax_with_bias(a, b)[:,0]
TT.verify_grad(self, f, [numpy.random.rand(3,4), TT.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test1(self): def test1(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,1] return softmax_with_bias(a, b)[:,1]
TT.verify_grad(self, f, [numpy.random.rand(3,4), TT.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test2(self): def test2(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,2] return softmax_with_bias(a, b)[:,2]
TT.verify_grad(self, f, [numpy.random.rand(3,4), TT.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test3(self): def test3(self):
def f(a, b): def f(a, b):
return softmax_with_bias(a, b)[:,3] return softmax_with_bias(a, b)[:,3]
TT.verify_grad(self, f, [numpy.random.rand(3,4), TT.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
class T_CrossentropySoftmax1Hot(unittest.TestCase): class T_CrossentropySoftmax1Hot(unittest.TestCase):
...@@ -74,13 +74,13 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase): ...@@ -74,13 +74,13 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
y_idx = [0,1,3] y_idx = [0,1,3]
def f(a, b): def f(a, b):
return crossentropy_softmax_1hot_with_bias(a, b, y_idx)[0] return crossentropy_softmax_1hot_with_bias(a, b, y_idx)[0]
TT.verify_grad(self, f, [numpy.random.rand(3,4), TT.verify_grad(f, [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test1(self): def test1(self):
y_idx = [0,1,3] y_idx = [0,1,3]
def f(a): def f(a):
return crossentropy_softmax_1hot(a, y_idx)[0] return crossentropy_softmax_1hot(a, y_idx)[0]
TT.verify_grad(self, f, [numpy.random.rand(3,4)]) TT.verify_grad(f, [numpy.random.rand(3,4)])
class T_prepend(unittest.TestCase): class T_prepend(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -23,7 +23,7 @@ class T_XlogX(unittest.TestCase): ...@@ -23,7 +23,7 @@ class T_XlogX(unittest.TestCase):
# class Dummy(object): # class Dummy(object):
# def make_node(self, a): # def make_node(self, a):
# return [xlogx(a)[:,2]] # return [xlogx(a)[:,2]]
TT.verify_grad(self, xlogx, [numpy.random.rand(3,4)]) TT.verify_grad(xlogx, [numpy.random.rand(3,4)])
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论