joddiy commented on a change in pull request #496: SINGA-474 Mean operator
URL: https://github.com/apache/incubator-singa/pull/496#discussion_r310489393
 
 

 ##########
 File path: test/python/test_operation.py
 ##########
 @@ -322,6 +335,48 @@ def test_LeakyRelu(self):
         np.testing.assert_array_almost_equal(tensor.to_numpy(result), XT)
         self.check_shape(dx.shape(), (3, 2))
 
+    def test_Mean_gpu(self):
+        x0 = np.array([-0.9, -0.3, -0.1, 0.1, 0.5, 0.9]).reshape(3, 
2).astype(np.float32)
+        x1 = np.array([0, -0.3, 0, 0.1, 0, 0.9]).reshape(3, 
2).astype(np.float32)
+        y = (x0+x1)/2
+        lossf =lambda x,y:np.sum((x+y)/2)
+        grad=eval_numerical_gradient(lossf,x0,x1)
+        grad1=eval_numerical_gradient(lossf,x1,x0)
 
 Review comment:
   I guess you can use this function:
   ```
   def eval_numerical_gradient_b(f, x, y, reverse = False):
       h = 0.00001
       grad = np.zeros(x.shape)
       t = y if reverse else x
       fx = f(x, y)
       it = np.nditer(t, flags=['multi_index'], op_flags=['readwrite'])
       while not it.finished:
           _it = it.multi_index
           old_value = t[_it]
           t[_it] = old_value + h # increment by h
           fth = f(x, y) # evaluate f(x + h)
           t[_it] = old_value # restore to previous value (very important!) 
           grad[_it] = (fth - fx) / h # the slope
           it.iternext() # step to next dimension
       return grad
   ```
   if the reverse if False, this function gets the grads of x based on f(x,y), 
if reverse if true, it gets the grads of y still based on f(x,y)

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

Reply via email to