forward of ReduceMean Args: x (CTensor): input tensor. Returns: the output CTensor.
(self, x)
| 4095 | self.keepdims = keepdims |
| 4096 | |
| 4097 | def forward(self, x): |
| 4098 | """ |
| 4099 | forward of ReduceMean |
| 4100 | Args: |
| 4101 | x (CTensor): input tensor. |
| 4102 | Returns: |
| 4103 | the output CTensor. |
| 4104 | """ |
| 4105 | _x = tensor.from_raw_tensor(x) |
| 4106 | x_shape = list(_x.shape) |
| 4107 | # handle the special axes |
| 4108 | if self.axes is None: |
| 4109 | self.axes = [i for i in range(len(x_shape))] # axes = None |
| 4110 | else: |
| 4111 | self.axes = [i if i >= 0 else len(x_shape) + i for i in self.axes |
| 4112 | ] # axes has negative |
| 4113 | self.axes.sort(reverse=True) |
| 4114 | for axis in self.axes: |
| 4115 | _x = tensor.sum(_x, axis) |
| 4116 | x_shape[axis] = 1 |
| 4117 | if self.keepdims == 1: |
| 4118 | _x = tensor.reshape(_x, x_shape) |
| 4119 | self.cache = (x_shape, x) |
| 4120 | scale = np.prod(x_shape) / np.prod(x.shape()) |
| 4121 | self.scale = scale |
| 4122 | _x = singa.MultFloat(_x.data, scale) |
| 4123 | return _x |
| 4124 | |
| 4125 | def backward(self, dy): |
| 4126 | """ |