docstr: Compute the similarity between all labels for better sampling of negatives.
docstr_tok: ["Compute","the","similarity","between","all","labels","for","better","sampling","of","negatives","."]
code: def _compute_label_similarity_for_current_epoch(self):
      """Compute the similarity between all labels for better sampling of negatives."""
      # get and embed all labels by making a Sentence object that contains only the label text
      all_labels = [
          label.decode("utf-8")
          for label in self.get_current_label_dictionary().idx2item
      ]
      label_sentences = [Sentence(label) for label in all_labels]
  
      self.tars_embeddings.eval()  # TODO: check if this is necessary
      self.tars_embeddings.embed(label_sentences)
      self.tars_embeddings.train()
  
      # get each label embedding and scale between 0 and 1
      if isinstance(self.tars_embeddings, TokenEmbeddings):
          encodings_np = [
              sentence[0].get_embedding().cpu().detach().numpy()
              for sentence in label_sentences
          ]
      else:
          encodings_np = [
              sentence.get_embedding().cpu().detach().numpy()
              for sentence in label_sentences
          ]
  
      normalized_encoding = minmax_scale(encodings_np)
  
      # compute similarity matrix
      similarity_matrix = cosine_similarity(normalized_encoding)
  
      # the higher the similarity, the greater the chance that a label is
      # sampled as negative example
      negative_label_probabilities = {}
      for row_index, label in enumerate(all_labels):
          negative_label_probabilities[label] = {}
          for column_index, other_label in enumerate(all_labels):
              if label != other_label:
                  negative_label_probabilities[label][
                      other_label
                  ] = similarity_matrix[row_index][column_index]
      self.label_nearest_map = negative_label_probabilities
  
imports: import logging
  import typing
  from abc import ABC
  from collections import OrderedDict
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Set, Tuple, Union
  import numpy
  import torch
  from sklearn.metrics.pairwise import cosine_similarity
  from sklearn.preprocessing import minmax_scale
  from tqdm import tqdm
  import flair
  from flair.data import Corpus, Dictionary, Sentence, Span
  from flair.datasets import DataLoader, FlairDatapointDataset
  from flair.embeddings import TokenEmbeddings, TransformerDocumentEmbeddings, TransformerWordEmbeddings
  from flair.file_utils import cached_path
  from flair.models.sequence_tagger_model import SequenceTagger
  from flair.models.text_classification_model import TextClassifier
  from flair.training_utils import store_embeddings
label_desc: Use image node_id 8 for calling the FewshotClassifier obj's underlying member method code with example usage: obj._compute_label_similarity_for_current_epoch() without return types