allennlp.models.event2mind

class allennlp.models.event2mind.Event2Mind(vocab: allennlp.data.vocabulary.Vocabulary, source_embedder: allennlp.modules.text_field_embedders.text_field_embedder.TextFieldEmbedder, embedding_dropout: float, encoder: allennlp.modules.seq2vec_encoders.seq2vec_encoder.Seq2VecEncoder, max_decoding_steps: int, beam_size: int = 10, target_names: List[str] = None, target_namespace: str = 'tokens', target_embedding_dim: int = None, regularizer: Optional[allennlp.nn.regularizers.regularizer_applicator.RegularizerApplicator] = None)[source]

Bases: allennlp.models.model.Model

This Event2Mind class is a Model which takes an event sequence, encodes it, and then uses the encoded representation to decode several mental state sequences.

It is based on the paper by Rashkin et al.

Parameters
vocabVocabulary, required

Vocabulary containing source and target vocabularies. They may be under the same namespace (tokens) or the target tokens can have a different namespace, in which case it needs to be specified as target_namespace.

source_embedderTextFieldEmbedder, required

Embedder for source side sequences.

embedding_dropout: float, required

The amount of dropout to apply after the source tokens have been embedded.

encoderSeq2VecEncoder, required

The encoder of the “encoder/decoder” model.

max_decoding_stepsint, required

Length of decoded sequences.

beam_sizeint, optional (default = 10)

The width of the beam search.

target_names: ``List[str]``, optional, (default = [‘xintent’, ‘xreact’, ‘oreact’])

Names of the target fields matching those in the Instance objects.

target_namespacestr, optional (default = ‘tokens’)

If the target side vocabulary is different from the source side’s, you need to specify the target’s namespace here. If not, we’ll assume it is “tokens”, which is also the default choice for the source side, and this might cause them to share vocabularies.

target_embedding_dimint, optional (default = source_embedding_dim)

You can specify an embedding dimensionality for the target side. If not, we’ll use the same value as the source embedder’s.

regularizerRegularizerApplicator, optional (default=``None``)

If provided, will be used to calculate the regularization penalty during training.

decode(self, output_dict: Dict[str, torch.Tensor]) → Dict[str, List[List[str]]][source]

This method overrides Model.decode, which gets called after Model.forward, at test time, to finalize predictions. The logic for the decoder part of the encoder-decoder lives within the forward method.

This method trims the output predictions to the first end symbol, replaces indices with corresponding tokens, and adds fields for the tokens to the output_dict.

decode_all(self, predicted_indices: torch.Tensor) → List[List[str]][source]
forward(self, source: Dict[str, torch.LongTensor], **target_tokens: Dict[str, Dict[str, torch.LongTensor]]) → Dict[str, torch.Tensor][source]

Decoder logic for producing the target sequences.

Parameters
sourceDict[str, torch.LongTensor]

The output of TextField.as_array() applied on the source TextField. This will be passed through a TextFieldEmbedder and then through an encoder.

target_tokensDict[str, Dict[str, torch.LongTensor]]:

Dictionary from name to output of Textfield.as_array() applied on target TextField. We assume that the target tokens are also represented as a TextField.

get_metrics(self, reset: bool = False) → Dict[str, float][source]

Returns a dictionary of metrics. This method will be called by allennlp.training.Trainer in order to compute and use model metrics for early stopping and model serialization. We return an empty dictionary here rather than raising as it is not required to implement metrics for a new model. A boolean reset parameter is passed, as frequently a metric accumulator will have some state which should be reset between epochs. This is also compatible with Metrics should be populated during the call to ``forward`, with the Metric handling the accumulation of the metric until this method is called.

greedy_predict(self, final_encoder_output: torch.LongTensor, target_embedder: allennlp.modules.token_embedders.embedding.Embedding, decoder_cell: torch.nn.modules.rnn.GRUCell, output_projection_layer: torch.nn.modules.linear.Linear) → torch.Tensor[source]

Greedily produces a sequence using the provided decoder_cell. Returns the predicted sequence.

Parameters
final_encoder_outputtorch.LongTensor, required

Vector produced by self._encoder.

target_embedderEmbedding, required

Used to embed the target tokens.

decoder_cell: ``GRUCell``, required

The recurrent cell used at each time step.

output_projection_layer: ``Linear``, required

Linear layer mapping to the desired number of classes.

Greedily produces a sequence using the provided decoder_cell. Returns the cross entropy between this sequence and target_tokens.

Parameters
final_encoder_outputtorch.LongTensor, required

Vector produced by self._encoder.

target_tokensDict[str, torch.LongTensor], required

The output of TextField.as_array() applied on some target TextField.

target_embedderEmbedding, required

Used to embed the target tokens.

decoder_cell: ``GRUCell``, required

The recurrent cell used at each time step.

output_projection_layer: ``Linear``, required

Linear layer mapping to the desired number of classes.

class allennlp.models.event2mind.StateDecoder(num_classes: int, input_dim: int, output_dim: int)[source]

Bases: torch.nn.modules.module.Module

Simple struct-like class for internal use.

take_step(self, last_predictions: torch.Tensor, state: Dict[str, torch.Tensor]) → Tuple[torch.Tensor, Dict[str, torch.Tensor]][source]