FFM

Reference:

Yuchin Juan et al. “Field-aware Factorization Machines for CTR Prediction” in RecSys 2016.

Reference code:

https://github.com/rixwew/pytorch-fm

class recbole.model.context_aware_recommender.ffm.FFM(config, dataset)[source]

Bases: recbole.model.abstract_recommender.ContextRecommender

FFM is a context-based recommendation model. It aims to model the different feature interactions between different fields. Each feature has several latent vectors \(v_{i,F(j)}\), which depend on the field of other features, and one of them is used to do the inner product.

The model defines as follows:

\[y = w_0 + \sum_{i=1}^{m}x_{i}w_{i} + \sum_{i=1}^{m}\sum_{j=i+1}^{m}x_{i}x_{j}<v_{i,F(j)}, v_{j,F(i)}>\]
calculate_loss(interaction)[source]

Calculate the training loss for a batch data.

Parameters

interaction (Interaction) – Interaction class of the batch.

Returns

Training loss, shape: []

Return type

torch.Tensor

forward(interaction)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_ffm_input(interaction)[source]

Get different types of ffm layer’s input.

predict(interaction)[source]

Predict the scores between users and items.

Parameters

interaction (Interaction) – Interaction class of the batch.

Returns

Predicted scores for given users and items, shape: [batch_size]

Return type

torch.Tensor

class recbole.model.context_aware_recommender.ffm.FieldAwareFactorizationMachine(feature_names, feature_dims, feature2id, feature2field, num_fields, embed_dim, device)[source]

Bases: torch.nn.modules.module.Module

This is Field-Aware Factorization Machine Module for FFM.

forward(input_x)[source]

Model the different interaction strengths of different field pairs.

Parameters

input_x (a tuple) –

(token_ffm_input, float_ffm_input, token_seq_ffm_input)

token_ffm_input (torch.cuda.FloatTensor): [batch_size, num_token_features] or None

float_ffm_input (torch.cuda.FloatTensor): [batch_size, num_float_features] or None

token_seq_ffm_input (list): length is num_token_seq_features or 0

Returns

The results of all features’ field-aware interactions. shape: [batch_size, num_fields, emb_dim]

Return type

torch.cuda.FloatTensor