code: def get_grad(self, guesses: Floats2d, truths: Floats2d) -> Floats2d:
      if guesses.shape != truths.shape:  # pragma: no cover
          err = f"Cannot calculate cosine similarity: mismatched shapes: {guesses.shape} vs {truths.shape}."
          raise ValueError(err)
  
      # Note: not using get_distance() here to avoid duplicating certain calculations
      xp = get_array_module(guesses)
      # Find the zero vectors
      if self.ignore_zeros:
          zero_indices = xp.abs(truths).sum(axis=1) == 0
      # Add a small constant to avoid 0 vectors
      yh = guesses + 1e-8
      y = truths + 1e-8
      # https://math.stackexchange.com/questions/1923613/partial-derivative-of-cosinesimilarity
      norm_yh = xp.linalg.norm(yh, axis=1, keepdims=True)
      norm_y = xp.linalg.norm(y, axis=1, keepdims=True)
      mul_norms = norm_yh * norm_y
      cosine = (yh * y).sum(axis=1, keepdims=True) / mul_norms
      d_yh = (y / mul_norms) - (cosine * (yh / norm_yh**2))
      if self.ignore_zeros:
          # If the target was a zero vector, don't count it in the loss.
          d_yh[zero_indices] = 0
      if self.normalize:
          d_yh = d_yh / guesses.shape[0]
      return -d_yh
  
imports: from abc import abstractmethod
  from typing import Any, Dict, Generic, List, Optional, Sequence, Tuple, TypeVar, Union, cast
  from .config import registry
  from .types import Floats2d, Ints1d
  from .util import get_array_module, to_categorical
label_desc: Use image node_id 4 for calling the CosineDistance obj's underlying member method code with example usage: obj.get_grad(guesses, truths) and returns: unknown