def METHOD_NAME(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