textattack.constraints.semantics.sentence_encoders package

Sentence Encoder

Sentence Encoder Class

class textattack.constraints.semantics.sentence_encoders.sentence_encoder.SentenceEncoder(threshold=0.8, metric='cosine', compare_against_original=True, window_size=None, skip_text_shorter_than_window=False)[source]

Bases: textattack.constraints.constraint.Constraint, abc.ABC

Constraint using cosine similarity between sentence encodings of x and x_adv.

Parameters
  • threshold (float, optional) – The threshold for the constraint to be met. Defaults to 0.8

  • metric (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).

encode(sentences)[source]

Encodes a list of sentences.

To be implemented by subclasses.

extra_repr_keys()[source]

Set the extra representation of the constraint using these keys.

To print customized extra information, you should reimplement this method in your own constraint. Both single-line and multi- line strings are acceptable.

textattack.constraints.semantics.sentence_encoders.sentence_encoder.get_angular_sim(emb1, emb2)[source]

Returns the _angular_ similarity between a batch of vector and a batch of vectors.

textattack.constraints.semantics.sentence_encoders.sentence_encoder.get_neg_euclidean_dist(emb1, emb2)[source]

Returns the Euclidean distance between a batch of vectors and a batch of vectors.

Thought Vector

class textattack.constraints.semantics.sentence_encoders.thought_vector.ThoughtVector(embedding=WordEmbedding, **kwargs)[source]

Bases: textattack.constraints.semantics.sentence_encoders.sentence_encoder.SentenceEncoder

A constraint on the distance between two sentences’ thought vectors.

Parameters

word_embedding (textattack.shared.AbstractWordEmbedding) – The word embedding to use

clear_cache()[source]
encode(raw_text_list)[source]

Encodes a list of sentences.

To be implemented by subclasses.

extra_repr_keys()[source]

Set the extra representation of the constraint using these keys.