You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@singa.apache.org by GitBox <gi...@apache.org> on 2020/01/30 14:38:49 UTC
[GitHub] [singa] joddiy commented on a change in pull request #586: Add
GlobalAVGPool operator for autograd and onnx
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_r372985903
##########
File path: test/python/test_operation.py
##########
@@ -2745,5 +2745,97 @@ def test_prelu_broadcast_cpu(self):
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx0)), grad0, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)), grad1, decimal=5)
+ def test_globalaveragepool_cpu(self):
+ X = np.array([[[
+ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]]]).astype(np.float32)
+ XT = np.array([[[[5]]]]).astype(np.float32)
+ DY = np.ones((1, 1, 1, 1), dtype=np.float32)
+
+ x = tensor.from_numpy(X)
+ x.to_device(cpu_dev)
+ dy = tensor.from_numpy(DY)
+ dy.to_device(cpu_dev)
+
+ result = autograd.globalaveragepool(x)
+ dx = result.creator.backward(dy.data)
+
+ DX = np.ones(X.shape, dtype=np.float32)
+ DX = np.multiply(DX, DY) / np.prod(X.shape[2:])
+
+ np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT, decimal=5)
+ np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)), DX, decimal=5)
+
+ def test_globalaveragepool_cpu_channel_last(self):
+ X = np.array([[
+ [[1], [2], [3]],
+ [[4], [5], [6]],
+ [[7], [8], [9]],
+ ]]).astype(np.float32)
+ XT = np.array([[[[5]]]]).astype(np.float32)
+ DY = np.ones((1, 1, 1, 1), dtype=np.float32)
+
+ x = tensor.from_numpy(X)
+ x.to_device(cpu_dev)
+ dy = tensor.from_numpy(DY)
+ dy.to_device(cpu_dev)
+
+ result = autograd.globalaveragepool(x, 'channel_last')
+ dx = result.creator.backward(dy.data)
+
+ DX = np.ones(X.shape, dtype=np.float32)
+ DX = np.multiply(DX, DY) / np.prod(X.shape[1:-1])
+
+ np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT, decimal=5)
+ np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)), DX, decimal=5)
+
+ def test_globalaveragepool_gpu(self):
+ X = np.array([[[
Review comment:
fixed
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services