DiffRec

Reference:

Wenjie Wang et al. “Diffusion Recommender Model.” in SIGIR 2023.

Reference code:

https://github.com/YiyanXu/DiffRec

class recbole.model.general_recommender.diffrec.DNN(dims: List, emb_size: int, time_type='cat', act_func='tanh', norm=False, dropout=0.5)[source]

Bases: torch.nn.modules.module.Module

A deep neural network for the reverse diffusion preocess.

forward(x, timesteps)[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.

training: bool
class recbole.model.general_recommender.diffrec.DiffRec(config, dataset)[source]

Bases: recbole.model.abstract_recommender.GeneralRecommender, recbole.model.abstract_recommender.AutoEncoderMixin

DiffRec is a generative recommender model which infers users’ interaction probabilities in a denoising manner. Note that DiffRec simultaneously ranks all items for each user. We implement the the DiffRec model with only user dataloader.

SNR(t)[source]

Compute the signal-to-noise ratio for a single timestep.

build_histroy_items(dataset)[source]

Add time-aware reweighting to the original user-item interaction matrix when config[‘time-aware’] is True.

calculate_for_diffusion()[source]

Calculate the coefficients for the diffusion process.

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

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

Given the schedule name, create the betas for the diffusion process.

input_type = 3
p_mean_variance(x, t)[source]

Apply the model to get p(x_{t-1} | x_t), as well as a prediction of the initial x, x_0.

p_sample(x_start)[source]

Generate users’ interaction probabilities in a denoising manner. :param x_start: the input tensor that contains user’s history interaction matrix,

for DiffRec shape: [batch_size, n_items] for LDiffRec shape: [batch_size, hidden_size]

Returns

the interaction probabilities,

for DiffRec shape: [batch_size, n_items] for LDiffRec shape: [batch_size, hidden_size]

Return type

torch.FloatTensor

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

q_posterior_mean_variance(x_start, x_t, t)[source]
Compute the mean and variance of the diffusion posterior:

q(x_{t-1} | x_t, x_0)

q_sample(x_start, t, noise=None)[source]
reweight_loss(x_start, x_t, mse, ts, target, model_output, device)[source]
sample_timesteps(batch_size, device, method='uniform', uniform_prob=0.001)[source]
training: bool
update_Lt_history(ts, reloss)[source]
class recbole.model.general_recommender.diffrec.ModelMeanType(value)[source]

Bases: enum.Enum

An enumeration.

EPSILON = 2
START_X = 1
recbole.model.general_recommender.diffrec.betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999)[source]

Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of (1-beta) over time from t = [0,1].

Parameters
  • num_diffusion_timesteps (int) – the number of betas to produce.

  • alpha_bar (Callable) – a lambda that takes an argument t from 0 to 1 and produces the cumulative product of (1-beta) up to that part of the diffusion process.

  • max_beta (int) – the maximum beta to use; use values lower than 1 to prevent singularities.

Returns

a 1-D array of beta values.

Return type

np.ndarray

recbole.model.general_recommender.diffrec.betas_from_linear_variance(steps, variance, max_beta=0.999)[source]
recbole.model.general_recommender.diffrec.mean_flat(tensor)[source]

Take the mean over all non-batch dimensions.

recbole.model.general_recommender.diffrec.normal_kl(mean1, logvar1, mean2, logvar2)[source]

Compute the KL divergence between two gaussians.

Shapes are automatically broadcasted, so batches can be compared to scalars, among other use cases.

recbole.model.general_recommender.diffrec.timestep_embedding(timesteps, dim, max_period=10000)[source]

Create sinusoidal timestep embeddings.

Parameters
  • timesteps – a 1-D Tensor of N indices, one per batch element. These may be fractional. (N,)

  • dim – the dimension of the output.

  • max_period – controls the minimum frequency of the embeddings.

Returns

an [N x dim] Tensor of positional embeddings.