singa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [singa] nudles commented on a change in pull request #586: Add GlobalAVGPool operator for autograd and onnx
Date Thu, 30 Jan 2020 13:48:04 GMT
nudles commented on a change in pull request #586: Add GlobalAVGPool operator for autograd
and onnx
URL: https://github.com/apache/singa/pull/586#discussion_r372956298
 
 

 ##########
 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:
   like this [test](https://github.com/apache/singa/pull/515/files#diff-c0a014dc6b3a07acd5e7c10001de82c9R1483)

----------------------------------------------------------------
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

Mime
View raw message