19. Wide & Deep 模型:融合广度与深度的推荐算法

Wide & Deep 模型:融合广度与深度的推荐算法

在推荐系统领域,Wide & Deep 模型是一种广泛应用的深度学习模型,它成功融合了广度和深度的特点,能够同时捕获用户的兴趣多样性和深层次的特征学习。本文将介绍 Wide & Deep 模型的原理,以及通过示例和代码展示如何构建一个基于 Wide & Deep 模型的推荐系统。

1. Wide & Deep 模型概述

Wide & Deep 模型最早由 Google 提出,并在 Google Play 推荐系统中取得了成功。这个模型的核心思想是将推荐任务划分为两部分:广度部分(Wide)和深度部分(Deep)。

1.1 广度部分

广度部分是一种线性模型,它主要用于捕获特征之间的交叉信息。具体来说,广度部分使用广义线性模型(Generalized Linear Model,GLM)来建模,其公式如下:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> y ^ wide = w ⋅ x + b \hat{y}_{\text{wide}} = w \cdot x + b </math>y^wide=w⋅x+b

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> y ^ wide \hat{y}_{\text{wide}} </math>y^wide 是广度部分的输出, <math xmlns="http://www.w3.org/1998/Math/MathML"> w w </math>w 是权重向量, <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x 是输入特征向量, <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b 是偏置项。

1.2 深度部分

深度部分是一个深度神经网络,它用于学习高阶特征的表征。深度部分通过多层神经网络进行特征学习,包括全连接层、激活函数等。其公式可以表示为:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> y ^ deep = f ( f ( f ( . . . f ( x ) ) ) ) \hat{y}_{\text{deep}} = f(f(f(...f(x)))) </math>y^deep=f(f(f(...f(x))))

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> y ^ deep \hat{y}_{\text{deep}} </math>y^deep 是深度部分的输出, <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( ⋅ ) f(\cdot) </math>f(⋅) 表示神经网络的前向传播。

1.3 Wide & Deep 模型整合

Wide & Deep 模型将广度部分和深度部分的输出进行整合,得到最终的推荐结果:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> y ^ = σ ( y ^ wide + y ^ deep ) \hat{y} = \sigma(\hat{y}{\text{wide}} + \hat{y}{\text{deep}}) </math>y^=σ(y^wide+y^deep)

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> y ^ \hat{y} </math>y^ 是最终的推荐结果, <math xmlns="http://www.w3.org/1998/Math/MathML"> σ ( ⋅ ) \sigma(\cdot) </math>σ(⋅) 是激活函数,通常为 sigmoid 函数。

2. 示例与代码实现

以下是一个简化的 Python 代码示例,用于构建一个基于 Wide & Deep 模型的二分类推荐系统:

python 复制代码
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim

# 构建训练数据
num_samples = 1000
num_features = 10
user_features = np.random.randn(num_samples, num_features)
item_features = np.random.randn(num_samples, num_features)
labels = np.random.randint(0, 2, num_samples)  # 0表示不喜欢,1表示喜欢

# 将数据转换为 PyTorch 张量
user_features = torch.FloatTensor(user_features)
item_features = torch.FloatTensor(item_features)
labels = torch.FloatTensor(labels)

# 定义 Wide & Deep 模型
class WideAndDeepModel(nn.Module):
    def __init__(self, num_features):
        super(WideAndDeepModel, self).__init__()
        # 广度部分
        self.wide = nn.Linear(num_features, 1)
        # 深度部分
        self.deep = nn.Sequential(
            nn.Linear(num_features, 32),
            nn.ReLU(),
            nn.Linear(32, 1)
        )
        # 输出层
        self.output_layer = nn.Linear(2, 1)
        
    def forward(self, user, item):
        # 广度部分
        wide_output = self.wide(user * item)
        # 深度部分
        deep_output = self.deep(user * item)
        # 合并广度和深度部分
        combined_output = torch.cat((wide_output, deep_output), dim=1)
        # 最终输出
        prediction = torch.sigmoid(self.output_layer(combined_output))
        return prediction

# 初始化模型和优化器
model = WideAndDeepModel(num_features)
optimizer = optim.Adam(model.parameters(), lr=0.01)

# 训练模型
num_epochs = 100
for epoch in range(num_epochs):
    optimizer.zero_grad()
    predictions = model(user_features, item_features)
    loss = nn.BCELoss()(predictions, labels.view(-1, 1))  # 二分类交叉熵损失
    loss.backward()
    optimizer.step()

# 使用模型进行预测
test_user = torch.FloatTensor(np.random.randn(1, num_features))
test_item = torch.FloatTensor(np.random.randn(1, num_features))
predicted_label = model(test_user, test_item).item()

print("预测标签:", predicted_label)

运行结果可能如下所示(数值仅为示例):

makefile 复制代码
预测标签: 0.7542184591293335

结论

Wide & Deep 模型成功将广度和深度结合,充分发挥了广度部分的特征交叉和深度部分的特征学习能力。通过代码示例,我们可以体验到如何使用 PyTorch 构建一个基于 Wide & Deep 模型的推荐系统。这种方法在广告推荐、电商推荐等领域取得了良好的效果,为提高推荐的准确性和个性化程度提供了有效的工具。

相关推荐
燃于AC之乐34 分钟前
我的算法修炼之路--4 ———我和算法的爱恨情仇
算法·前缀和·贪心算法·背包问题·洛谷
小鸡吃米…5 小时前
机器学习 - K - 中心聚类
人工智能·机器学习·聚类
好奇龙猫6 小时前
【AI学习-comfyUI学习-第三十节-第三十一节-FLUX-SD放大工作流+FLUX图生图工作流-各个部分学习】
人工智能·学习
沈浩(种子思维作者)6 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
minhuan6 小时前
大模型应用:大模型越大越好?模型参数量与效果的边际效益分析.51
人工智能·大模型参数评估·边际效益分析·大模型参数选择
Cherry的跨界思维6 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
MM_MS6 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
ASF1231415sd6 小时前
【基于YOLOv10n-CSP-PTB的大豆花朵检测与识别系统详解】
人工智能·yolo·目标跟踪
独自破碎E6 小时前
【二分法】寻找峰值
算法
mit6.8247 小时前
位运算|拆分贪心
算法