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 aModel
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
- vocab
Vocabulary
, 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 astarget_namespace
.- source_embedder
TextFieldEmbedder
, required Embedder for source side sequences.
- embedding_dropout: float, required
The amount of dropout to apply after the source tokens have been embedded.
- encoder
Seq2VecEncoder
, 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.
- regularizer
RegularizerApplicator
, optional (default=``None``) If provided, will be used to calculate the regularization penalty during training.
- vocab
-
decode
(self, output_dict: Dict[str, torch.Tensor]) → Dict[str, List[List[str]]][source]¶ This method overrides
Model.decode
, which gets called afterModel.forward
, at test time, to finalize predictions. The logic for the decoder part of the encoder-decoder lives within theforward
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
.
-
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
- source
Dict[str, torch.LongTensor]
The output of
TextField.as_array()
applied on the sourceTextField
. This will be passed through aTextFieldEmbedder
and then through an encoder.- target_tokens
Dict[str, Dict[str, torch.LongTensor]]
: Dictionary from name to output of
Textfield.as_array()
applied on targetTextField
. We assume that the target tokens are also represented as aTextField
.
- source
-
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 withMetrics should be populated during the call to ``forward`
, with theMetric
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_output
torch.LongTensor
, required Vector produced by
self._encoder
.- target_embedder
Embedding
, 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.
- final_encoder_output
-
greedy_search
(self, final_encoder_output: torch.LongTensor, target_tokens: Dict[str, 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.FloatTensor[source]¶ Greedily produces a sequence using the provided
decoder_cell
. Returns the cross entropy between this sequence andtarget_tokens
.- Parameters
- final_encoder_output
torch.LongTensor
, required Vector produced by
self._encoder
.- target_tokens
Dict[str, torch.LongTensor]
, required The output of
TextField.as_array()
applied on some targetTextField
.- target_embedder
Embedding
, 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.
- final_encoder_output