class_docstr: Constraint using cosine similarity between sentence encodings of x and
  x_adv.
  
  Args:
      threshold (:obj:`float`, optional): The threshold for the constraint to be met.
          Defaults to 0.8
      metric (:obj:`str`, optional): The similarity metric to use. Defaults to
          cosine. Options: ['cosine, 'angular']
      compare_against_original (bool):  If `True`, compare new `x_adv` against the original `x`.
          Otherwise, compare it against the previous `x_adv`.
      window_size (int): The number of words to use in the similarity
          comparison. `None` indicates no windowing (encoding is based on the
          full input).
class_docstr_tok: ["Constraint","using","cosine","similarity","between","sentence","encodings","of","x","and","x_adv",".","Args",":","threshold","(",":","obj",":","`","float","`",",","optional",")",":","The","threshold","for","the","constraint","to","be","met",".","Defaults","to","0.8","metric","(",":","obj",":","`","str","`",",","optional",")",":","The","similarity","metric","to","use",".","Defaults","to","cosine",".","Options",":","[","'cosine",",","'angular","'","]","compare_against_original","(","bool",")",":","If","`","True","`",",","compare","new","`","x_adv","`","against","the","original","`","x","`",".","Otherwise",",","compare","it","against","the","previous","`","x_adv","`",".","window_size","(","int",")",":","The","number","of","words","to","use","in","the","similarity","comparison",".","`","None","`","indicates","no","windowing","(","encoding","is","based","on","the","full","input",")","."]
docstr: Returns the metric similarity between the embedding of the starting
  text and a list of transformed texts.
  
  Args:
      starting_text: The ``AttackedText``to use as a starting point.
      transformed_texts: A list of transformed ``AttackedText``
  
  Returns:
      A list with the similarity between the ``starting_text`` and each of
          ``transformed_texts``. If ``transformed_texts`` is empty,
          an empty tensor is returned
docstr_tok: ["Returns","the","metric","similarity","between","the","embedding","of","the","starting","text","and","a","list","of","transformed","texts",".","Args",":","starting_text",":","The","``","AttackedText","``","to","use","as","a","starting","point",".","transformed_texts",":","A","list","of","transformed","``","AttackedText","``","Returns",":","A","list","with","the","similarity","between","the","``","starting_text","``","and","each","of","``","transformed_texts","``",".","If","``","transformed_texts","``","is","empty",",","an","empty","tensor","is","returned"]
code: def _score_list(self, starting_text, transformed_texts):
      """Returns the metric similarity between the embedding of the starting
      text and a list of transformed texts.
  
      Args:
          starting_text: The ``AttackedText``to use as a starting point.
          transformed_texts: A list of transformed ``AttackedText``
  
      Returns:
          A list with the similarity between the ``starting_text`` and each of
              ``transformed_texts``. If ``transformed_texts`` is empty,
              an empty tensor is returned
      """
      # Return an empty tensor if transformed_texts is empty.
      # This prevents us from calling .repeat(x, 0), which throws an
      # error on machines with multiple GPUs (pytorch 1.2).
      if len(transformed_texts) == 0:
          return torch.tensor([])
  
      if self.window_size:
          starting_text_windows = []
          transformed_text_windows = []
          for transformed_text in transformed_texts:
              # @TODO make this work when multiple indices have been modified
              try:
                  modified_index = next(
                      iter(
                          transformed_text.attack_attrs[
                              "newly_modified_indices"
                          ]
                      )
                  )
              except KeyError:
                  raise KeyError(
                      "Cannot apply sentence encoder constraint without `newly_modified_indices`"
                  )
              starting_text_windows.append(
                  starting_text.text_window_around_index(
                      modified_index, self.window_size
                  )
              )
              transformed_text_windows.append(
                  transformed_text.text_window_around_index(
                      modified_index, self.window_size
                  )
              )
          embeddings = self.encode(
              starting_text_windows + transformed_text_windows
          )
          if not isinstance(embeddings, torch.Tensor):
              embeddings = torch.tensor(embeddings)
          starting_embeddings = embeddings[: len(transformed_texts)]
          transformed_embeddings = embeddings[len(transformed_texts) :]
      else:
          starting_raw_text = starting_text.text
          transformed_raw_texts = [t.text for t in transformed_texts]
          embeddings = self.encode(
              [starting_raw_text] + transformed_raw_texts
          )
          if not isinstance(embeddings, torch.Tensor):
              embeddings = torch.tensor(embeddings)
  
          starting_embedding = embeddings[0]
  
          transformed_embeddings = embeddings[1:]
  
          # Repeat original embedding to size of perturbed embedding.
          starting_embeddings = starting_embedding.unsqueeze(
              dim=0
          ).repeat(len(transformed_embeddings), 1)
  
      return self.sim_metric(
          starting_embeddings, transformed_embeddings
      )
  
file_path: TextAttack/textattack/constraints/semantics/sentence_encoders/sentence_encoder.py
imports: from abc import ABC
  import math
  import numpy
  import torch
  from textattack.constraints import Constraint
label_desc: Use image node_id 4 for calling the SentenceEncoder obj's underlying member method code with example usage: obj._score_list(starting_text, transformed_texts) and returns: self, torch