Calculate the sign of the given input tensor element-wise. If input > 0, output 1. if input < 0, output -1. if input == 0, output 0.
| 2787 | |
| 2788 | |
| 2789 | class Sign(Operator): |
| 2790 | """ |
| 2791 | Calculate the sign of the given input tensor element-wise. If input > 0, |
| 2792 | output 1. if input < 0, output -1. if input == 0, output 0. |
| 2793 | """ |
| 2794 | |
| 2795 | def __init__(self): |
| 2796 | super(Sign, self).__init__() |
| 2797 | |
| 2798 | def forward(self, a): |
| 2799 | """ |
| 2800 | Args: |
| 2801 | a (CTensor): Input tensor |
| 2802 | Returns: |
| 2803 | CTensor, the output |
| 2804 | """ |
| 2805 | if training: |
| 2806 | self.input = a |
| 2807 | return singa.Sign(a) |
| 2808 | |
| 2809 | def backward(self, dy): |
| 2810 | """ |
| 2811 | Args: |
| 2812 | dy (CTensor): the gradient tensor from upper operations |
| 2813 | Returns: |
| 2814 | CTensor, the gradient over input |
| 2815 | """ |
| 2816 | dx = singa.MultFloat(dy, 0.0) |
| 2817 | return dx |
| 2818 | |
| 2819 | |
| 2820 | def sign(a): |