ShichengChen commented on a change in pull request #524: SINGA-474 
prelu,add,equal,selu,elu operator
URL: https://github.com/apache/incubator-singa/pull/524#discussion_r317383759
 
 

 ##########
 File path: test/python/test_operation.py
 ##########
 @@ -1587,25 +1587,148 @@ def test_min_gpu(self):
         
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)),
 DX1, decimal=5)
 
 
-def test_HardSigmoid(self):
-    def test_helper(gpu=False):
-        x = np.random.randn(3, 2)
-        #y = max(0, min(1, alpha * x + gamma))
-        a=0.2
-        g=0.5
-        y = np.clip(x * 0.2 + 0.5, 0, 1)
-        grad=(0<(np.clip(x * 0.2 + 0.5, 0, 1)) * (np.clip(x * 0.2 + 0.5, 0, 
1)<1))*0.2
-        x = tensor.from_numpy(x)
-        if(gpu):
-            x.to_device(gpu_dev)
-        result = autograd.hardsigmoid(x,a,g)
-        dy = tensor.from_numpy(np.random.randn((3,2)).astype(np.float32))
-        dx = result.creator.backward(dy.data)
-        np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
-        
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
-    test_helper(False)
-    test_helper(True)
+    def test_HardSigmoid(self):
+        def test_helper(gpu=False):
+            x = np.random.randn(3, 2)
+            #y = max(0, min(1, alpha * x + gamma))
+            a=0.2
+            g=0.5
+            y = np.clip(x * 0.2 + 0.5, 0, 1)
+            dy=np.random.randn(3,2)
+            grad=(0<(np.clip(x * 0.2 + 0.5, 0, 1)) * (np.clip(x * 0.2 + 0.5, 
0, 1)<1))*0.2 * dy
+            x = tensor.from_numpy(x)
+            dy = tensor.from_numpy(dy)
+            if(gpu):
+                x.to_device(gpu_dev)
+                dy.to_device(gpu_dev)
+            result = autograd.hardsigmoid(x,a,g)
+            dx = result.creator.backward(dy.data)
+            np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+            
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
+        test_helper(False)
+        test_helper(True)
+
+    @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+    def test_prelu(self):
+        def hepler(gpu):
+            x = np.random.randn(3, 2)
+            slope = np.random.randn(3, 2)
+            y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope
+            dy = np.random.randn(3, 2)
+            x0=x.copy()
+            x0[x0>0]=1
+            x0[x0<1]=0
+            grad0=(x0+(1-x0)*slope)*dy
+            grad1 = (1-x0)*x*dy
+            x = tensor.from_numpy(x)
+            slope = tensor.from_numpy(slope)
+            dy = tensor.from_numpy(dy)
+            if(gpu):
+                x.to_device(gpu_dev)
+                slope.to_device(gpu_dev)
+                dy.to_device(gpu_dev)
+            result = autograd.prelu(x,slope)
+            dx0,dx1 = result.creator.backward(dy.data)
+            np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+            
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)
+        hepler(False)
+        hepler(True)
+
+    @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+    def test_SeLU(self):
+        def test_helper(gpu):
+            x = np.random.randn(3, 2)
+            a=0.2
+            g=0.3
+            y = np.clip(x, 0, np.inf) * g + (np.exp(np.clip(x, -np.inf, 0)) - 
1) * a * g
+            dy=np.random.randn(3, 2)
+            grad = (np.exp(np.clip(x, -np.inf, 0))) * g
+            grad[x<=0]=grad[x<=0]*a
+            grad*=dy
+            x = tensor.from_numpy(x)
+
 
+            result = autograd.selu(x,a,g)
+            dy = tensor.from_numpy(dy)
+            if(gpu):
+                dy.to_device(gpu_dev)
+                x.to_device(gpu_dev)
+            dx = result.creator.backward(dy.data)
+
+            np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+            
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
+        test_helper(False)
+        test_helper(True)
+
+
+    @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+    def test_Equal(self):
+        def test_helper(gpu):
+            x0 = np.random.randn(3, 2)
+            x1 = np.random.randn(3, 2)
+            y = np.equal(x0,x1)
+            x0 = tensor.from_numpy(x0)
+            x1 = tensor.from_numpy(x1)
+            if(gpu):
+                x0.to_device(gpu_dev)
+                x1.to_device(gpu_dev)
+
+            result = autograd.equal(x0,x1)
+
+            np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+        test_helper(False)
+        test_helper(True)
+
+    @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
+    def test_Elu(self):
+        def test_helper(gpu):
+            #f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0
+            x = np.random.randn(3, 2)
+            y = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 
1.0
+            dy=np.random.randn(3, 2)
+            grad=np.exp(np.clip(x, -np.inf, 0))*dy
+
+            x = tensor.from_numpy(x)
+            result = autograd.elu(x)
+            dy = tensor.from_numpy(dy)
+            if(gpu):
+                dy.to_device(gpu_dev)
+                x.to_device(gpu_dev)
+            dx = result.creator.backward(dy.data)
+            np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, 
decimal=5)
+            
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx)),
 grad, decimal=5)
+        test_helper(False)
+        test_helper(True)
+
+    @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled')
 
 Review comment:
   I just changed it to if-else checks inside unit test functions

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to