assert_close
- paddle.testing. assert_close ( actual: Any, expected: Any, *, allow_subclasses: bool = True, rtol: Optional[float] = None, atol: Optional[float] = None, equal_nan: bool = False, check_device: bool = True, check_dtype: bool = True, msg: Optional[Union[str, Callable[[str], str]]] = None ) None [source]
-
Asserts that
actualandexpectedare close.If
actualandexpectedare real-valued, and finite, they are considered close if\[\lvert \text{actual} - \text{expected} \rvert \le \texttt{atol} + \texttt{rtol} \cdot \lvert \text{expected} \rvert\]Non-finite values (
-infandinf) are only considered close if and only if they are equal.NaN’s are only considered equal to each other ifequal_nanisTrue.In addition, they are only considered close if they have the same
place(ifcheck_deviceisTrue),dtype(ifcheck_dtypeisTrue),
In static graph mode, only the check_dtype attribute verification will be performed.
actualandexpectedcan beTensor’s or any tensor-or-scalar-likes from whichpaddle.Tensor’s can be constructed withpaddle.to_tensor(). Except for Python scalars the input types have to be directly related. In addition,actualandexpectedcan beSequence’s orMapping’s in which case they are considered close if their structure matches and all their elements are considered close according to the above definition.Note
Python scalars are an exception to the type relation requirement, because their
type(), i.e.int,float, andcomplex, is equivalent to thedtypeof a tensor-like. Thus, Python scalars of different types can be checked, but requirecheck_dtype=False.- Parameters
-
actual (Any) – Actual input.
expected (Any) – Expected input.
allow_subclasses (bool) – If
True(default) and except for Python scalars, inputs of directly related types are allowed. Otherwise type equality is required.rtol (float, optional) – Relative tolerance. If specified
atolmust also be specified. If omitted, default values based on thedtypeare selected with the below table.atol (float, optional) – Absolute tolerance. If specified
rtolmust also be specified. If omitted, default values based on thedtypeare selected with the below table.equal_nan (bool|str, optional) – If
True, twoNaNvalues will be considered equal.check_device (bool) – If
True(default), asserts that corresponding tensors are on the sameplace. If this check is disabled, tensors on differentplace’s are moved to the CPU before being compared.check_dtype (bool) – If
True(default), asserts that corresponding tensors have the samedtype. If this check is disabled, tensors with differentdtype’s are promoted to a commondtypebefore being compared.msg (str|Callable[[str], str], optional) – Optional error message to use in case a failure occurs during the comparison. Can also be passed as callable in which case it will be called with the generated message and should return the new message.
The following table displays the default
rtolandatolfor differentdtype’s. In case of mismatchingdtype’s, the maximum of both tolerances is used.dtypertolatolfloat161e-31e-5bfloat161.6e-21e-5float321.3e-61e-5float641e-71e-7complex641.3e-61e-5complex1281e-71e-7other
0.00.0Note
This function is highly configurable with strict default settings. Users are encouraged to
partial()it to fit their use case.Examples
>>> import paddle >>> import numpy as np >>> import functools >>> # tensor to tensor comparison >>> expected = paddle.to_tensor([1e0, 1e-1, 1e-2]) >>> actual = paddle.acos(paddle.cos(expected)) >>> paddle.testing.assert_close(actual, expected) >>> # scalar to scalar comparison >>> import math >>> expected = math.sqrt(2.0) >>> actual = 2.0 / math.sqrt(2.0) >>> paddle.testing.assert_close(actual, expected) >>> # numpy array to numpy array comparison >>> expected = np.array([1e0, 1e-1, 1e-2]) >>> actual = np.arccos(np.cos(expected)) >>> paddle.testing.assert_close(actual, expected) >>> # sequence to sequence comparison >>> # The types of the sequences do not have to match. They only have to have the same >>> # length and their elements have to match. >>> expected = [paddle.to_tensor([1.0]), 2.0, np.array(3.0)] >>> actual = tuple(expected) >>> paddle.testing.assert_close(actual, expected) >>> # mapping to mapping comparison >>> from collections import OrderedDict >>> foo = paddle.to_tensor(1.0) >>> bar = 2.0 >>> baz = np.array(3.0) >>> # The types and a possible ordering of mappings do not have to match. They only >>> # have to have the same set of keys and their elements have to match. >>> expected = OrderedDict([("foo", foo), ("bar", bar), ("baz", baz)]) >>> actual = {"baz": baz, "bar": bar, "foo": foo} >>> paddle.testing.assert_close(actual, expected) >>> # Customize the error message >>> expected = paddle.to_tensor([1.0, 2.0, 3.0]) >>> actual = paddle.to_tensor([1.0, 4.0, 5.0]) >>> try: ... paddle.testing.assert_close(actual, expected, msg="Argh, the tensors are not close!") ... except AssertionError as e: ... print(e) Argh, the tensors are not close! >>> # Using functools to create strict equality check >>> assert_equal = functools.partial(paddle.testing.assert_close, rtol=0, atol=0) >>> try: ... assert_equal(1e-9, 1e-10) ... except AssertionError as e: ... print(e) Scalars are not equal! Expected 1e-10 but got 1e-09. Absolute difference: 9.000000000000001e-10 Relative difference: 9.0 >>> # NaN check >>> expected = paddle.to_tensor(float("Nan")) >>> actual = expected.clone() >>> # NaN != NaN by default, so this raises AssertionError >>> try: ... paddle.testing.assert_close(actual, expected) ... except AssertionError as e: ... print("Assertion Failed") Assertion Failed >>> # Pass equal_nan=True to succeed >>> paddle.testing.assert_close(actual, expected, equal_nan=True)
