Class LogisticRegressorQualityEstimator

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

Class Documentation

class LogisticRegressorQualityEstimator : public marian::bergamot::QualityEstimator

LogisticRegressorQualityEstimator model implementation through a linear regressor + sigmoid function.

Simply speaking, an LR model depends on features to be scaled, so it contains four elements of data: a vector of coefficients and an intercept (which represents the linear model) and a vector of means and stds (which are necessary for feature scaling). These variables are firstly initialized by parsing a file (which comes from fromAlignedMemory), and then they are used to build a model representation

Public Types

using Array = std::array<float, 4>

Public Functions

LogisticRegressorQualityEstimator(Scale &&scale, Array &&coefficients, const float intercept)

Logistic Regressor constructor.

It creates a LR model that fits proper for the QualityEstimator use.

Parameters
  • [in] scale: Array of stds and means that can be used to apply standard scaling in the features

  • [in] coefficients: coefficient values of linear part of LR model

  • [in] intercept: intercept value of the linear part of LR model

LogisticRegressorQualityEstimator(LogisticRegressorQualityEstimator &&other)

Move constructor.

AlignedMemory toAlignedMemory() const
void computeQualityScores(const Histories &histories, Response &response) const

Computes quality-scores using values from Histories and subword tokens which comes from Response.

Parameters
  • [in] histories: Histories obtained from translating a blob of source-text

  • [in] response: Partially constructed response, holding tokenization info for source and target. The quality-scores for each sentence obtained from source-text blob are written out as SentenceQualityEstimate into response.

std::vector<float> predict(const Matrix &features) const

Given an input matrix \(\mathbf{X}\), the usual Logistic Regression calculus can be seen as the following:

1) Standardize it, returning in \(\mathbf{Z} = \frac{(\mathbf{X}-\mu)}{\sigma}\), where \(\mu\) stands for the mean vector and \(\sigma\) represents the standard deviation

2) Then, we apply \(\sum_{i=1}^{D}{ w_i z_i}\), where \(D\) is the dimension (i.e. the number of features) and \(w\) is the model vector with learnt weights

3) We apply the sigmoid function to the result

Notice, however, that for the first two steps we can do the following:

\[\begin{split}\begin{align*} \sum_{i=1}^{D}{ w_i z_i} &= \mathbf{w^T}\left(\mathbf{\sigma^{-1}} \odot (\mathbf{x} - \mathbf{\mu})\right) \text{ // we are just vectoring step 1}\\ &= \sum_{i=1}^{D}{\sigma_i^{-1} w_i (x_i - \mu_i)} \\ &= \sum_{i=1}^{D}{\sigma_i^{-1} w_ix_i - \sigma_i^{-1} w_i \mu_i} \\ &= \sum_{i=1}^{D}{\left(\sigma_i^{-1} w_i\right)x_i - \left(\sigma_i^{-1} w_i \mu_i\right)} \end{align*}\end{split}\]
Then, \((\sigma_i^{-1} w_i \mu_i)\) can be precomputed without any dependence on inference data. This is done by the variable \(\textit{constantFactor_}\) and \(\textit{intercept_}\) in the code.

Parameters
  • [in] features: A Matrix struct of features. For a defintion what features currently means, please refer to extractFeatures method in quality_estimator.cpp

Public Static Functions

LogisticRegressorQualityEstimator fromAlignedMemory(const AlignedMemory &alignedMemory)

Binary file parser which came from AlignedMemory It’s expected from AlignedMemory the following structure:

  • -a header with the number of parameters dimensions

  • -a vector of standard deviations of features

  • -a vector of means of features

  • -a vector of coefficients

  • -a intercept value

struct Header

Public Members

uint64_t magic

Binary QE File magic number.

uint64_t lrParametersDims

Length of lr parameters stds, means and coefficients.

class Matrix

Matrix is an internal data structure that was created only to be used in LogisticRegressorQualityEstimator methods.

It intends to represent a matrix, so it receives row and column values as a constructor. Furthermore, the method at can access specific data given a row and col position.

Public Functions

Matrix(const size_t rowsParam, const size_t colsParam)

Parameters
  • [in] rowsParam: number of rows in the Matrix

  • [in] colsParam: number of columns in the Matrix

Matrix(Matrix &&other)

Move constructor.

const float &at(const size_t row, const size_t col) const

Return data value given a row and col position.

Parameters
  • [in] row: row position

  • [in] col: col position

float &at(const size_t row, const size_t col)

Public Members

const size_t rows

Number of rows.

const size_t cols

Number of columns.

struct Scale

Struct that contains information for applying standard scaling.

Public Members

Array stds

Array of standard deviations of feature values. Its length will be equals as featureDims.

Array means

Array of mean of feature values. Its length will be equals as featureDims.