code: def triplet_semihard_loss_np(labels, embedding, margin, dist_func):
      num_data = embedding.shape[0]
      # Reshape labels to compute adjacency matrix.
      labels_reshaped = np.reshape(
          labels.astype(np.float32), (labels.shape[0], 1)
      )
  
      adjacency = np.equal(labels_reshaped, labels_reshaped.T)
      pdist_matrix = dist_func(embedding)
      loss_np = 0.0
      num_positives = 0.0
      for i in range(num_data):
          for j in range(num_data):
              if adjacency[i][j] > 0.0 and i != j:
                  num_positives += 1.0
  
                  pos_distance = pdist_matrix[i][j]
                  neg_distances = []
  
                  for k in range(num_data):
                      if adjacency[i][k] == 0:
                          neg_distances.append(pdist_matrix[i][k])
  
                  # Sort by distance.
                  neg_distances.sort()
                  chosen_neg_distance = neg_distances[0]
  
                  for m in range(len(neg_distances)):
                      chosen_neg_distance = neg_distances[m]
                      if chosen_neg_distance > pos_distance:
                          break
  
                  loss_np += np.maximum(
                      0.0, margin - chosen_neg_distance + pos_distance
                  )
  
      loss_np /= num_positives
      return loss_np
  
file_path: addons/tensorflow_addons/losses/tests/triplet_test.py
imports: import pytest
  import numpy
  import tensorflow
  from tensorflow_addons.losses import triplet
  from tensorflow_addons.utils import test_utils
label_desc: Use image node_id 5 for calling a global function with example usage: triplet_semihard_loss_np(labels, embedding, margin, dist_func) and returns: loss_np