# SpectralCF¶

Reference:

Lei Zheng et al. “Spectral collaborative filtering.” in RecSys 2018.

Reference code:

https://github.com/lzheng21/SpectralCF

class recbole.model.general_recommender.spectralcf.SpectralCF(config, dataset)[source]

SpectralCF is a spectral convolution model that directly learns latent factors of users and items from the spectral domain for recommendation.

The spectral convolution operation with C input channels and F filters is shown as the following:

$\begin{split}\left[\begin{array} {c} X_{new}^{u} \\ X_{new}^{i} \end{array}\right]=\sigma\left(\left(U U^{\top}+U \Lambda U^{\top}\right) \left[\begin{array}{c} X^{u} \\ X^{i} \end{array}\right] \Theta^{\prime}\right)\end{split}$

where $$X_{new}^{u} \in R^{n_{users} \times F}$$ and $$X_{new}^{i} \in R^{n_{items} \times F}$$ denote convolution results learned with F filters from the spectral domain for users and items, respectively; $$\sigma$$ denotes the logistic sigmoid function.

Note

Our implementation is a improved version which is different from the original paper. For a better stability, we replace $$U U^T$$ with identity matrix $$I$$ and replace $$U \Lambda U^T$$ with laplace matrix $$L$$.

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()[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.

full_sort_predict(interaction)[source]

full sort prediction function. Given users, calculate the scores between users and all candidate items.

Parameters

interaction (Interaction) – Interaction class of the batch.

Returns

Predicted scores for given users and all candidate items, shape: [n_batch_users * n_candidate_items]

Return type

torch.Tensor

get_ego_embeddings()[source]

Get the embedding of users and items and combine to an embedding matrix.

Returns

Tensor of the embedding matrix. Shape of (n_items+n_users, embedding_dim)

get_eye_mat(num)[source]

Construct the identity matrix with the size of n_items+n_users.

Parameters

num – number of column of the square matrix

Returns

Sparse tensor of the identity matrix. Shape of (n_items+n_users, n_items+n_users)

get_laplacian_matrix()[source]

Get the laplacian matrix of users and items.

$L = I - D^{-1} \times A$
Returns

Sparse tensor of the laplacian matrix.

input_type = 2
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

training: bool