Calculates the hyperbolic tangent of the given input tensor element-wise.
| 1919 | |
| 1920 | |
| 1921 | class Tanh(Operator): |
| 1922 | """ |
| 1923 | Calculates the hyperbolic tangent of the given input tensor element-wise. |
| 1924 | """ |
| 1925 | |
| 1926 | def __init__(self): |
| 1927 | super(Tanh, self).__init__() |
| 1928 | |
| 1929 | def forward(self, x): |
| 1930 | """ |
| 1931 | Args: |
| 1932 | x (CTensor): Input tensor |
| 1933 | Returns: |
| 1934 | CTensor, the output |
| 1935 | """ |
| 1936 | out = singa.Tanh(x) |
| 1937 | if training: |
| 1938 | self.cache = (out,) |
| 1939 | return out |
| 1940 | |
| 1941 | def backward(self, dy): |
| 1942 | """ |
| 1943 | Args: |
| 1944 | dy (CTensor): the gradient tensor from upper operations |
| 1945 | Returns: |
| 1946 | CTensor, the gradient over input |
| 1947 | """ |
| 1948 | dx = singa.__mul__(self.cache[0], self.cache[0]) |
| 1949 | dx = singa.MultFloat(dx, -1.0) |
| 1950 | dx = singa.AddFloat(dx, 1.0) |
| 1951 | dx *= dy |
| 1952 | return dx |
| 1953 | |
| 1954 | |
| 1955 | def tanh(x): |