Calculates the arccosine (inverse of cosine) of the given input tensor, element-wise.
| 2050 | |
| 2051 | |
| 2052 | class Acos(Operator): |
| 2053 | """ |
| 2054 | Calculates the arccosine (inverse of cosine) of the given input tensor, |
| 2055 | element-wise. |
| 2056 | """ |
| 2057 | |
| 2058 | def __init__(self): |
| 2059 | super(Acos, self).__init__() |
| 2060 | |
| 2061 | def forward(self, x): |
| 2062 | """ |
| 2063 | Args: |
| 2064 | x (CTensor): Input tensor |
| 2065 | Returns: |
| 2066 | CTensor, the output |
| 2067 | """ |
| 2068 | if training: |
| 2069 | self.input = x |
| 2070 | return singa.Acos(x) |
| 2071 | |
| 2072 | def backward(self, dy): |
| 2073 | """ |
| 2074 | Args: |
| 2075 | dy (CTensor): the gradient tensor from upper operations |
| 2076 | Returns: |
| 2077 | CTensor, the gradient over input |
| 2078 | """ |
| 2079 | dx = singa.Square(self.input) |
| 2080 | dx = singa.MultFloat(dx, -1.0) |
| 2081 | dx = singa.AddFloat(dx, 1.0) |
| 2082 | dx = singa.PowFloat(dx, -0.5) |
| 2083 | dx = singa.MultFloat(dx, -1.0) |
| 2084 | dx *= dy |
| 2085 | return dx |
| 2086 | |
| 2087 | |
| 2088 | def acos(x): |