class_docstr: This class defines distance functions, that can be used with Batch[All/Hard/SemiHard]TripletLoss
docstr: Compute the 2D matrix of eucledian distances between all the embeddings.
  Args:
      embeddings: tensor of shape (batch_size, embed_dim)
      squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
               If false, output is the pairwise euclidean distance matrix.
  Returns:
      pairwise_distances: tensor of shape (batch_size, batch_size)
docstr_tok: ["Compute","the","2D","matrix","of","eucledian","distances","between","all","the","embeddings",".","Args",":","embeddings",":","tensor","of","shape","(","batch_size",",","embed_dim",")","squared",":","Boolean",".","If","true",",","output","is","the","pairwise","squared","euclidean","distance","matrix",".","If","false",",","output","is","the","pairwise","euclidean","distance","matrix",".","Returns",":","pairwise_distances",":","tensor","of","shape","(","batch_size",",","batch_size",")"]
code: def eucledian_distance(embeddings, squared=False):
      """
      Compute the 2D matrix of eucledian distances between all the embeddings.
      Args:
          embeddings: tensor of shape (batch_size, embed_dim)
          squared: Boolean. If true, output is the pairwise squared euclidean distance matrix.
                   If false, output is the pairwise euclidean distance matrix.
      Returns:
          pairwise_distances: tensor of shape (batch_size, batch_size)
      """
  
      dot_product = torch.matmul(embeddings, embeddings.t())
  
      # Get squared L2 norm for each embedding. We can just take the diagonal of `dot_product`.
      # This also provides more numerical stability (the diagonal of the result will be exactly 0).
      # shape (batch_size,)
      square_norm = torch.diag(dot_product)
  
      # Compute the pairwise distance matrix as we have:
      # ||a - b||^2 = ||a||^2  - 2 <a, b> + ||b||^2
      # shape (batch_size, batch_size)
      distances = (
          square_norm.unsqueeze(0)
          - 2.0 * dot_product
          + square_norm.unsqueeze(1)
      )
  
      # Because of computation errors, some distances might be negative so we put everything >= 0.0
      distances[distances < 0] = 0
  
      if not squared:
          # Because the gradient of sqrt is infinite when distances == 0.0 (ex: on the diagonal)
          # we need to add a small epsilon where distances == 0.0
          mask = distances.eq(0).float()
          distances = distances + mask * 1e-16
  
          distances = (1.0 - mask) * torch.sqrt(distances)
  
      return distances
  
file_path: sentence-transformers/sentence_transformers/losses/BatchHardTripletLoss.py
imports: import torch
  from torch import nn, Tensor
  from typing import Iterable, Dict
  from sentence_transformers import util
  from sentence_transformers.SentenceTransformer import SentenceTransformer
label_desc: Use image node_id 2 for calling the BatchHardTripletLossDistanceFunction obj's underlying member method code with example usage: obj.eucledian_distance(embeddings, squared) and returns: distances