forward propogation of GlobalAveragePool Args: x (CTensor): the input tensor Returns: CTensor, the output
(self, x)
| 3815 | self.data_format = data_format |
| 3816 | |
| 3817 | def forward(self, x): |
| 3818 | """ |
| 3819 | forward propogation of GlobalAveragePool |
| 3820 | Args: |
| 3821 | x (CTensor): the input tensor |
| 3822 | Returns: |
| 3823 | CTensor, the output |
| 3824 | """ |
| 3825 | if training: |
| 3826 | self.mask = singa.Tensor(x.shape(), x.device()) |
| 3827 | |
| 3828 | shape = list(x.shape()) |
| 3829 | |
| 3830 | # (N x C x H x W) for channels_first |
| 3831 | if self.data_format == 'channels_first': |
| 3832 | axes = tuple(i for i in range(2, len(shape))) |
| 3833 | self.shape_divisor = 1 / np.prod(shape[2:]) |
| 3834 | else: # (N x H x W x C) for channels_last |
| 3835 | axes = tuple(i for i in range(1, len(shape) - 1)) |
| 3836 | self.shape_divisor = 1 / np.prod(shape[1:-1]) |
| 3837 | |
| 3838 | # output shape |
| 3839 | # (N x C x 1 x 1) for channels_first |
| 3840 | # (N x 1 x 1 x C) for channels_last |
| 3841 | for i in axes: |
| 3842 | shape[i] = 1 |
| 3843 | |
| 3844 | x = tensor.from_raw_tensor(x) |
| 3845 | x = tensor.sum(x, axis=axes) |
| 3846 | x = tensor.reshape(x, shape) |
| 3847 | return singa.MultFloat(x.data, self.shape_divisor) |
| 3848 | |
| 3849 | def backward(self, dy): |
| 3850 | """ |