textattack.constraints.semantics.sentence_encoders.infer_sent package

infer sent

infer sent for sentence similarity

class textattack.constraints.semantics.sentence_encoders.infer_sent.infer_sent.InferSent(*args, **kwargs)[source]

Bases: SentenceEncoder

Constraint using similarity between sentence encodings of x and x_adv where the text embeddings are created using InferSent.

encode(sentences)[source]

Encodes a list of sentences.

To be implemented by subclasses.

get_infersent_model()[source]

Retrieves the InferSent model.

Returns:

The pretrained InferSent model.

MODEL_PATH = 'constraints/semantics/sentence-encoders/infersent-encoder'
WORD_EMBEDDING_PATH = 'word_embeddings'

Infer sent model

This file contains the definition of encoders used in https://arxiv.org/pdf/1705.02364.pdf.

class textattack.constraints.semantics.sentence_encoders.infer_sent.infer_sent_model.InferSentModel(config)[source]

Bases: Module

build_vocab(sentences, tokenize=True)[source]
build_vocab_k_words(K)[source]
encode(sentences, bsize=64, tokenize=True, verbose=False)[source]
forward(sent_tuple)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_batch(batch)[source]
get_w2v(word_dict)[source]
get_w2v_k(K)[source]
get_word_dict(sentences, tokenize=True)[source]
is_cuda()[source]
prepare_samples(sentences, bsize, tokenize, verbose)[source]
set_w2v_path(w2v_path)[source]
tokenize(s)[source]
update_vocab(sentences, tokenize=True)[source]
training: bool