class_docstr: Embedding Coherence Test (ECT) score measures if groups of words
  have stereotypical associations by computing the Spearman Coefficient
  of lists of attribute embeddings sorted based on their similarity to
  target embeddings.
  
  Based on: Dev, S., & Phillips, J.M. (2019). [Attenuating Bias in Word Vectors]
  (https://api.semanticscholar.org/CorpusID:59158788). AISTATS.
class_docstr_tok: ["Embedding","Coherence","Test","(","ECT",")","score","measures","if","groups","of","words","have","stereotypical","associations","by","computing","the","Spearman","Coefficient","of","lists","of","attribute","embeddings","sorted","based","on","their","similarity","to","target","embeddings",".","Based","on",":","Dev",",","S.",",","&","Phillips",",","J.M",".","(","2019",")",".","[","Attenuating","Bias","in","Word","Vectors","]","(","https",":","\/\/api.semanticscholar.org\/CorpusID:59158788",")",".","AISTATS","."]
docstr: # Parameters
  
  !!! Note
      In the examples below, we treat gender identity as binary, which does not accurately
      characterize gender in real life.
  
  target_embeddings1 : `torch.Tensor`, required.
      A tensor of size (target_embeddings_batch_size, ..., dim) containing target word
      embeddings related to a concept group. For example, if the concept is gender,
      target_embeddings1 could contain embeddings for linguistically masculine words, e.g.
      "man", "king", "brother", etc. Represented as X.
  
  target_embeddings2 : `torch.Tensor`, required.
      A tensor of the same size as target_embeddings1 containing target word
      embeddings related to a different group for the same concept. For example,
      target_embeddings2 could contain embeddings for linguistically feminine words, e.g.
      "woman", "queen", "sister", etc. Represented as Y.
  
  attribute_embeddings : `torch.Tensor`, required.
      A tensor of size (attribute_embeddings_batch_size, ..., dim) containing attribute word
      embeddings related to a concept associated with target_embeddings1 and target_embeddings2.
      For example, if the concept is professions, attribute_embeddings could contain embeddings for
      "doctor", "banker", "engineer", etc. Represented as AB.
  
  # Returns
  
  ect_score : `torch.FloatTensor`
      The Spearman Coefficient measuring the similarity of lists of attribute embeddings sorted
      based on their similarity to the target embeddings. Ranges from [-1, 1], with values closer
      to 1 indicating less biased associations.
docstr_tok: ["#","Parameters","!","!","!","Note","In","the","examples","below",",","we","treat","gender","identity","as","binary",",","which","does","not","accurately","characterize","gender","in","real","life",".","target_embeddings1",":","`","torch.Tensor","`",",","required",".","A","tensor","of","size","(","target_embeddings_batch_size",",","...",",","dim",")","containing","target","word","embeddings","related","to","a","concept","group",".","For","example",",","if","the","concept","is","gender",",","target_embeddings1","could","contain","embeddings","for","linguistically","masculine","words",",","e.g",".","``","man","''",",","``","king","''",",","``","brother","''",",","etc",".","Represented","as","X.","target_embeddings2",":","`","torch.Tensor","`",",","required",".","A","tensor","of","the","same","size","as","target_embeddings1","containing","target","word","embeddings","related","to","a","different","group","for","the","same","concept",".","For","example",",","target_embeddings2","could","contain","embeddings","for","linguistically","feminine","words",",","e.g",".","``","woman","''",",","``","queen","''",",","``","sister","''",",","etc",".","Represented","as","Y.","attribute_embeddings",":","`","torch.Tensor","`",",","required",".","A","tensor","of","size","(","attribute_embeddings_batch_size",",","...",",","dim",")","containing","attribute","word","embeddings","related","to","a","concept","associated","with","target_embeddings1","and","target_embeddings2",".","For","example",",","if","the","concept","is","professions",",","attribute_embeddings","could","contain","embeddings","for","``","doctor","''",",","``","banker","''",",","``","engineer","''",",","etc",".","Represented","as","AB",".","#","Returns","ect_score",":","`","torch.FloatTensor","`","The","Spearman","Coefficient","measuring","the","similarity","of","lists","of","attribute","embeddings","sorted","based","on","their","similarity","to","the","target","embeddings",".","Ranges","from","[","-1",",","1","]",",","with","values","closer","to","1","indicating","less","biased","associations","."]
code: def __call__(
      self,
      target_embeddings1: torch.Tensor,
      target_embeddings2: torch.Tensor,
      attribute_embeddings: torch.Tensor,
  ) -> torch.FloatTensor:
      """
  
      # Parameters
  
      !!! Note
          In the examples below, we treat gender identity as binary, which does not accurately
          characterize gender in real life.
  
      target_embeddings1 : `torch.Tensor`, required.
          A tensor of size (target_embeddings_batch_size, ..., dim) containing target word
          embeddings related to a concept group. For example, if the concept is gender,
          target_embeddings1 could contain embeddings for linguistically masculine words, e.g.
          "man", "king", "brother", etc. Represented as X.
  
      target_embeddings2 : `torch.Tensor`, required.
          A tensor of the same size as target_embeddings1 containing target word
          embeddings related to a different group for the same concept. For example,
          target_embeddings2 could contain embeddings for linguistically feminine words, e.g.
          "woman", "queen", "sister", etc. Represented as Y.
  
      attribute_embeddings : `torch.Tensor`, required.
          A tensor of size (attribute_embeddings_batch_size, ..., dim) containing attribute word
          embeddings related to a concept associated with target_embeddings1 and target_embeddings2.
          For example, if the concept is professions, attribute_embeddings could contain embeddings for
          "doctor", "banker", "engineer", etc. Represented as AB.
  
      # Returns
  
      ect_score : `torch.FloatTensor`
          The Spearman Coefficient measuring the similarity of lists of attribute embeddings sorted
          based on their similarity to the target embeddings. Ranges from [-1, 1], with values closer
          to 1 indicating less biased associations.
  
      """
      # Some sanity checks
      if target_embeddings1.ndim < 2 or target_embeddings2.ndim < 2:
          raise ConfigurationError(
              "target_embeddings1 and target_embeddings2 must have at least two dimensions."
          )
      if attribute_embeddings.ndim < 2:
          raise ConfigurationError(
              "attribute_embeddings must have at least two dimensions."
          )
      if target_embeddings1.size() != target_embeddings2.size():
          raise ConfigurationError(
              "target_embeddings1 and target_embeddings2 must be of the same size."
          )
      if attribute_embeddings.size(dim=-1) != target_embeddings1.size(
          dim=-1
      ):
          raise ConfigurationError(
              "All embeddings must have the same dimensionality."
          )
  
      mean_target_embedding1 = target_embeddings1.flatten(
          end_dim=-2
      ).mean(dim=0)
      mean_target_embedding2 = target_embeddings2.flatten(
          end_dim=-2
      ).mean(dim=0)
      attribute_embeddings = attribute_embeddings.flatten(end_dim=-2)
  
      # Normalize
      mean_target_embedding1 = torch.nn.functional.normalize(
          mean_target_embedding1, p=2, dim=-1
      )
      mean_target_embedding2 = torch.nn.functional.normalize(
          mean_target_embedding2, p=2, dim=-1
      )
      attribute_embeddings = torch.nn.functional.normalize(
          attribute_embeddings, p=2, dim=-1
      )
  
      # Compute cosine similarities
      AB_sim_m = torch.matmul(
          attribute_embeddings, mean_target_embedding1
      )
      AB_sim_f = torch.matmul(
          attribute_embeddings, mean_target_embedding2
      )
  
      return self.spearman_correlation(AB_sim_m, AB_sim_f)
  
imports: from typing import Optional, Dict, Union, List
  import torch
  import torch.distributed
  from allennlp.common.util import is_distributed
  from allennlp.common.checks import ConfigurationError
  from allennlp.nn.util import dist_reduce_sum
  from allennlp.training.metrics.metric import Metric
label_desc: Use image node_id 1 for calling the EmbeddingCoherenceTest obj's underlying member method code with example usage: obj.__call__(target_embeddings1, target_embeddings2, attribute_embeddings) and returns: self