Transpose the input tensor similar to numpy.transpose.
| 2608 | |
| 2609 | |
| 2610 | class Transpose(Operator): |
| 2611 | """ |
| 2612 | Transpose the input tensor similar to numpy.transpose. |
| 2613 | """ |
| 2614 | |
| 2615 | def __init__(self, perm): |
| 2616 | """ |
| 2617 | Args: |
| 2618 | perm (list of ints): A list of integers. By default, reverse the |
| 2619 | dimensions, otherwise permute the axes according to the values given. |
| 2620 | """ |
| 2621 | super(Transpose, self).__init__() |
| 2622 | self.perm = list(perm) |
| 2623 | |
| 2624 | def forward(self, x): |
| 2625 | """ |
| 2626 | Args: |
| 2627 | x (CTensor): Input tensor |
| 2628 | Returns: |
| 2629 | CTensor, the output |
| 2630 | """ |
| 2631 | return singa.Transpose(x, self.perm) |
| 2632 | |
| 2633 | def backward(self, dy): |
| 2634 | """ |
| 2635 | Args: |
| 2636 | dy (CTensor): the gradient tensor from upper operations |
| 2637 | Returns: |
| 2638 | CTensor, the gradient over input |
| 2639 | """ |
| 2640 | cur = [] |
| 2641 | for i in range(len(self.perm)): |
| 2642 | cur += [self.perm.index(i)] |
| 2643 | return singa.Transpose(dy, cur) |
| 2644 | |
| 2645 | |
| 2646 | def transpose(x, shape): |