搜广推校招面经四

字节

  • 一、手撕DIN
python 复制代码
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
class DIN(nn.Module):
    def __init__(self, num_users, num_items, embedding_dim, history_size):
        super(DIN, self).__init__()
        
        self.user_embedding = nn.Embedding(num_users, embedding_dim)
        self.item_embedding = nn.Embedding(num_items, embedding_dim)
        self.history_embedding = nn.Embedding(num_items, embedding_dim)
        
        self.attention_layer = nn.Linear(embedding_dim * 2, 1)
        self.fc_layer = nn.Sequential(
            nn.Linear(embedding_dim * 2, 128),
            nn.ReLU(),
            nn.Linear(128, 1)
        )
    def forward(self, user_id, item_id, history_ids):
        user_emb = self.user_embedding(user_id)
        item_emb = self.item_embedding(item_id)
        history_emb = self.history_embedding(history_ids)
        
        # 计算兴趣提取:使用注意力机制加权历史行为嵌入
        # 计算历史行为与目标物品的相似度 (比如使用点积)
        combined_emb = torch.cat((history_emb, item_emb_expanded), dim=-1)  # 拼接历史行为和物品嵌入
        # 计算注意力权重
        attention_weights = F.softmax(self.attention_layer(combined_emb), dim=1)
        weighted_history_emb = torch.sum(attention_weights * history_emb, dim=1)

        fusion_emb = torch.cat((item_emb, weighted_history_emb), dim=-1)
        output = self.fc_layer(fusion_emb)
        return output
model = DIN(num_users, num_items, embedding_dim, history_size)
  • 二、手撕NCE loss
    NCE Loss 是一种用于训练概率模型的损失函数,最初在学习词向量(Word2Vec)时提出。NCE Loss 的核心思想是通过将目标分布与噪声分布进行对比,来简化模型的训练过程,从而避免了传统的 softmax 计算中昂贵的归一化步骤。NCE Loss 适用于训练大规模语言模型和推荐系统等。

    NCE 通过将原始任务转换为二分类任务:对于每个真实的样本,判断它是来自目标分布还是来自噪声分布(负样本)。这样,我们不再需要计算所有词汇的总和,而是只需要根据噪声词和正样本来训练模型。具体来说,NCE 会为每个正样本 w w w和上下文词 c c c生成一组负样本 w 1 , . . . . . w k w_1,.....w_k w1,.....wk (噪声词),然后训练模型区分正样本和负样本。
python 复制代码
import torch
import torch.nn.functional as F
def nce_loss(pos_score, neg_scores, num_neg_samples):
    """
    计算NCE损失
    :param pos_score: 正样本的得分(来自目标分布)
    :param neg_scores: 负样本的得分(来自噪声分布)
    :param num_neg_samples: 负样本数量
    :return: NCE损失
    """
  
    # 正样本的损失,sigmoid对正样本得分
    pos_loss = F.logsigmoid(pos_score)
    
    # 负样本的损失,sigmoid对负样本得分,取负
    neg_loss = F.logsigmoid(-neg_scores).sum(dim=-1)
    
    # 计算总损失
    total_loss = -(pos_loss + neg_loss) / num_neg_samples
    return total_loss.mean()  # 返回平均损失
 
# 假设 pos_score 是正样本得分,neg_scores 是负样本得分(来自噪声分布)
pos_score = torch.randn(batch_size, 1)  # 正样本得分
neg_scores = torch.randn(batch_size, num_neg_samples)  # 负样本得分
num_neg_samples = 5  # 负样本数量
# 计算NCE损失
loss = nce_loss(pos_score, neg_scores, num_neg_samples)
  • 三、手撕AUC
预测为1 预测为0
真实label=1 (正类) True Positive (TP) False Negative (FN)
真实label=0 (负类) False Positive (FP) True Negative (TN)

精确率(Precision) : Precision = T P T P + F P \text{Precision} = \frac{TP}{TP + FP} Precision=TP+FPTP
召回率(Recall) : Recall = T P T P + F N \text{Recall} = \frac{TP}{TP + FN} Recall=TP+FNTP
F1分数(F1-Score) : F1-Score = 2 × Precision × Recall Precision + Recall \text{F1-Score} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} F1-Score=2×Precision+RecallPrecision×Recall
真正率(TPR)/ 召回率 : T P R = T P T P + F N TPR = \frac{TP}{TP + FN} TPR=TP+FNTP
假正率(FPR) : T P R = F P F P + T N TPR = \frac{FP}{FP + TN} TPR=FP+TNFP

通过绘制不同阈值下TPR 和 FPR 就可以得到ROC曲线,AUC就等于ROC曲线和X轴之间的面积。但实际上AUC有一个更为常用的定义:随机从正样本和负样本中各选一个,分类器对于该正样本打分大于该负样本打分的概率。基于计算AUC的代码可以写为

python 复制代码
def cal_auc_1(label, pred):
    numerator = 0    # 分子
    denominator = 0  # 分母
    for i in range(len(label) - 1):
        for j in range(i, len(label)):
            if label[i] != label[j]:
                denominator += 1
                # 统计所有正负样本对中,模型把相对位置排序正确的数量
                r = (label[i] - label[j]) * (pred[i] - pred[j])
                if r > 0:
                    numerator += 1
                elif r == 0:
                    numerator += 0.5
    return numerator / denominator
  • 四、算法题:力扣53题 最大子数组和
    这道题可以直接双循环暴力求解,考试想不起还是可以用用的
python 复制代码
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        ans = nums[0]
        for i in range(len(nums)):
            for j in range(i, len(nums)):
                ans = max(ans, sum(nums[i:j+1]))
        return ans
        
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        size = len(nums)
        if size == 0:
            return 0
        dp = [0 for _ in range(size)]

        dp[0] = nums[0]
        for i in range(1, size):
            if dp[i - 1] >= 0:
                dp[i] = dp[i - 1] + nums[i]
            else:
                dp[i] = nums[i]
        return max(dp)   
相关推荐
小玮看世界17 小时前
[Python]从“脏”数据到优雅实现:一个IoT滑动窗口最大值问题的测试驱动优化实录
linux·前端·python
MC皮蛋侠客17 小时前
SQLAlchemy 系列(十一):从 1.x 到 2.x——渐进迁移与数据访问层治理
数据库·python
Elastic 中国社区官方博客17 小时前
使用 Elasticsearch open inference API 对 OpenAI chat completions 进行支持
大数据·elasticsearch·搜索引擎·全文检索·jenkins
魔镜前的帅比18 小时前
(开源项目)x-claw (设计)
python·ai·rust·开源
谢尔登18 小时前
分享一些我常用的Skill
java·人工智能·python·actionscript
k4m7v2pz19 小时前
macOS 解压 40GB 分卷+中文密码固件镜像的五个深坑与解决方案
python·7-zip·aes加密·踩坑记录·r36s·多卷zip解压
小白学大数据19 小时前
Python 爬虫实战:抓取汽车之家二手车成交价格与里程数据
开发语言·爬虫·python·汽车
一碗白开水一19 小时前
入门实践工程四:基于 PyTorch 的手写数字识别(MNIST 图像分类)|附:环境依赖环境及工程源码
人工智能·pytorch·分类
Jazz_z19 小时前
如何用Python将彩色PDF一键转为黑白(灰度)
java·python·pdf
SEO_juper20 小时前
2026年Schema自动注入实战:用Python批量给1000个页面加上JSON-LD,AI引用率实测提升38%
人工智能·python·json·seo·独立站