kiwi.models package

Submodules

kiwi.models.linear_word_qe_classifier module

This is the main script for the linear sequential word-based quality estimator.

class kiwi.models.linear_word_qe_classifier.LinearWordQEClassifier(use_basic_features_only=True, use_bigrams=True, use_simple_bigram_features=True, use_parse_features=False, use_stacked_features=False, evaluation_metric='f1_bad', cost_false_positives=0.5, cost_false_negatives=0.5)[source]

Bases: kiwi.models.linear.structured_classifier.StructuredClassifier

Main class for the word-level quality estimator. Inherits from a general structured classifier.

create_instances(dataset)[source]

Preprocess the dataset if needed to create instances. Default is returning the dataset itself. Override if needed.

create_prediction(instance, parts, predicted_output)[source]

Creates a list of word-level predictions for a sentence. For compliance with probabilities, it returns 1 if label is BAD, 0 if OK.

evaluate(instances, predictions, print_scores=True)[source]

Evaluates the model’s accuracy and F1-BAD score.

static fieldset(*args, **kwargs)[source]
static from_options(vocabs, opts)[source]
get_coarse_label(label)[source]

Get the coarse part of a fine-grained label. The coarse label is the prefix before the underscore (if any). For example, the coarse part of BAD_SUB, BAD_DEL, and BAD is BAD.

label_instance(instance, parts, predicted_output)[source]

Return a labeled instance by adding the predicted output information.

load(model_path)[source]

Load the full configuration and model.

load_configuration(config)[source]
make_features(instance, parts)[source]

Creates a feature vector for each part.

make_parts(instance)[source]

Creates the parts (unigrams and bigrams) for an instance.

num_parameters()[source]
save(model_path)[source]

Save the full configuration and model.

save_configuration()[source]
test(instances)[source]

Run the model on test data.

title = 'Linear Model'

kiwi.models.model module

class kiwi.models.model.Model(vocabs, ConfigCls=<class 'kiwi.models.model.ModelConfig'>, config=None, **kwargs)[source]

Bases: torch.nn.modules.module.Module

static create_from_file(path)[source]
forward(*args, **kwargs)[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.

classmethod from_dict(model_dict)[source]
classmethod from_file(path)[source]
get_mask(batch, output)[source]

Compute Mask of Tokens for side.

Parameters:
  • batch – Namespace of tensors
  • side – String identifier.
loss(model_out, target)[source]
num_parameters()[source]
predict(batch, class_name='BAD', unmask=True)[source]
predict_raw(examples)[source]
preprocess(examples)[source]

Preprocess Raw Data.

Parameters:examples (list of dict) – List of examples. Each Example is a dict with field strings as keys, and unnumericalized, tokenized data as values.
Returns:A batch object.
classmethod register_subclass(subclass)[source]
save(path)[source]
subclasses = {'Estimator': <class 'kiwi.models.predictor_estimator.Estimator'>, 'NuQE': <class 'kiwi.models.nuqe.NuQE'>, 'Predictor': <class 'kiwi.models.predictor.Predictor'>, 'QUETCH': <class 'kiwi.models.quetch.QUETCH'>}
class kiwi.models.model.ModelConfig(vocabs)[source]

Bases: object

classmethod from_dict(config_dict, vocabs)[source]

Create config from a saved state_dict. :param config_dict: A dictionary that is the return value of

a call to the state_dict() method of cls
Parameters:vocab – See ModelConfig.__init__
state_dict()[source]

Return the __dict__ for serialization.

update(other_config)[source]

Updates the config object with the values of other_config :param other_config: The dict or ModelConfig object to update with.

kiwi.models.nuqe module

class kiwi.models.nuqe.NuQE(vocabs, **kwargs)[source]

Bases: kiwi.models.quetch.QUETCH

Neural Quality Estimation (NuQE) model for word level quality estimation.

build(source_vectors=None, target_vectors=None)[source]
static fieldset(*args, **kwargs)[source]
forward(batch)[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.

static from_options(vocabs, opts)[source]
title = 'NuQE'

kiwi.models.predictor module

class kiwi.models.predictor.Predictor(vocabs, **kwargs)[source]

Bases: kiwi.models.model.Model

Bidirectional Conditional Language Model

Implemented after Kim et al 2017, see:
http://www.statmt.org/wmt17/pdf/WMT63.pdf
static fieldset(*args, **kwargs)[source]
forward(batch, source_side=None, target_side=None)[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.

static from_options(vocabs, opts)[source]
Parameters:
  • vocabs
  • opts

Returns:

loss(model_out, batch, target_side=None)[source]
metrics()[source]
metrics_ordering()[source]
title = 'PredEst Predictor model (an embedder model)'
class kiwi.models.predictor.PredictorConfig(vocabs, hidden_pred=400, rnn_layers_pred=3, dropout_pred=0.0, share_embeddings=False, embedding_sizes=0, target_embeddings_size=200, source_embeddings_size=200, out_embeddings_size=200, predict_inverse=False)[source]

Bases: kiwi.models.model.ModelConfig

kiwi.models.predictor_estimator module

class kiwi.models.predictor_estimator.Estimator(vocabs, predictor_tgt=None, predictor_src=None, **kwargs)[source]

Bases: kiwi.models.model.Model

binary_loss(model_out, batch)[source]

Compute Sentence Classification Loss

static fieldset(*args, **kwargs)[source]
forward(batch)[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.

static from_options(vocabs, opts)[source]
Parameters:
  • vocabs
  • opts

    predict_target (bool): Predict target tags predict_source (bool): Predict source tags predict_gaps (bool): Predict gap tags token_level (bool): Train predictor using PE field. sentence_level (bool): Predict Sentence Scores sentence_ll (bool): Use likelihood loss for sentence scores

    (instead of squared error)

    binary_level: Predict binary sentence labels target_bad_weight: Weight for target tags bad class. Default 3.0 source_bad_weight: Weight for source tags bad class. Default 3.0 gaps_bad_weight: Weight for gap tags bad class. Default 3.0

Returns:

loss(model_out, batch)[source]

Compute Model Loss

make_contexts_gaps(contexts)[source]
make_input(model_out, batch, tagset)[source]

Make Input Sequence from predictor outputs.

make_sentence_input(h_tgt, h_src)[source]

Reshape last hidden state.

metrics()[source]
metrics_ordering()[source]
predict_sentence(sentence_input)[source]

Compute Sentence Score predictions.

predict_tags(contexts, out_embed=None)[source]

Compute Tag Predictions.

sentence_loss(model_out, batch)[source]

Compute Sentence score loss

title = 'PredEst (Predictor-Estimator)'
word_loss(model_out, batch)[source]

Compute Sequence Tagging Loss

class kiwi.models.predictor_estimator.EstimatorConfig(vocabs, hidden_est=100, rnn_layers_est=1, mlp_est=True, dropout_est=0.0, start_stop=False, predict_target=True, predict_gaps=False, predict_source=False, token_level=True, sentence_level=True, sentence_ll=True, binary_level=True, target_bad_weight=2.0, source_bad_weight=2.0, gaps_bad_weight=2.0, **kwargs)[source]

Bases: kiwi.models.predictor.PredictorConfig

kiwi.models.quetch module

class kiwi.models.quetch.QUETCH(vocabs, **kwargs)[source]

Bases: kiwi.models.model.Model

QUality Estimation from scraTCH (QUETCH) model.

TODO: add references.

build(source_vectors=None, target_vectors=None)[source]
static fieldset(*args, **kwargs)[source]
forward(batch)[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.

static from_options(vocabs, opts)[source]
loss(model_out, target)[source]
make_input(batch, side)[source]
metrics()[source]
metrics_ordering()[source]
title = 'QUETCH'
class kiwi.models.quetch.QUETCHConfig(vocabs, predict_target=True, predict_gaps=False, predict_source=False, source_embeddings_size=50, target_embeddings_size=50, hidden_sizes=None, bad_weight=3.0, window_size=10, max_aligned=5, dropout=0.4, embeddings_dropout=0.4, freeze_embeddings=False)[source]

Bases: kiwi.models.model.ModelConfig

kiwi.models.utils module

class kiwi.models.utils.GradientMul[source]

Bases: torch.autograd.function.Function

static backward(ctx, grad)[source]

Defines a formula for differentiating the operation.

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by as many outputs did forward() return, and it should return as many tensors, as there were inputs to forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input.

The context can be used to retrieve tensors saved during the forward pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if the first input to forward() needs gradient computated w.r.t. the output.

static forward(ctx, x, constant=0)[source]

Performs the operation.

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).

The context can be used to store tensors that can be then retrieved during the backward pass.

kiwi.models.utils.align_source(source, trg2src_alignments, max_aligned, unaligned_idx, padding_idx, pad_size)[source]
kiwi.models.utils.align_tensor(tensor, alignments, max_aligned, unaligned_idx, padding_idx, pad_size, target_length=None)[source]
kiwi.models.utils.apply_packed_sequence(rnn, embedding, lengths)[source]

Runs a forward pass of embeddings through an rnn using packed sequence. :param rnn: The RNN that that we want to compute a forward pass with. :param embedding: A batch of sequence embeddings. :type embedding: FloatTensor b x seq x dim :param lengths: The length of each sequence in the batch. :type lengths: LongTensor batch

Returns:The output of the RNN rnn with input embedding
Return type:output
kiwi.models.utils.convolve_tensor(sequences, window_size, pad_value=0)[source]

Convolve a sequence and apply padding

Parameters:
  • sequence – 2D tensor
  • window_size – filter length
  • pad_value – int value used as padding
Returns:

3D tensor, where the last dimension has size window_size

kiwi.models.utils.gradient_mul()
kiwi.models.utils.load_torch_file(file_path)[source]
kiwi.models.utils.make_loss_weights(nb_classes, target_idx, weight)[source]

Creates a loss weight vector for nn.CrossEntropyLoss

Parameters:
  • nb_classes – Number of classes
  • target_idx – ID of the target (reweighted) class
  • weight – Weight of the target class
Returns:

Weight Tensor of shape nb_classes such that

weights[target_idx] = weight weights[other_idx] = 1.0

Return type:

weights (FloatTensor)

kiwi.models.utils.map_alignments_to_target(src2tgt_alignments, target_length=None)[source]

Maps a target index to a list of source indexes.

Parameters:
  • src2tgt_alignments (list) – list of tuples with source, target indexes.
  • target_length – size of the target side; if None, the highest index in the alignments is used.
Returns:

A list of size target_length where position i refers to the i-th target token and contains a list of source indexes aligned to it.

kiwi.models.utils.replace_token(target, old, new)[source]

Replaces old tokens with new.

args: target (LongTensor)
old (int): The token to be replaced by new new (int): The token used to replace old
kiwi.models.utils.unroll(list_of_lists)[source]
Parameters:
  • list_of_lists – a list that contains lists
  • rec – unroll recursively
Returns:

a flattened list

Module contents