/
hyperbolic.py
78 lines (56 loc) · 1.73 KB
/
hyperbolic.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from chainer import backend
from chainer import function_node
from chainer import utils
from chainer.utils import type_check
class Cosh(function_node.FunctionNode):
@property
def label(self):
return 'cosh'
def check_type_forward(self, in_types):
type_check._argname(in_types, ('x',))
type_check.expect(in_types[0].dtype.kind == 'f')
def forward(self, x):
self.retain_inputs((0,))
xp = backend.get_array_module(*x)
return utils.force_array(xp.cosh(x[0])),
def backward(self, indexes, gy):
x = self.get_retained_inputs()
gx = sinh(x[0])
gx *= gy[0]
return gx,
def cosh(x):
"""Elementwise hyperbolic cosine function.
.. math::
y_i = \\cosh x_i.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable.
Returns:
~chainer.Variable: Output variable.
"""
return Cosh().apply((x,))[0]
class Sinh(function_node.FunctionNode):
@property
def label(self):
return 'sinh'
def check_type_forward(self, in_types):
type_check._argname(in_types, ('x',))
type_check.expect(in_types[0].dtype.kind == 'f')
def forward(self, x):
self.retain_inputs((0,))
xp = backend.get_array_module(*x)
return utils.force_array(xp.sinh(x[0])),
def backward(self, x, gy):
x = self.get_retained_inputs()
gx = cosh(x[0])
gx *= gy[0]
return gx,
def sinh(x):
"""Elementwise hyperbolic sine function.
.. math::
y_i = \\sinh x_i.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable.
Returns:
~chainer.Variable: Output variable.
"""
return Sinh().apply((x,))[0]