backward of ReduceSum Args: dy (CTensor): gradient tensor. Returns: the gradient tensor over input tensor.
(self, dy)
| 4042 | return _x.data |
| 4043 | |
| 4044 | def backward(self, dy): |
| 4045 | """ |
| 4046 | backward of ReduceSum |
| 4047 | Args: |
| 4048 | dy (CTensor): gradient tensor. |
| 4049 | Returns: |
| 4050 | the gradient tensor over input tensor. |
| 4051 | """ |
| 4052 | x_shape, x = self.cache |
| 4053 | dy = singa.Reshape(dy, x_shape) |
| 4054 | scale = np.prod(x_shape) / np.prod(x.shape()) |
| 4055 | mask = singa.Tensor(list(x.shape()), x.device()) |
| 4056 | mask.SetFloatValue(scale) |
| 4057 | dy = singa.__mul__(mask, dy) |
| 4058 | return dy |
| 4059 | |
| 4060 | |
| 4061 | def reduce_sum(x, axes=None, keepdims=1): |