joddiy commented on a change in pull request #586: Add GlobalAVGPool operator
for autograd and onnx
URL: https://github.com/apache/singa/pull/586#discussion_r372986616
##########
File path: python/singa/autograd.py
##########
@@ -2760,3 +2760,42 @@ def backward(self, dy):
def reciprocal(x):
return Reciprocal()(x)[0]
+
+
+class GlobalAveragePool(Operation):
+ def __init__(self, data_format='channels_first'):
+ super(GlobalAveragePool, self).__init__()
+ self.data_format = data_format
+
+ def forward(self, x):
+ if training:
+ self.mask = singa.Tensor(list(x.shape()), x.device())
+
+ shape = list(x.shape())
+
+ # (N x C x H x W) for channels_first
+ if self.data_format == 'channels_first':
+ axes = tuple(i for i in range(2, len(shape)))
+ self.shape_divisor = 1/np.prod(shape[2:])
+ else: # (N x H x W x C) for channels_last
+ axes = tuple(i for i in range(1, len(shape)-1))
+ self.shape_divisor = 1/np.prod(shape[1:-1])
+
+ # output shape
+ # (N x C x 1 x 1) for channels_first
+ # (N x 1 x 1 x C) for channels_last
+ for i in axes:
+ shape[i] = 1
+
+ x = tensor.from_raw_tensor(x)
+ x = tensor.sum(x, axis=axes)
Review comment:
it seems the singa.Sum does not support sum over axis larger than 1.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services