Class LogisticRegressorQualityEstimator¶
Defined in File quality_estimator.h
Nested Relationships¶
Nested Types¶
Inheritance Relationships¶
Base Type¶
public marian::bergamot::QualityEstimator
(Class QualityEstimator)
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 representationPublic 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 toextractFeatures
method inquality_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
¶
-
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.
-
struct
Scale
¶ Struct that contains information for applying standard scaling.
-
using