code: def _np_sparsemax_loss(z, q):
      z = z - np.mean(z, axis=1)[:, np.newaxis]
  
      # Calculate q^T * z
      z_k = np.sum(q * z, axis=1)
  
      # calculate sum over S(z)
      p = _np_sparsemax(z)
      s = p > 0
      # z_i^2 - tau(z)^2 = p_i (2 * z_i - p_i) for i \in S(z)
      s_sum = np.sum(s * p * (2 * z - p), axis=1)
  
      # because q is binary, sum([q_1^2, q_2^2, ...]) is just sum(q)
      q_norm = np.sum(q, axis=1)
  
      return 0.5 * s_sum + 0.5 * q_norm - z_k
  
file_path: addons/tensorflow_addons/losses/tests/sparsemax_loss_test.py
imports: import pytest
  from packaging.version import Version
  import tensorflow
  import numpy
  from tensorflow_addons.activations import sparsemax
  from tensorflow_addons.losses import sparsemax_loss, SparsemaxLoss
  from tensorflow_addons.utils import test_utils
label_desc: Use image node_id 2 for calling a global function with example usage: _np_sparsemax_loss(z, q) and returns: unknown