# DeepFM: A Factorization-Machine based Neural Network for CTR Prediction 2017

## DeepFM

* [返回顶层目录](https://github.com/luweikxy/machine-learning-notes/tree/2eeeade100d47dc40a47fceee191977ef627aa8a/SUMMARY.md)
* [返回上层目录](/machine-learning-notes/advanced-knowledge/deep-learning.md)
* [DeepFM架构](https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/pages/-M5AUkoU5gI_qbj_wEa9#DeepFM架构)
* [DeepFM原理](https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/pages/-M5AUkoU5gI_qbj_wEa9#DeepFM原理)
  * [FM部分](https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/pages/-M5AUkoU5gI_qbj_wEa9#FM部分)
  * [Deep部分](https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/pages/-M5AUkoU5gI_qbj_wEa9#Deep部分)
  * [特征的Embedding层](https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/pages/-M5AUkoU5gI_qbj_wEa9#特征的Embedding层)
* [基于双塔架构的DeepFM用于召回](https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/pages/-M5AUkoU5gI_qbj_wEa9#基于双塔架构的DeepFM用于召回)

PDF: [*DeepFM: A Factorization-Machine based Neural Network for CTR Prediction*](https://arxiv.org/abs/1703.04247)

![deepfm](/files/-M5AUm6jnR3TGAf77IQU)

类似于Wide\&Deep，华为诺亚方舟实验室提出了DeepFM模型，它的特点是：

* 不需要预训练，直接端到端训练
* 同时学习低阶和高阶的特征交叉
* Wide部分和Deep部分共享特征的Embedding

## DeepFM架构

![deepfm-architecture](/files/-M5AUm6sAV_lK0SRbsXJ)

显然，Wide侧是FM，Deep侧就是一个多层神经网络。而且Wide侧和Deep侧共享了底层的特征Embedding。

DeepFM同时考虑了低阶（FM）和高阶（Deep）特征交叉，和单独各自作为模型相比，这样做会带来额外的收益。

Wide侧采用FM避免了特征工程，因为它能自动进行特征交叉。

## DeepFM原理

设训练数据为n个$$(x,y)$$样本，其中，$$x=\[x\_{field\_1}, x\_{field\_2}, ... x\_{field\_j}, ... , x\_{field\_m}]$$，$$y\in {0, 1}$$。

CTR预测模型为$$\hat{y}=CTR\_model(x)$$。

就和FM一样，对于每一个特征，都会有一个一阶权值$$w\_i$$和一个二阶特征交叉的隐向量（Embedding）$$V\_i$$，而且$$V\_i$$还会被Deep模型共享用来进行高阶特征交叉。

所有的参数，包括$$w\_i$$，$$V\_i$$，和深度网络参数$$(W^{(l)},\ b^{(l)})$$，会按照如下的模型进行训练：

$$
\hat{y}=sigmoid(y\_{FM}+y\_{DNN})
$$

其中，$$y\_{FM}$$为FM部分的输出值，$$y\_{DNN}$$为Deep部分的输出值。

### FM部分

![deepfm-fm](/files/-M5AUm70t6LrOFhYOMqf)

模型为：

$$
y\_{FM}=w\_0+\sum\_{i=1}^dw\_ix\_i+\sum\_{j\_1=1}^d\sum\_{j\_2=j\_1+1}^d\<V\_i,V\_j>x\_{j\_1}\cdot x\_{j\_2}
$$

### Deep部分

![deepfm-deep](/files/-M5AUm7SMAGlkQ_DD7Br)

Deep部分就是一个简单的前馈神经网络，用于高阶特征交叉。

### 特征的Embedding层

FM部分和Deep部分共享了特征的Embedding层，该层具体结构如下：

![deepfm-embedding](/files/-M5AUm7arhfZQ5WLAHEx)

特征的Embedding其实就是OneHot对应的神经网络权重，这一点和Word2Vec一样。

## 基于双塔架构的DeepFM用于召回

DeepFM的结构只适合用于排序，那怎么用于召回呢？

其实，我们联想到FM用于召回的方法，即分别将User和Item各自所有特征的Embedding相加，得到表征User和Item的两个向量，即

$$
\begin{aligned}
\text{Vec}*U&=\sum*{i=1}^nVec\_{U\_i}\\
\text{Vec}*I&=\sum*{i=1}^nVec\_{I\_i}
\end{aligned}
$$

则FM的预测值为

$$
y\_{FM}=\sum\_{i=1}^nVec\_{U\_i}\cdot \sum\_{i=1}^nVec\_{I\_i}=\text{Vec}\_U\cdot \text{Vec}\_I
$$

当然上述只取了FM原始公式中的User和Item的二阶交叉项，是原始FM的一种近似。

然后我们发现，如果把DeepFM中的FM部分改为上述形式，就可以使用双塔架构来构建用于召回的DeepFM了。

为什么要用双塔架构呢，除了这样可以使用Deep外，另一个原因是，其负采样方式特别适合召回。

具体的架构如下所示：

![deepfm-dssm](/files/-M5AUm7lKWgDN6F-5Abq)

## 参考资料

\===

[CTR预估算法之FM, FFM, DeepFM及实践](https://blog.csdn.net/John_xyz/article/details/78933253)

[FFM及DeepFFM模型在推荐系统的探索](http://axure.pmskill.net/AI/%E6%96%B0%E6%B5%AA-ffm%E5%8F%8Adeepffm%E6%A8%A1%E5%9E%8B%E5%9C%A8%E6%8E%A8%E8%8D%90%E7%B3%BB%E7%BB%9F%E7%9A%84%E6%8E%A2%E7%B4%A2%E5%8F%8A%E5%AE%9E%E8%B7%B5.pdf)

[翻译：DeepFM：基于因子分解 -机器的CTR预测神经网络](https://www.kdocs.cn/l/sY7EQM541)

[【通俗易懂】手把手带你实现DeepFM！](https://mp.weixin.qq.com/s/QrO48ZdP483TY_EnnWFhsQ)

[推荐系统遇上深度学习(三)--DeepFM模型理论和实践](https://www.jianshu.com/p/6f1c2643d31b)

[推荐算法——从FM到XDeepFM](https://zhuanlan.zhihu.com/p/81937617)

[\[带你撸论文\]之Deep FM算法代码级精讲](https://zhuanlan.zhihu.com/p/109901389)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://luweikxy.gitbook.io/machine-learning-notes/advanced-knowledge/deep-learning/deepfm-a-factorization-machine-based-neural-network-for-ctr-prediction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
