推荐系统之BRP模型获取及初始化

推荐系统之BRP模型获取及初始化,第1张

1:模型获取
BPR 是一个基本的矩阵分解模型,以成对的方式进行训练。

model = get_model(config['model'])(config, train_data).to(config['device'])

2:拼接模型路径

module_path = '.'.join(['rec.model', submodule, model_file_name])

获取的模型路径
rec.model.general_recommender.bpr

拼接模型

 if importlib.util.find_spec(module_path, __name__):
    model_module = importlib.import_module(module_path, __name__)

拼接的模型

3:获取类
通过反射机制获取类

 model_class = getattr(model_module, model_name)

反射机制没有初始化该类

4:BRP模型初始化
用Torch的 nn.Embedding模块初始化用户矩阵和商品矩阵

  self.user_embedding = nn.Embedding(self.n_users, self.embedding_size)
  self.item_embedding = nn.Embedding(self.n_items, self.embedding_size)

初始化成 Embedding(944, 64)

5:BRP
BPRLoss:基于贝叶斯个性化排名

Examples:
https://blog.csdn.net/sigmeta/article/details/80517828?utm_source=blogxgwz8
>>> loss = BPRLoss()
>>> pos_score = torch.randn(3, requires_grad=True)
>>> neg_score = torch.randn(3, requires_grad=True)
>>> output = loss(pos_score, neg_score)
>>> output.backward()
BPRLoss损失的目标是让正样本的分数远远大于负样本的分数

  def forward(self, pos_score, neg_score):
        loss = -torch.log(self.gamma + torch.sigmoid(pos_score - neg_score)).mean()
        return loss

调用torch中的apply()模块进行参数初始化
初始化商品矩阵,用户矩阵,和BPRLoss。

self.apply(xavier_normal_initialization)

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/943717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存