code: def test_nce_backward_indices(
      classes, xdim, batch, expected_value, device_id, precision
  ):
      """
      Simple test that makes sure that the derivatives have the correct sparsity pattern
      """
  
      # ignore precision, only sparsity pattern matters for this test
      dt = np.float32
  
      from cntk.losses import nce_loss
      import scipy
  
      trials = 10
  
      # Establish baseline
      expected_count = np.zeros(classes)
      I = C.constant(np.eye(classes, dtype=dt))
      q = np.arange(classes, dtype=dt) + 1
      z = C.reduce_sum(
          C.times(C.random_sample(q, 32, True, seed=98052), I), axis=0
      )
      for i in range(trials):
          expected_count[np.nonzero(z.eval().ravel())] += 1
  
      # Set things up to measure the same thing with nce_loss
  
      x = C.input_variable(xdim, needs_gradient=True)
      y = C.input_variable(classes, is_sparse=True)
  
      x0 = np.arange(batch * xdim, dtype=dt).reshape((batch, xdim)) / (
          batch * xdim
      )
      data = np.ones(batch, dtype=dt)
      indices = list(range(10, 10 * batch + 1, 10))
      indptr = list(range(batch + 1))
      y0 = scipy.sparse.csr_matrix(
          (data, indices, indptr), shape=(batch, classes)
      )
  
      b = C.parameter((classes, 1))
      W = C.parameter((classes, C.InferredDimension))
  
      gb = np.zeros(classes)
      vb = C.input_variable((classes, 1), dtype=dt)
      Ib = C.constant(np.eye(1, dtype=dt))
      zb = C.times(vb, Ib)
  
      loss = C.nce_loss(W, b, x, y, q, seed=98052)
      for i in range(trials):
          v = loss.grad(
              {x: x0, y: y0}, wrt=loss.parameters, as_numpy=False
          )
          gb[np.nonzero(zb.eval({vb: v[b]}).ravel())] += 1
      for i in range(classes):
          assert gb[i] == expected_count[i] or (
              i in indices and gb[i] == trials
          )
  
file_path: CNTK/bindings/python/cntk/losses/tests/losses_test.py
imports: from __future__ import division
  import numpy
  import pytest
  import cntk
  from cntk.ops.tests.ops_test_utils import _test_binary_op, AA, precision, PRECISION_TO_TYPE, unittest_helper
label_desc: Use image node_id 6 for calling a global function with example usage: test_nce_backward_indices(classes, xdim, batch, expected_value, device_id, precision) without return types