chainer.testing.unary_math_function_unittest¶
-
chainer.testing.unary_math_function_unittest(func, func_expected=None, label_expected=None, make_data=None)[source]¶ Decorator for testing unary mathematical Chainer functions.
This decorator makes test classes test unary mathematical Chainer functions. Tested are forward and backward computations on CPU and GPU across parameterized
shapeanddtype.Parameters: - func (Function) – Chainer function to be tested by the decorated test class.
- func_expected – Function used to provide expected values for
testing forward computation. If not given, a corresponsing numpy
function for
funcis implicitly picked up by its class name. - label_expected (string) – String used to test labels of Chainer
functions. If not given, the class name of
funclowered is implicitly used. - make_data – Function to customize input and gradient data used
in the tests. It takes
shapeanddtypeas its arguments, and returns a tuple of input and gradient data. By default, uniform destribution ranged[-1, 1]is used for both.
The decorated test class tests forward and backward computations on CPU and GPU across the following
parameterize()ed parameters:- shape: rank of zero, and rank of more than zero
- dtype:
numpy.float16,numpy.float32andnumpy.float64
Additionally, it tests the label of the Chainer function.
Chainer functions tested by the test class decorated with the decorator should have the following properties:
- Unary, taking one parameter and returning one value
dtypeof input and output are the same- Elementwise operation for the supplied ndarray
Example
The following code defines a test class that tests
sin()Chainer function, which takes a parameter withdtypeof float and returns a value with the samedtype.>>> import unittest >>> from chainer import testing >>> from chainer import functions as F >>> >>> @testing.unary_math_function_unittest(F.Sin()) ... class TestSin(unittest.TestCase): ... pass
Because the test methods are implicitly injected to
TestSinclass by the decorator, it is enough to placepassin the class definition.Now the test is run with
nosemodule.>>> import nose >>> nose.run( ... defaultTest=__name__, argv=['', '-a', '!gpu'], exit=False) True
To customize test data,
make_dataoptional parameter can be used. The following is an example of testingsqrtChainer function, which is tested in positive value domain here instead of the default input.>>> import numpy >>> >>> def make_data(shape, dtype): ... x = numpy.random.uniform(0.1, 1, shape).astype(dtype) ... gy = numpy.random.uniform(-1, 1, shape).astype(dtype) ... return x, gy ... >>> @testing.unary_math_function_unittest(F.Sqrt(), ... make_data=make_data) ... class TestSqrt(unittest.TestCase): ... pass ... >>> nose.run( ... defaultTest=__name__, argv=['', '-a', '!gpu'], exit=False) True
make_datafunction which returns input and gradient data generated in proper value domains with givenshapeanddtypeparameters is defined, then passed to the decorator’smake_dataparameter.