convlab2.nlu.milu package¶
Subpackages¶
Submodules¶
convlab2.nlu.milu.dai_f1_measure module¶
-
class
convlab2.nlu.milu.dai_f1_measure.
DialogActItemF1Measure
¶ Bases:
allennlp.training.metrics.metric.Metric
-
get_metric
(reset: bool = False)¶ A Dict per label containing following the span based metrics: precision : float recall : float f1-measure : float
Additionally, an
overall
key is included, which provides the precision, recall and f1-measure for all spans.
-
reset
()¶ Reset any accumulators or internal state.
-
convlab2.nlu.milu.dataset_reader module¶
-
class
convlab2.nlu.milu.dataset_reader.
MILUDatasetReader
(context_size: int = 0, agent: Optional[str] = None, random_context_size: bool = True, token_delimiter: Optional[str] = None, token_indexers: Optional[Dict[str, allennlp.data.token_indexers.token_indexer.TokenIndexer]] = None, lazy: bool = False)¶ Bases:
allennlp.data.dataset_readers.dataset_reader.DatasetReader
Reads instances from a pretokenised file where each line is in the following format:
WORD###TAG [TAB] WORD###TAG [TAB] …..
and converts it into a
Dataset
suitable for sequence tagging. You can also specify alternative delimiters in the constructor.- word_tag_delimiter:
str
, optional (default=``”###”``) The text that separates each WORD from its TAG.
- token_delimiter:
str
, optional (default=``None``) The text that separates each WORD-TAG pair from the next pair. If
None
then the line will just be split on whitespace.- token_indexers
Dict[str, TokenIndexer]
, optional (default=``{“tokens”: SingleIdTokenIndexer()}``) We use this to define the input representation for the text. See
TokenIndexer
. Note that the output tags will always correspond to single token IDs based on how they are pre-tokenised in the data file.
-
text_to_instance
(context_tokens: List[allennlp.data.tokenizers.token.Token], tokens: List[allennlp.data.tokenizers.token.Token], tags: Optional[List[str]] = None, intents: Optional[List[str]] = None, dialog_act: Optional[Dict[str, Any]] = None) → allennlp.data.instance.Instance¶ We take pre-tokenized input here, because we don’t have a tokenizer in this class.
- word_tag_delimiter:
convlab2.nlu.milu.evaluate module¶
The evaluate
subcommand can be used to
evaluate a trained model against a dataset
and report any metrics calculated by the model.
-
convlab2.nlu.milu.evaluate.
evaluate_from_args
(args: argparse.Namespace) → Dict[str, Any]¶
convlab2.nlu.milu.model module¶
-
class
convlab2.nlu.milu.model.
MILU
(vocab: allennlp.data.vocabulary.Vocabulary, text_field_embedder: allennlp.modules.text_field_embedders.text_field_embedder.TextFieldEmbedder, encoder: allennlp.modules.seq2seq_encoders.seq2seq_encoder.Seq2SeqEncoder, intent_encoder: Optional[allennlp.modules.seq2seq_encoders.seq2seq_encoder.Seq2SeqEncoder] = None, tag_encoder: Optional[allennlp.modules.seq2seq_encoders.seq2seq_encoder.Seq2SeqEncoder] = None, attention: Optional[allennlp.modules.attention.attention.Attention] = None, attention_function: Optional[allennlp.modules.similarity_functions.similarity_function.SimilarityFunction] = None, context_for_intent: bool = True, context_for_tag: bool = True, attention_for_intent: bool = True, attention_for_tag: bool = True, sequence_label_namespace: str = 'labels', intent_label_namespace: str = 'intent_labels', feedforward: Optional[allennlp.modules.feedforward.FeedForward] = None, label_encoding: Optional[str] = None, include_start_end_transitions: bool = True, crf_decoding: bool = False, constrain_crf_decoding: Optional[bool] = None, focal_loss_gamma: Optional[float] = None, nongeneral_intent_weight: float = 5.0, num_train_examples: Optional[float] = None, calculate_span_f1: Optional[bool] = None, dropout: Optional[float] = None, verbose_metrics: bool = False, initializer: allennlp.nn.initializers.InitializerApplicator = <allennlp.nn.initializers.InitializerApplicator object>, regularizer: Optional[allennlp.nn.regularizers.regularizer_applicator.RegularizerApplicator] = None)¶ Bases:
allennlp.models.model.Model
The
MILU
encodes a sequence of text with aSeq2SeqEncoder
, then performs multi-label classification for closed-class dialog act items and sequence labeling to predict a tag for each token in the sequence.- initializer
InitializerApplicator
, optional (default=``InitializerApplicator()``) Used to initialize the model parameters.
- regularizer
RegularizerApplicator
, optional (default=``None``) If provided, will be used to calculate the regularization penalty during training.
-
decode
(output_dict: Dict[str, torch.Tensor]) → Dict[str, torch.Tensor]¶ Converts the tag ids to the actual tags.
output_dict["tags"]
is a list of lists of tag_ids, so we use an ugly nested list comprehension.
-
forward
(context_tokens: Dict[str, torch.LongTensor], tokens: Dict[str, torch.LongTensor], tags: Optional[torch.LongTensor] = None, intents: Optional[torch.LongTensor] = None, metadata: Optional[List[Dict[str, Any]]] = None, **kwargs) → Dict[str, torch.Tensor]¶
-
get_metrics
(reset: bool = False) → Dict[str, float]¶ 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.
Does a simple position-wise argmax over each token, converts indices to string labels, and adds a
"tags"
key to the dictionary with the result.
- initializer
convlab2.nlu.milu.multilabel_f1_measure module¶
-
class
convlab2.nlu.milu.multilabel_f1_measure.
MultiLabelF1Measure
(vocabulary: allennlp.data.vocabulary.Vocabulary, namespace: str = 'intent_labels', ignore_classes: Optional[List[str]] = None, coarse: bool = True)¶ Bases:
allennlp.training.metrics.metric.Metric
-
get_metric
(reset: bool = False)¶ A Dict per label containing following the span based metrics: precision : float recall : float f1-measure : float
Additionally, an
overall
key is included, which provides the precision, recall and f1-measure for all spans.
-
reset
()¶ Reset any accumulators or internal state.
-
convlab2.nlu.milu.train module¶
The train
subcommand can be used to train a model.
It requires a configuration file and a directory in
which to write the results.
-
convlab2.nlu.milu.train.
train_model
(params: allennlp.common.params.Params, serialization_dir: str, file_friendly_logging: bool = False, recover: bool = False, force: bool = False) → allennlp.models.model.Model¶ Trains the model specified in the given
Params
object, using the data and training parameters also specified in that object, and saves the results inserialization_dir
.- params
Params
A parameter object specifying an AllenNLP Experiment.
- serialization_dir
str
The directory in which to save results and logs.
- file_friendly_logging
bool
, optional (default=False) If
True
, we add newlines to tqdm output, even on an interactive terminal, and we slow down tqdm’s output to only once every 10 seconds.- recover
bool
, optional (default=False) If
True
, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see thefine-tune
command.- force
bool
, optional (default=False) If
True
, we will overwrite the serialization directory if it already exists.
- best_model:
Model
The model with the best epoch weights.
- params
-
convlab2.nlu.milu.train.
train_model_from_args
(args: argparse.Namespace)¶ Just converts from an
argparse.Namespace
object to string paths.
-
convlab2.nlu.milu.train.
train_model_from_file
(parameter_filename: str, serialization_dir: str, overrides: str = '', file_friendly_logging: bool = False, recover: bool = False, force: bool = False) → allennlp.models.model.Model¶ A wrapper around
train_model()
which loads the params from a file.- parameter_filename
str
A json parameter file specifying an AllenNLP experiment.
- serialization_dir
str
The directory in which to save results and logs. We just pass this along to
train_model()
.- overrides
str
A JSON string that we will use to override values in the input parameter file.
- file_friendly_logging
bool
, optional (default=False) If
True
, we make our output more friendly to saved model files. We just pass this along totrain_model()
.- recover``bool`, optional (default=False)
If
True
, we will try to recover a training run from an existing serialization directory. This is only intended for use when something actually crashed during the middle of a run. For continuing training a model on new data, see thefine-tune
command.- force
bool
, optional (default=False) If
True
, we will overwrite the serialization directory if it already exists.
- parameter_filename