MCPcopy Create free account
hub / github.com/apache/singa / Tanh

Class Tanh

python/singa/autograd.py:1921–1952  ·  view source on GitHub ↗

Calculates the hyperbolic tangent of the given input tensor element-wise.

Source from the content-addressed store, hash-verified

1919
1920
1921class 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
1955def tanh(x):

Callers 4

tanhFunction · 0.85
TEST_FFunction · 0.85
TEST_FFunction · 0.85
ForwardMethod · 0.85

Calls

no outgoing calls

Tested by 2

TEST_FFunction · 0.68
TEST_FFunction · 0.68