You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/09/16 23:59:51 UTC

[GitHub] anirudhacharya commented on a change in pull request #11209: [MXNET-536] implement var/std operators

anirudhacharya commented on a change in pull request #11209: [MXNET-536] implement var/std operators
URL: https://github.com/apache/incubator-mxnet/pull/11209#discussion_r217932280
 
 

 ##########
 File path: tests/python/unittest/test_operator.py
 ##########
 @@ -5702,6 +5702,98 @@ def test_softmax():
     check_softmax_grad(default_context())
     check_smoothed_softmax_grad(default_context())
 
+@with_seed()
+def test_variance():
+  def true_var(x, axis=None):
+    if len(x.shape) == 1:
+      return np.var(x, keepdims=True, axis=axis)
+    else:
+      return np.var(x, axis=axis)
+  def true_var_grad(x, ograd, axis=None):
+    if axis is None:
+      return 2 * (x - np.mean(x)) * ograd / np.prod(x.shape)
+    else:
+      denom = x.shape[axis]
+      ograd_shape = tuple(1 if i == axis else k for i, k in enumerate(x.shape))
+      return 2 * (x - np.mean(x, axis=axis, keepdims=True)) * ograd.reshape(ograd_shape) / denom
+
+  for ndim in range(1, 6):
+    # check forward
+    shape = rand_shape_nd(ndim, 5)
+    data = rand_ndarray(shape=shape, stype='default')
+    data_np = data.asnumpy()
+    expected = true_var(data_np)
+    output = mx.nd.variance(data)
 
 Review comment:
   why not include the test for the imperative interface in `test_ndarray.py` file and use `check_symbolic_forward` here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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