`y = abs(x)`, is applied to the tensor elementwise.
| 2665 | |
| 2666 | |
| 2667 | class Abs(Operator): |
| 2668 | """ |
| 2669 | `y = abs(x)`, is applied to the tensor elementwise. |
| 2670 | """ |
| 2671 | |
| 2672 | def forward(self, a): |
| 2673 | """ |
| 2674 | Return `abs(a)`, where a is CTensor. |
| 2675 | """ |
| 2676 | if training: |
| 2677 | self.input = a |
| 2678 | return singa.Abs(a) |
| 2679 | |
| 2680 | def backward(self, dy): |
| 2681 | """ |
| 2682 | Args: |
| 2683 | dy (CTensor): the gradient tensor from upper operations |
| 2684 | Returns: |
| 2685 | CTensor, the gradient over input |
| 2686 | """ |
| 2687 | dx = singa.Sign(self.input) |
| 2688 | dx *= dy |
| 2689 | return dx |
| 2690 | |
| 2691 | |
| 2692 | def abs(a): |