code: def cosine_similarity(X, Y=None, dense_output=True):
      """Compute cosine similarity between samples in X and Y.
  
      Cosine similarity, or the cosine kernel, computes similarity as the
      normalized dot product of X and Y:
  
          K(X, Y) = <X, Y> / (||X||*||Y||)
  
      On L2-normalized data, this function is equivalent to linear_kernel.
  
      Read more in the :ref:`User Guide <cosine_similarity>`.
  
      Parameters
      ----------
      X : Tensor or sparse tensor, shape: (n_samples_X, n_features)
          Input data.
  
      Y : Tensor or sparse tensor, shape: (n_samples_Y, n_features)
          Input data. If ``None``, the output will be the pairwise
          similarities between all samples in ``X``.
  
      dense_output : boolean (optional), default True
          Whether to return dense output even when the input is sparse. If
          ``False``, the output is sparse if both input tensors are sparse.
  
      Returns
      -------
      kernel matrix : Tensor
          A tensor with shape (n_samples_X, n_samples_Y).
      """
      X, Y = PairwiseDistances.check_pairwise_arrays(X, Y)
  
      X_normalized = normalize(X, copy=True)
      if X is Y:
          Y_normalized = X_normalized
      else:
          Y_normalized = normalize(Y, copy=True)
  
      K = X_normalized.dot(Y_normalized.T)
      if dense_output:
          K = K.todense()
      return K
  
file_path: mars/mars/learn/metrics/pairwise/cosine.py
imports: import numpy
  from ....None import opcodes
  from ....core import recursive_tile
  from ....serialization.serializables import KeyField
  from ....None import tensor
  from ....tensor.core import TensorOrder
  from ...preprocessing import normalize
  from .core import PairwiseDistances
label_desc: Use image node_id 1 for calling a global function with example usage: cosine_similarity(X, Y, dense_output) and returns: K