SequenceClassifier

class opennmt.models.SequenceClassifier(*args, **kwargs)[source]

A sequence classifier.

Inherits from: opennmt.models.Model

__init__(inputter, encoder)[source]

Initializes a sequence classifier.

Parameters
Raises

ValueError – if encoding is invalid.

build(input_shape)[source]

Creates the variables of the layer (for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

Parameters

input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

call(features, labels=None, training=None, step=None)[source]

Runs the model.

Parameters
  • features – A nested structure of features tf.Tensor.

  • labels – A nested structure of labels tf.Tensor.

  • training – If True, run in training mode.

  • step – The current training step.

Returns

A tuple containing,

  • The model outputs (usually unscaled probabilities).

  • The model predictions.

compute_loss(outputs, labels, training=True)[source]

Computes the loss.

Parameters
  • outputs – The model outputs (usually unscaled probabilities).

  • labels – The dict of labels tf.Tensor.

  • training – If True, compute the loss for training.

Returns

The loss or a tuple (numerator, train_denominator, stats_denominator) to use a different normalization for training compared to reporting (e.g. batch-normalized for training vs. token-normalized for reporting).

get_metrics()[source]

Returns the metrics for this model.

Returns

A dictionary of tf.keras.metrics.Metric metrics.

update_metrics(metrics, predictions, labels)[source]

Computes additional metrics on the predictions.

Parameters
  • metrics – A dictionary of metrics to update.

  • predictions – The model predictions.

  • labels – The dict of labels tf.Tensor.

format_prediction(prediction, params=None)[source]

Formats the model prediction for file saving.

Parameters
  • prediction – The model prediction (same structure as the second output of opennmt.models.Model.call()).

  • params – (optional) Dictionary of formatting parameters.

Returns

A string or list of strings.