Skip to content

FwFMRegressor

Field-weighted Factorization Machine for regression.

The model equation is defined as:

\[\hat{y}(x) = w_{0} + \sum_{j=1}^{p} w_{j} x_{j} + \sum_{j=1}^{p} \sum_{j'=j+1}^{p} r_{f_j, f_{j'}} \langle \mathbf{v}_j, \mathbf{v}_{j'} \rangle x_{j} x_{j'}\]

Where \(f_j\) and \(f_{j'}\) are \(j\) and \(j'\) fields, respectively, and \(\mathbf{v}_j\) and \(\mathbf{v}_{j'}\) are \(j\) and \(j'\) latent vectors, respectively.

For more efficiency, this model automatically one-hot encodes strings features considering them as categorical variables. Field names are inferred from feature names by taking everything before the first underscore: feature_name.split('_')[0].

Parameters

  • n_factors

    Default10

    Dimensionality of the factorization or number of latent factors.

  • weight_optimizer

    Typeoptim.base.Optimizer | None

    DefaultNone

    The sequential optimizer used for updating the feature weights. Note that the intercept is handled separately.

  • latent_optimizer

    Typeoptim.base.Optimizer | None

    DefaultNone

    The sequential optimizer used for updating the latent factors.

  • int_weight_optimizer

    Typeoptim.base.Optimizer | None

    DefaultNone

    The sequential optimizer used for updating the field pairs interaction weights.

  • loss

    Typeoptim.losses.RegressionLoss | None

    DefaultNone

    The loss function to optimize for.

  • sample_normalization

    DefaultFalse

    Whether to divide each element of x by x's L2-norm.

  • l1_weight

    Default0.0

    Amount of L1 regularization used to push weights towards 0.

  • l2_weight

    Default0.0

    Amount of L2 regularization used to push weights towards 0.

  • l1_latent

    Default0.0

    Amount of L1 regularization used to push latent weights towards 0.

  • l2_latent

    Default0.0

    Amount of L2 regularization used to push latent weights towards 0.

  • intercept

    Default0.0

    Initial intercept value.

  • intercept_lr

    Typeoptim.base.Scheduler | float

    Default0.01

    Learning rate scheduler used for updating the intercept. An instance of optim.schedulers.Constant is used if a float is passed. No intercept will be used if this is set to 0.

  • weight_initializer

    Typeoptim.initializers.Initializer | None

    DefaultNone

    Weights initialization scheme. Defaults to optim.initializers.Zeros()`.

  • latent_initializer

    Typeoptim.initializers.Initializer | None

    DefaultNone

    Latent factors initialization scheme. Defaults to optim.initializers.Normal(mu=.0, sigma=.1, random_state=self.random_state)`.

  • clip_gradient

    Default1000000000000.0

    Clips the absolute value of each gradient value.

  • seed

    Typeint | None

    DefaultNone

    Randomization seed used for reproducibility.

Attributes

  • weights

    The current weights assigned to the features.

  • latents

    The current latent weights assigned to the features.

  • interaction_weights

    The current interaction strengths of field pairs.

Examples

from river import facto

dataset = (
    ({'user': 'Alice', 'item': 'Superman'}, 8),
    ({'user': 'Alice', 'item': 'Terminator'}, 9),
    ({'user': 'Alice', 'item': 'Star Wars'}, 8),
    ({'user': 'Alice', 'item': 'Notting Hill'}, 2),
    ({'user': 'Alice', 'item': 'Harry Potter '}, 5),
    ({'user': 'Bob', 'item': 'Superman'}, 8),
    ({'user': 'Bob', 'item': 'Terminator'}, 9),
    ({'user': 'Bob', 'item': 'Star Wars'}, 8),
    ({'user': 'Bob', 'item': 'Notting Hill'}, 2)
)

model = facto.FwFMRegressor(
    n_factors=10,
    intercept=5,
    seed=42,
)

for x, y in dataset:
    model = model.learn_one(x, y)

model.predict_one({'Bob': 1, 'Harry Potter': 1})
5.236501

report = model.debug_one({'Bob': 1, 'Harry Potter': 1})

print(report)
Name                                    Value      Weight     Contribution
                            Intercept    1.00000    5.23426        5.23426
Bob(Harry Potter) - Harry Potter(Bob)    1.00000    0.00224        0.00224
                         Harry Potter    1.00000    0.00000        0.00000
                                  Bob    1.00000    0.00000        0.00000

Methods

debug_one

Debugs the output of the FM regressor.

Parameters

  • x'dict'
  • decimals'int' — defaults to 5

Returns

str: A table which explains the output.

learn_one

Fits to a set of features x and a real-valued target y.

Parameters

  • x'dict'
  • y'base.typing.RegTarget'
  • sample_weight — defaults to 1.0

Returns

Regressor: self

predict_one

Predict the output of features x.

Parameters

  • x

Returns

The prediction.