code: def test_nce_loss(
      classes, xdim, batch, expected_value, device_id, precision
  ):
      dt = PRECISION_TO_TYPE[precision]
  
      from cntk.losses import nce_loss
      import scipy
  
      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)
      )
  
      q = np.arange(classes, dtype=dt) + 1
  
      b = C.parameter((classes, 1), init=-np.log(classes))
      W = C.parameter(
          (classes, C.InferredDimension),
          init=C.glorot_uniform(seed=98052),
      )
  
      loss = C.nce_loss(W, b, x, y, q, seed=98052)
      v = loss.grad({x: x0, y: y0}, wrt=loss.parameters, as_numpy=False)
      for key in v:
          assert v[key].is_sparse, (
              "gradient of nce_loss with respect to %s is not sparse"
              % key
          )
      losses = np.zeros((100, batch))
      for i in range(100):
          losses[i, :] = loss.eval({x: x0, y: y0})
      assert np.allclose(np.mean(losses, axis=0), AA(expected_value))
  
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 5 for calling a global function with example usage: test_nce_loss(classes, xdim, batch, expected_value, device_id, precision) without return types