FwFMClassifier¶
Field-weighted Factorization Machine for binary classification.
The model equation is defined as:
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 – defaults to
10Dimensionality of the factorization or number of latent factors.
-
weight_optimizer (optim.base.Optimizer) – defaults to
NoneThe sequential optimizer used for updating the feature weights. Note that the intercept is handled separately.
-
latent_optimizer (optim.base.Optimizer) – defaults to
NoneThe sequential optimizer used for updating the latent factors.
-
int_weight_optimizer (optim.base.Optimizer) – defaults to
NoneThe sequential optimizer used for updating the field pairs interaction weights.
-
loss (optim.losses.BinaryLoss) – defaults to
NoneThe loss function to optimize for.
-
sample_normalization – defaults to
FalseWhether to divide each element of
xbyx's L2-norm. -
l1_weight – defaults to
0.0Amount of L1 regularization used to push weights towards 0.
-
l2_weight – defaults to
0.0Amount of L2 regularization used to push weights towards 0.
-
l1_latent – defaults to
0.0Amount of L1 regularization used to push latent weights towards 0.
-
l2_latent – defaults to
0.0Amount of L2 regularization used to push latent weights towards 0.
-
intercept – defaults to
0.0Initial intercept value.
-
intercept_lr (Union[optim.base.Scheduler, float]) – defaults to
0.01Learning rate scheduler used for updating the intercept. An instance of
optim.schedulers.Constantis used if afloatis passed. No intercept will be used if this is set to 0. -
weight_initializer (optim.base.Initializer) – defaults to
NoneWeights initialization scheme. Defaults to
optim.initializers.Zeros(). -
latent_initializer (optim.base.Initializer) – defaults to
NoneLatent factors initialization scheme. Defaults to
optim.initializers.Normal(mu=.0, sigma=.1, random_state=self.random_state). -
clip_gradient – defaults to
1000000000000.0Clips the absolute value of each gradient value.
-
seed (int) – defaults to
NoneRandomization 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'}, True),
... ({'user': 'Alice', 'item': 'Terminator'}, True),
... ({'user': 'Alice', 'item': 'Star Wars'}, True),
... ({'user': 'Alice', 'item': 'Notting Hill'}, False),
... ({'user': 'Alice', 'item': 'Harry Potter '}, True),
... ({'user': 'Bob', 'item': 'Superman'}, True),
... ({'user': 'Bob', 'item': 'Terminator'}, True),
... ({'user': 'Bob', 'item': 'Star Wars'}, True),
... ({'user': 'Bob', 'item': 'Notting Hill'}, False)
... )
>>> model = facto.FwFMClassifier(
... n_factors=10,
... seed=42,
... )
>>> for x, y in dataset:
... model = model.learn_one(x, y)
>>> model.predict_one({'Bob': 1, 'Harry Potter': 1})
True
Methods¶
clone
Return a fresh estimator with the same parameters.
The clone has the same parameters but has not been updated with any data. This works by looking at the parameters from the class signature. Each parameter is either - recursively cloned if it's a River classes. - deep-copied via copy.deepcopy if not. If the calling object is stochastic (i.e. it accepts a seed parameter) and has not been seeded, then the clone will not be idempotent. Indeed, this method's purpose if simply to return a new instance with the same input parameters.
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
Update the model with a set of features x and a label y.
Parameters
- x (dict)
- y (Union[bool, str, int])
- sample_weight – defaults to
1.0
Returns
Classifier: self
predict_one
Predict the label of a set of features x.
Parameters
- x (dict)
Returns
typing.Union[bool, str, int]: The predicted label.
predict_proba_one
Predict the probability of each label for a dictionary of features x.
Parameters
- x
Returns
A dictionary that associates a probability which each label.
References¶
-
Junwei Pan, Jian Xu, Alfonso Lobos Ruiz, Wenliang Zhao, Shengjun Pan, Yu Sun, and Quan Lu, 2018, April. Field-weighted Factorization Machines for Click-Through Rate Prediction in Display Advertising. In Proceedings of the 2018 World Wide Web Conference on World Wide Web. International World Wide Web Conferences Steering Committee, (pp. 1349–1357). ↩