forward of ReduceSum Args: x (CTensor): input tensor. Returns: the output CTensor.
(self, x)
| 4017 | self.keepdims = keepdims |
| 4018 | |
| 4019 | def forward(self, x): |
| 4020 | """ |
| 4021 | forward of ReduceSum |
| 4022 | Args: |
| 4023 | x (CTensor): input tensor. |
| 4024 | Returns: |
| 4025 | the output CTensor. |
| 4026 | """ |
| 4027 | _x = tensor.from_raw_tensor(x) |
| 4028 | x_shape = list(_x.shape) |
| 4029 | # handle the special axes |
| 4030 | if self.axes is None: |
| 4031 | self.axes = [i for i in range(len(x_shape))] # axes = None |
| 4032 | else: |
| 4033 | self.axes = [i if i >= 0 else len(x_shape) + i for i in self.axes |
| 4034 | ] # axes has negative |
| 4035 | self.axes.sort(reverse=True) |
| 4036 | for axis in self.axes: |
| 4037 | _x = tensor.sum(_x, axis) |
| 4038 | x_shape[axis] = 1 |
| 4039 | if self.keepdims == 1: |
| 4040 | _x = tensor.reshape(_x, x_shape) |
| 4041 | self.cache = (x_shape, x) |
| 4042 | return _x.data |
| 4043 | |
| 4044 | def backward(self, dy): |
| 4045 | """ |