`y = x^0.5`, is applied to the tensor elementwise.
| 2921 | |
| 2922 | |
| 2923 | class Sqrt(Operator): |
| 2924 | """ |
| 2925 | `y = x^0.5`, is applied to the tensor elementwise. |
| 2926 | """ |
| 2927 | |
| 2928 | def __init__(self): |
| 2929 | super(Sqrt, self).__init__() |
| 2930 | |
| 2931 | def forward(self, x): |
| 2932 | """ |
| 2933 | Return `x^0.5`, where x is CTensor. |
| 2934 | """ |
| 2935 | if training: |
| 2936 | self.input = x |
| 2937 | return singa.Sqrt(x) |
| 2938 | |
| 2939 | def backward(self, dy): |
| 2940 | """ |
| 2941 | Args: |
| 2942 | dy (CTensor): the gradient tensor from upper operations |
| 2943 | Returns: |
| 2944 | CTensor, the gradient over input |
| 2945 | """ |
| 2946 | dx = singa.PowFloat(self.input, -0.5) |
| 2947 | dx = singa.MultFloat(dx, 0.5) |
| 2948 | dx = singa.__mul__(dy, dx) |
| 2949 | return dx |
| 2950 | |
| 2951 | |
| 2952 | def sqrt(x): |