hopwise.model.context_aware_recommender.ffm

Reference:

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

Reference code:

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

Classes

FFM

FFM is a context-based recommendation model. It aims to model the different feature interactions

FieldAwareFactorizationMachine

This is Field-Aware Factorization Machine Module for FFM.

Module Contents

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

Bases: hopwise.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)}>\]
fields
sigmoid
feature2id
feature2field
feature_names
feature_dims
num_fields
ffm
loss
_init_weights(module)[source]
_get_feature2field()[source]

Create a mapping between features and fields.

get_ffm_input(interaction)[source]

Get different types of ffm layer’s input.

forward(interaction)[source]
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

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 hopwise.model.context_aware_recommender.ffm.FieldAwareFactorizationMachine(feature_names, feature_dims, feature2id, feature2field, num_fields, embed_dim, device)[source]

Bases: torch.nn.Module

This is Field-Aware Factorization Machine Module for FFM.

token_feature_names
float_feature_names
token_seq_feature_names
float_seq_feature_names
token_feature_dims
float_feature_dims
token_seq_feature_dims
float_seq_feature_dims
feature2id
feature2field
num_features
num_fields
embed_dim
device
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

_get_input_x_emb(token_input_x_emb, float_input_x_emb, token_seq_input_x_emb, float_seq_input_x_emb)[source]
_emb_token_ffm_input(token_ffm_input)[source]
_emb_float_ffm_input(float_ffm_input)[source]
_emb_token_seq_ffm_input(token_seq_ffm_input)[source]
_emb_float_seq_ffm_input(float_seq_ffm_input)[source]