chainer.testing.unary_math_function_unittest¶
-
chainer.testing.unary_math_function_unittest(func, func_expected=None, label_expected=None, make_data=None, is_linear=None, forward_options=None, backward_options=None, double_backward_options=None)[source]¶ Decorator for testing unary mathematical Chainer functions.
This decorator makes test classes test unary mathematical Chainer functions. Tested are forward and backward, including double backward, computations on CPU and GPU across parameterized
shapeanddtype.- Parameters
func (function or Function) – Chainer function to be tested by the decorated test class. Taking
Functionis for backward compatibility.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 name.label_expected (string) – String used to test labels of Chainer functions. If not given, the name of
funcis 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, gradient and double gradient data. By default, uniform destribution ranged[-1, 1]is used for all of them.is_linear – Tells the decorator that
funcis a linear function so that it wrapsfuncas a non-linear function to perform double backward test. This argument is left for backward compatibility. Linear functions can be tested by default without specifyingis_linearin Chainer v5 or later.forward_options (dict) – Options to be specified as an argument of
chainer.testing.assert_allclose()function. If not given, preset tolerance values are automatically selected.backward_options (dict) – Options to be specified as an argument of
chainer.gradient_check.check_backward()function. If not given, preset tolerance values are automatically selected depending ondtype.double_backward_options (dict) – Options to be specified as an argument of
chainer.gradient_check.check_double_backward()function. If not given, preset tolerance values are automatically selected depending ondtype.
The decorated test class tests forward, backward and double 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 sameElementwise 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.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) ... ggx = numpy.random.uniform(-1, 1, shape).astype(dtype) ... return x, gy, ggx ... >>> @testing.unary_math_function_unittest(F.sqrt, ... make_data=make_data) ... class TestSqrt(unittest.TestCase): ... pass
make_datafunction which returns input, gradient and double gradient data generated in proper value domains with givenshapeanddtypeparameters is defined, then passed to the decorator’smake_dataparameter.