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_r224232619
 
 

 ##########
 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)
+    assert_almost_equal(output.asnumpy(), expected)
+    for axis in range(ndim):
+      expected = true_var(data_np, axis=axis)
+      output = mx.nd.variance(data, axis=axis)
 
 Review comment:
   if it is piggy-backing on ReduceAxesOp, then it should be able support tuple 
values for axes I think. Because the `sum` operator does do it - 
http://mxnet.incubator.apache.org/api/python/symbol/symbol.html#mxnet.symbol.sum

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to