BPR LOSS

bpr loss

BPR LOSS

BPR Loss是用得比较多的一种raking loss。它是基于Bayesian Personalized Ranking。BPR Loss 的思想很简单,就是让正样本和负样本的得分之差尽可能达到最大。具体公式如下:
在这里插入图片描述

### BPR Loss Function 的概念 BPR(Bayesian Personalized Ranking)是一种广泛应用于推荐系统的优化目标函数。其核心思想在于通过最大化正样本相对于负样本的概率来提升模型性能[^2]。 具体而言,BPR的目标是最小化用户对不同物品之间偏好顺序的错误预测概率。假设对于某个用户 \( u \),存在两个物品 \( i \) 和 \( j \),其中 \( i \) 是用户交互过的正样本,而 \( j \) 则是没有交互过的负样本,则期望模型能够学习到 \( f(u,i) > f(u,j) \)[^3]。这里的 \( f(u,x) \) 表示用户 \( u \) 对于物品 \( x \) 的评分或者兴趣程度估计值。 为了实现这一目的,BPR采用了基于梯度下降法的学习策略,并设计了一个专门针对排名问题的损失函数: \[ L_{BPR} = -\sum_{u}\sum_{i \in I_u^{+}}\sum_{j \in I_u^{-}} \ln(\sigma(f(u,i)-f(u,j))) + \lambda ||\Theta||^2 \] 上述公式中的每一项含义如下: - \( L_{BPR} \): 总体损失; - \( \sigma(x)=\frac{1}{1+\exp(-x)} \): Sigmoid激活函数,用来计算两者的相对得分差异转化为概率形式; - \( I_u^{+},I_u^{-} \): 用户\( u \)分别对应的已知喜好项目集合与未接触过可能不喜欢项目的候选集; - \( \lambda ||\Theta||^2 \): 正则化项防止过拟合; 此公式的直观意义即为尽可能拉大每一个正面实例同负面实例之间的差距,从而提高整体排序质量。 ### Python 实现代码示例 以下是利用PyTorch框架的一个简单版本BPR损失函数实现方式: ```python import torch from torch import nn class BPRLoss(nn.Module): def __init__(self, reg_weight=0.01): super(BPRLoss, self).__init__() self.reg_weight = reg_weight def forward(self, pos_scores, neg_scores, user_embeddings=None, item_pos_embeddings=None, item_neg_embeddings=None): """ Args: pos_scores (Tensor): Shape of [batch_size], positive items' scores. neg_scores (Tensor): Shape of [batch_size], negative sampled items' scores. user_embeddings (Tensor), item_pos_embeddings (Tensor), item_neg_embeddings (Tensor): For regularization. Returns: Tensor: bpr loss value. """ loss = -torch.mean(torch.log(torch.sigmoid(pos_scores - neg_scores))) if user_embeddings is not None and item_pos_embeddings is not None and item_neg_embeddings is not None: reg_loss = self.reg_weight * ( torch.norm(user_embeddings)**2 + torch.norm(item_pos_embeddings)**2 + torch.norm(item_neg_embeddings)**2 ) loss += reg_loss return loss ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值