-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
absolute_error.py
52 lines (38 loc) · 1.42 KB
/
absolute_error.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from chainer import backend
from chainer import function_node
from chainer import utils
from chainer.utils import type_check
class AbsoluteError(function_node.FunctionNode):
"""Element-wise absolute error function."""
def check_type_forward(self, in_types):
type_check._argname(in_types, ('x0', 'x1'))
type_check.expect(
in_types[0].dtype.kind == 'f',
in_types[0].dtype == in_types[1].dtype,
in_types[0].shape == in_types[1].shape
)
def forward(self, inputs):
x0, x1 = inputs
self.diff = x0 - x1
return utils.force_array(abs(self.diff), dtype=x0.dtype),
def backward(self, indexes, grad_outputs):
gy, = grad_outputs
gx = gy * backend.get_array_module(gy).sign(self.diff)
return gx, -gx
def absolute_error(x0, x1):
"""Element-wise absolute error function.
Computes the element-wise absolute error :math:`L` between two inputs
:math:`x_0` and :math:`x_1` defined as follows.
.. math::
L = |x_0 - x_1|
Args:
x0 (:class:`~chainer.Variable` or :ref:`ndarray`):
First input variable.
x1 (:class:`~chainer.Variable` or :ref:`ndarray`):
Second input variable.
Returns:
~chainer.Variable:
An array representing the element-wise absolute error between the
two inputs.
"""
return AbsoluteError().apply((x0, x1))[0]