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 the transformed text.
  
  Args:
      starting_text: The ``AttackedText``to use as a starting point.
      transformed_text: A transformed ``AttackedText``
  
  Returns:
      The similarity between the starting and transformed text using the metric.
docstr_tok: ["Returns","the","metric","similarity","between","the","embedding","of","the","starting","text","and","the","transformed","text",".","Args",":","starting_text",":","The","``","AttackedText","``","to","use","as","a","starting","point",".","transformed_text",":","A","transformed","``","AttackedText","``","Returns",":","The","similarity","between","the","starting","and","transformed","text","using","the","metric","."]
code: def _sim_score(self, starting_text, transformed_text):
      """Returns the metric similarity between the embedding of the starting
      text and the transformed text.
  
      Args:
          starting_text: The ``AttackedText``to use as a starting point.
          transformed_text: A transformed ``AttackedText``
  
      Returns:
          The similarity between the starting and transformed text using the metric.
      """
      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_window = starting_text.text_window_around_index(
          modified_index, self.window_size
      )
  
      transformed_text_window = (
          transformed_text.text_window_around_index(
              modified_index, self.window_size
          )
      )
  
      starting_embedding, transformed_embedding = self.model.encode(
          [starting_text_window, transformed_text_window]
      )
  
      if not isinstance(starting_embedding, torch.Tensor):
          starting_embedding = torch.tensor(starting_embedding)
  
      if not isinstance(transformed_embedding, torch.Tensor):
          transformed_embedding = torch.tensor(transformed_embedding)
  
      starting_embedding = torch.unsqueeze(starting_embedding, dim=0)
      transformed_embedding = torch.unsqueeze(
          transformed_embedding, dim=0
      )
  
      return self.sim_metric(starting_embedding, transformed_embedding)
  
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 3 for calling the SentenceEncoder obj's underlying member method code with example usage: obj._sim_score(starting_text, transformed_text) and returns: self