包含标签 召回 articles

Batch内负采样

In-batch Negative Sampling code: 1import torch 2import torch.nn as nn 3import torch.nn.functional as F 4 5class RecommenderModel(nn.Module): 6 def __init__(self, user_size, item_size, embedding_dim): 7 super(RecommenderModel, self).__init__() 8 self.user_embedding = nn.Embedding(user_size, embedding_dim) 9 self.item_embedding = nn.Embedding(item_size, embedding_dim) 10 11 def forward(self, user_ids, item_ids): 12 user_embeds = self.user_embedding(user_ids) 13 item_embeds = self.item_embedding(item_ids) 14 return user_embeds, item_embeds 15 16 def in_batch_negative_sampling_loss(user_embeds, item_embeds): 17 batch_size = user_embeds.size(0) 18 19 # 正样本得分 (batch_size,) 20 positive_scores = torch.sum(user_embeds * item_embeds, dim=-1) 21 22 # 负样本得分 (batch_size, batch_size) 23 negative_scores = torch.matmul(user_embeds, item_embeds.t()) 24 25 # 创建标签 (batch_size, batch_size) 26 labels = torch.eye(batch_size).to(user_embeds.device) 27 28 # 计算损失 29 loss = F.cross_entropy(negative_scores, labels.argmax(dim=-1)) 30 31 return loss 32 33# 示例数据 34batch_size = 4 35embedding_dim = 8 36user_size = 100 37item_size = 1000 38 39user_ids = torch.randint(0, user_size, (batch_size,)) 40item_ids = torch.randint(0, item_size, (batch_size,)) 41 42model = RecommenderModel(user_size, item_size, embedding_dim) 43user_embeds, item_embeds = model(user_ids, item_ids) 44 45loss = in_batch_negative_sampling_loss(user_embeds, item_embeds) 46print(f'Loss: {loss.item()}') 优点 效性:批量内负采样能够充分利用每个训练批次中的样本,提高……

阅读全文

召回模型的评估

召回模型评测指标 为什么不用AUC指标 AUC指标不适用于衡量召回模型。原因有三: 计算AUC时,正样本容易获得,可以拿点击样本做正样本。但负样本从哪里来?照搬精排,用曝光未点击做负样本,行不行?不行。否则,测试样本都来自曝光物料,也就是从系统筛选过的、比较匹配用户爱好的优质物料,这样的测试数据明显与召回的实际应用场景(海量的、和用户毫不相关的物料)有着天壤之别。失真的测试环境只能产生失真的指标,不能反……

阅读全文

多兴趣召回推荐

传统双塔 user_tower: 产生$V_u$ item_tower: 产生$V_i$ $$ score = f(u\_emb, i\_emb)= <V_u, V_i> $$ 多兴趣双塔 MIND 1interest_capsules = CapsuleLayer(input_units=user_seq_embedding.shape[-1], 2 out_units=params['embedding_dim'], 3 max_len=params['max_seq_len'], 4 k_max=params['k_max'], 5 mode=mode)((user_seq_embedding, like_user_seq_len)) # [B, k_max, embedding_dim] 6 7q_embedding_layer = tf.tile(tf.expand_dims(q_embedding, -2), [1, params['k_max'], 1]) # [B, k_max, 64] 8 9q_deep_input = tf.concat([q_embedding_layer, interest_capsules], axis=-1) # [B, k_max, embedding_dim+64] $Dynamic \ \ Routing$: $Loss$: $$ \begin{aligned} \overrightarrow{\boldsymbol{\upsilon}}_{u} &=\mathrm{Attention}\left(\overrightarrow{\boldsymbol{e}}_{i},\mathrm{V}_{u},\mathrm{V}_{u}\right) \\ &=\mathrm{V}_{u}\mathrm{softmax}(\mathrm{pow}(\mathrm{V}_{u}^{\mathrm{T}}\overrightarrow{\boldsymbol{e}}_{i},p)) \end{aligned} $$ 问题 interest之间差异比较小,学到的兴趣接近 改进$squash$函数 $$ \text{squash函数: }a\leftarrow\frac{|a|^2}{1+|a|^2}\frac a{|a|} \\ \text{ 改进的Squash: }a\leftarrow pow\left(\frac{|a|^2}{1+|a|^2},\mathbf{p}\right)\frac a{|a|} \ \ (0 \leq p \leq 1) $$ 或者对学到的K个兴趣向量加正则损失: $$ D_{output}=-\frac{1}{K^2}\sum_{i=1}^K\sum_{j=1}^K\frac{O^i\cdot O^j}{|O^i||O^j|} $$ ComiRec $Dynamic \ \ Routing$提取兴……

阅读全文