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
shape
anddtype
.- Parameters
func (function or Function) – Chainer function to be tested by the decorated test class. Taking
Function
is for backward compatibility.func_expected – Function used to provide expected values for testing forward computation. If not given, a corresponsing numpy function for
func
is implicitly picked up by its name.label_expected (string) – String used to test labels of Chainer functions. If not given, the name of
func
is implicitly used.make_data – Function to customize input and gradient data used in the tests. It takes
shape
anddtype
as 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
func
is a linear function so that it wrapsfunc
as 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_linear
in 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.float32
andnumpy.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
dtype
of 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 withdtype
of 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
TestSin
class by the decorator, it is enough to placepass
in the class definition.To customize test data,
make_data
optional parameter can be used. The following is an example of testingsqrt
Chainer 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_data
function which returns input, gradient and double gradient data generated in proper value domains with givenshape
anddtype
parameters is defined, then passed to the decorator’smake_data
parameter.