大廠技術實現 | 騰訊資訊流推薦排序中的並聯雙塔CTR結構 @推薦與計算廣告系列

ShowMeAI發表於2021-11-24

雙塔模型是推薦、搜尋、廣告等多個領域的演算法實現中最常用和經典的結構,實際各公司應用時,雙塔結構中的每個塔會做結構升級,用CTR預估中的新網路結構替代全連線DNN,本期看到的是騰訊瀏覽器團隊的推薦場景下,巧妙並聯CTR模型應用於雙塔的方案。

一圖讀懂全文

實現程式碼

文中涉及到的DCN / FM / DeepFM / FFM / CIN(xDeepFM) 等CTR預估方法實現,請前往 GitHub 進行檢視:https://github.com/ShowMeAI-Hub/ctr-prediction

論文下載 & 資料集下載

相關資料請在公眾號(AI演算法研究所)回覆 『推薦與CTR資料集』 ,獲取下載連結。

CTR預估中的多工多目標建模的大廠技術實現,歡迎檢視同系列相關文章!

一、雙塔模型結構

1.1 模型結構介紹

雙塔模型廣泛應用於推薦、搜尋、廣告等多個領域的召回和排序階段,模型結構如下所示:

雙塔模型結構中,左側是User塔,右側是Item塔,對應的,我們也可以將特徵拆分為兩大類:

  • User相關特徵 :使用者基本資訊、群體統計屬性以及互動過的Item序列等;
  • Item相關特徵 :Item基本資訊、屬性資訊等。

如果有上下文特徵(Context feature)可以放入使用者側塔。最初版本的結構中,這兩個塔中間都是經典的 DNN 模型(即全連線結構),從特徵 Embedding 經過若干層 MLP 隱層,兩個塔分別輸出 User Embedding 和 Item Embedding 編碼。

在訓練過程中,User Embedding 和 Item Embedding 做內積或者Cosine相似度計算,使得當前 User和正例 Item 在 Embedding 空間更接近,和負例 Item 在 Embedding 空間距離拉遠。損失函式則可用標準交叉熵損失(將問題當作一個分類問題),或者採用 BPR 或者 Hinge Loss(將問題當作一個表示學習問題)。

1.2 雙塔模型優缺點

雙塔模型 優點 很明顯:

  • 結構清晰。分別對 User 和 Item 建模學習之後,再互動完成預估
  • 訓練完成之後,線上 inference 過程高效,效能優秀。線上 serving 階段,Item 向量是預先計算好的,可根據變化特徵計算一次 User 向量,再計算內積或者 cosine 即可。

雙塔模型也存在 缺點

  • 原始的雙塔模型結構,特徵受限,無法使用交叉特徵。
  • 模型結構限制下,User 和 Item 是分開構建,只能通過最後的內積來互動,不利於 User-Item 互動的學習。

1.3 雙塔模型的優化

騰訊資訊流團隊(QQ 瀏覽器小說推薦場景) 基於以上限制對雙塔模型結構進行優化,增強模型結構與效果上,取得了不錯的收益,具體做法為:

  • 把雙塔結構中的DNN簡單結構,替換有效CTR模組(MLP、DCN、FM、FFM、CIN)的"並聯"結構,充分利用不同結構的特徵交叉優勢,拓寬模型的"寬度"來緩解雙塔內積的瓶頸;
  • 使用LR學習"並聯"的多個雙塔的權重,LR 權重最終融入到 User Embedding 中,使得最終的模型仍然保持的內積形式。

二、並聯雙塔模型結構

並聯的雙塔模型可以分總分為三層:** 輸入層、表示層和匹配層** 。對應圖中的3個層次,分別的處理和操作如下。

2.1 輸入層(Input Layer)

騰訊QQ瀏覽器小說場景下有以下兩大類特徵:

  • User 特徵 :使用者 id、使用者畫像(年齡、性別、城市)、行為序列(點選、閱讀、收藏)、外部行為(瀏覽器資訊、騰訊視訊等);
  • Item 特徵 :小說內容特徵(小說 id、分類、標籤等)、統計類特徵等;

將 User 和 Item 特徵都經過離散化後對映成 Feature Embedding,方便在表示層進行網路構建。

2.2 表示層(Representation Layer)

  • 對輸入應用深度神經網路CTR模組(MLP、DCN、FM、CIN 等)進行學習,不同的模組可以以不同方式學習輸入層 feature 的融合和互動。
  • 對不同模組學習的表徵,構建並聯結構用於匹配層計算。
  • 表示層的 User-User 和 Item-Item 的特徵互動(塔內資訊交叉)在本塔分支就可以做到,而 User-Item 的特徵互動只能通過上層操作實現。

2.3 匹配層(Matching Layer)

  • 將表示層得到的 User 和 Item 向量,按照不同並聯模型分別進行 hadamard 積,拼接後再經過LR 進行結果融合計算最後score。
  • 線上 serving 階段 LR 的每一維的權重可預先融合到 User Embedding 裡,從而保持線上打分仍然是內積操作。

三、雙塔的表示層結構 -MLP/DCN結構

雙塔內一般都會使用 MLP 結構(多層全連線),騰訊QQ瀏覽器團隊還引入了 DCN 中的 Cross Network 結構用於顯式的構造高階特徵互動,參考的結構是 Google 論文改進版 DCN-Mix。

3.1 DCN 結構

DCN 的特點是引入 Cross Network這種交叉網路結構,提取交叉組合特徵,避免傳統機器學習中的人工手造特徵的過程,網路結構簡單複雜度可控,隨深度增加獲得多階交叉特徵。

DCN模型具體結構如上圖:

  • 底層是 Embedding layer 並對 Embedding 做了stack。
  • 上層是並行的 Cross Network 和 Deep Network。
  • 頭部是 Combination Layer 把 Cross Network 和 Deep Network 的結果 stack 得到 Output。

3.2 優化的DCN-V2結構引入

Google在DCN的基礎上提出改進版 DCN-Mix/DCN-V2,針對 Cross Network 進行了改進,我們主要關注 Cross Network 的計算方式變更:

3.2.1 原始 Cross Network 計算方式

原始計算公式下,經過多層計算,可以顯式地學習到高維的特徵互動,存在的問題是被證明最終的 \(k\) 階互動結果 \(x_{k}\) 等於 \(x_{0}\) 和一個標量的乘積(但不同的\(x_{0}\)這個標量不同,\(x_{0}\)\(x_{k}\) 並不是線性關係),這個計算方式下 Cross Network 的表達受限。

3.2.2 改進版 Cross Network 計算方式

Google改進版的 DCN-Mix 做的處理如下:

  • W 由向量變更為矩陣,更大的引數量帶來了更強的表達能力(實際W 矩陣也可以進行矩陣分解)。
  • 變更特徵互動方式:不再使用外積,應用哈達瑪積(Hadamard product)。

3.2.3 DCN-V2程式碼參考

DCN-v2的程式碼實現和ctr應用案例可以參考 Google官方實現 (https://github.com/tensorflow/models/tree/master/official/recommendation/ranking)

其中核心的改進後的 deep cross layer程式碼如下:

class Cross(tf.keras.layers.Layer):
  """Cross Layer in Deep & Cross Network to learn explicit feature interactions.

    A layer that creates explicit and bounded-degree feature interactions
    efficiently. The `call` method accepts `inputs` as a tuple of size 2
    tensors. The first input `x0` is the base layer that contains the original
    features (usually the embedding layer); the second input `xi` is the output
    of the previous `Cross` layer in the stack, i.e., the i-th `Cross`
    layer. For the first `Cross` layer in the stack, x0 = xi.

    The output is x_{i+1} = x0 .* (W * xi + bias + diag_scale * xi) + xi,
    where .* designates elementwise multiplication, W could be a full-rank
    matrix, or a low-rank matrix U*V to reduce the computational cost, and
    diag_scale increases the diagonal of W to improve training stability (
    especially for the low-rank case).

    References:
        1. [R. Wang et al.](https://arxiv.org/pdf/2008.13535.pdf)
          See Eq. (1) for full-rank and Eq. (2) for low-rank version.
        2. [R. Wang et al.](https://arxiv.org/pdf/1708.05123.pdf)

    Example:

        ```python
        # after embedding layer in a functional model:
        input = tf.keras.Input(shape=(None,), name='index', dtype=tf.int64)
        x0 = tf.keras.layers.Embedding(input_dim=32, output_dim=6)
        x1 = Cross()(x0, x0)
        x2 = Cross()(x0, x1)
        logits = tf.keras.layers.Dense(units=10)(x2)
        model = tf.keras.Model(input, logits)
        ```

    Args:
        projection_dim: project dimension to reduce the computational cost.
          Default is `None` such that a full (`input_dim` by `input_dim`) matrix
          W is used. If enabled, a low-rank matrix W = U*V will be used, where U
          is of size `input_dim` by `projection_dim` and V is of size
          `projection_dim` by `input_dim`. `projection_dim` need to be smaller
          than `input_dim`/2 to improve the model efficiency. In practice, we've
          observed that `projection_dim` = d/4 consistently preserved the
          accuracy of a full-rank version.
        diag_scale: a non-negative float used to increase the diagonal of the
          kernel W by `diag_scale`, that is, W + diag_scale * I, where I is an
          identity matrix.
        use_bias: whether to add a bias term for this layer. If set to False,
          no bias term will be used.
        kernel_initializer: Initializer to use on the kernel matrix.
        bias_initializer: Initializer to use on the bias vector.
        kernel_regularizer: Regularizer to use on the kernel matrix.
        bias_regularizer: Regularizer to use on bias vector.

    Input shape: A tuple of 2 (batch_size, `input_dim`) dimensional inputs.
    Output shape: A single (batch_size, `input_dim`) dimensional output.
  """

  def __init__(
      self,
      projection_dim: Optional[int] = None,
      diag_scale: Optional[float] = 0.0,
      use_bias: bool = True,
      kernel_initializer: Union[
          Text, tf.keras.initializers.Initializer] = "truncated_normal",
      bias_initializer: Union[Text,
                              tf.keras.initializers.Initializer] = "zeros",
      kernel_regularizer: Union[Text, None,
                                tf.keras.regularizers.Regularizer] = None,
      bias_regularizer: Union[Text, None,
                              tf.keras.regularizers.Regularizer] = None,
      **kwargs):

    super(Cross, self).__init__(**kwargs)

    self._projection_dim = projection_dim
    self._diag_scale = diag_scale
    self._use_bias = use_bias
    self._kernel_initializer = tf.keras.initializers.get(kernel_initializer)
    self._bias_initializer = tf.keras.initializers.get(bias_initializer)
    self._kernel_regularizer = tf.keras.regularizers.get(kernel_regularizer)
    self._bias_regularizer = tf.keras.regularizers.get(bias_regularizer)
    self._input_dim = None

    self._supports_masking = True

    if self._diag_scale < 0:
      raise ValueError(
          "`diag_scale` should be non-negative. Got `diag_scale` = {}".format(
              self._diag_scale))

  def build(self, input_shape):
    last_dim = input_shape[-1]

    if self._projection_dim is None:
      self._dense = tf.keras.layers.Dense(
          last_dim,
          kernel_initializer=self._kernel_initializer,
          bias_initializer=self._bias_initializer,
          kernel_regularizer=self._kernel_regularizer,
          bias_regularizer=self._bias_regularizer,
          use_bias=self._use_bias,
      )
    else:
      self._dense_u = tf.keras.layers.Dense(
          self._projection_dim,
          kernel_initializer=self._kernel_initializer,
          kernel_regularizer=self._kernel_regularizer,
          use_bias=False,
      )
      self._dense_v = tf.keras.layers.Dense(
          last_dim,
          kernel_initializer=self._kernel_initializer,
          bias_initializer=self._bias_initializer,
          kernel_regularizer=self._kernel_regularizer,
          bias_regularizer=self._bias_regularizer,
          use_bias=self._use_bias,
      )
    self.built = True

  def call(self, x0: tf.Tensor, x: Optional[tf.Tensor] = None) -> tf.Tensor:
    """Computes the feature cross.

    Args:
      x0: The input tensor
      x: Optional second input tensor. If provided, the layer will compute
        crosses between x0 and x; if not provided, the layer will compute
        crosses between x0 and itself.

    Returns:
     Tensor of crosses.
    """

    if not self.built:
      self.build(x0.shape)

    if x is None:
      x = x0

    if x0.shape[-1] != x.shape[-1]:
      raise ValueError(
          "`x0` and `x` dimension mismatch! Got `x0` dimension {}, and x "
          "dimension {}. This case is not supported yet.".format(
              x0.shape[-1], x.shape[-1]))

    if self._projection_dim is None:
      prod_output = self._dense(x)
    else:
      prod_output = self._dense_v(self._dense_u(x))

    if self._diag_scale:
      prod_output = prod_output + self._diag_scale * x

    return x0 * prod_output + x

  def get_config(self):
    config = {
        "projection_dim":
            self._projection_dim,
        "diag_scale":
            self._diag_scale,
        "use_bias":
            self._use_bias,
        "kernel_initializer":
            tf.keras.initializers.serialize(self._kernel_initializer),
        "bias_initializer":
            tf.keras.initializers.serialize(self._bias_initializer),
        "kernel_regularizer":
            tf.keras.regularizers.serialize(self._kernel_regularizer),
        "bias_regularizer":
            tf.keras.regularizers.serialize(self._bias_regularizer),
    }
    base_config = super(Cross, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))
```

四、雙塔的表示層結構 - FM/FFM/CIN結構

另一類在CTR預估中常用的結構是FM系列的結構,典型的模型包括FM、FFM、DeepFM、xDeepFM,他們特殊的建模方式也能挖掘有效的資訊,騰訊QQ瀏覽器團隊的最終模型上,也使用了上述模型的子結構。

上文提到的MLP和DCN的特徵互動交叉,無法顯式指定某些特徵互動,而FM系列模型中的FM / FFM / CIN結構可以對特徵粒度的互動做顯式操作,且從計算公式上看,它們都具備很好的內積形式,從能方便直接地實現雙塔建模 User-Item 的特徵粒度的互動。

4.1 FM結構引入

\[y = \omega_{0}+\sum_{i=1}^{n} \omega_{i} x_{i}+\sum_{i=1}^{n-1} \sum_{j=i+1}^{n}<v_{i}, v_{j}>x_{i} x_{j} \]

FM是CTR預估中最常見的模型結構,它通過矩陣分解的方法構建特徵的二階互動,計算公式上表現為特徵向量 \(v_{i}\)\(v_{j}\) 的兩兩內積操作再求和(在深度學習裡可以看做特徵Embedding的組對內積),通過內積運算分配率可以轉換成求和再內積的形式。

\[\begin{array}{c} y=\sum_{i} \sum_{j}\left\langle V_{i}, V_{j}\right\rangle=\left\langle\sum_{i} V_{i}, \sum_{j} V_{j}\right\rangle \\ i \in \text { user fea, } \quad j \in \text { item fea } \end{array} \]

在騰訊QQ瀏覽器團隊小說推薦場景中,只考慮 User-Item 的互動(因為User內部或者Item內部的特徵二階互動上文提到的模型已捕捉到),如上公式所示,$i$ 是 User 側的特徵,$j$ 是 Item 側的特徵,通過內積計算分配率的轉換,User-Item 的二階特徵互動也可以轉化為 User、Item 特徵向量先求和(神經網路中體現為sum pooling)再做內積,很方便可以轉為雙塔結構處理。

4.2 FFM結構引入

FFM 模型是 FM 的升級版本,相比 FM,它多了 field 的概念,FFM 把相同性質的特徵歸於同一個field,構建的隱向量不僅與特徵相關,也與field相關,最終的特徵互動可以在不同的隱向量空間,進而提升區分能力加強效果,FFM 也可以通過一些方法轉換成雙塔內積的結構。

\[y(\mathbf{x})=w_{0}+\sum_{i=1}^{n} w_{i} x_{i}+\sum_{i=1}^{n} \sum_{j=i+1}^{n}\left\langle\mathbf{v}_{i f_{j}}, \mathbf{v}_{j f_{i}}\right\rangle x_{i} x_{j} \]

一個轉換的例子如下:

User 有 2 個特徵 field、Item 有 3 個特徵 field,圖中任意2個特徵互動都有獨立的 Embedding 向量。根據 FFM 公式,計算 User-Item 的二階互動,需要將所有的內積計算出來並求和。

我們將User、Item 的特徵 Embedding 做重新排序,再進行拼接,可以把 FFM 也轉換成雙塔內積形式。FFM 內的 User-User 和 Item-Item 都在塔內,所以我們可預先算好放入一階項裡。

騰訊QQ瀏覽器團隊實踐應用中發現:應用 FFM 的雙塔,訓練資料上 AUC 提升明顯,但引數量的增加帶來了嚴重的過擬合,且上述結構調整後雙塔的寬度極寬(可能達到萬級別),對效能效率影響較大,進一步嘗試的優化方式如下:

  • 人工篩選參與 FFM 訓練特徵互動的 User 和 Item 特徵 field,控制雙塔寬度(1000左右)。
  • 調整 FFM 的 Embedding 引數初始化方式(接近 0)及學習率(降低)。

最終效果不是很理想,因此團隊實際線上並未使用 FFM。

4.3 CIN結構引入

前面提到的FM和FFM能完成二階特徵互動,而xDeepFM模型中提出的 CIN 結構可以實現更高階的特徵互動(比如 User-User-Item、User-User-Item-Item、User-Item-Item 等3階),騰訊QQ瀏覽器團隊嘗試了兩種用法把CIN應用在雙塔結構中:

4.3.1 CIN(User) * CIN(Item)

雙塔每個塔內生成 User、Item 的自身多階 CIN 結果,再分別 sum pooling 生成 User/Item 向量,然後User 與 Item 向量內積

根據分配率,我們對 sum pooling 再內積的公式進行拆解,會發現這個計算方式內部其實已經實現了 User-Item 的多階互動:

\[\begin{array}{c} \left(U^{1}+U^{2}+U^{3}\right) *\left(I^{1}+I^{2}+I^{3}\right) \\ U^{1} I^{1}+U^{1} I^{2}+U^{1} I^{3}+U^{2} I^{1}+U^{2} I^{2}+U^{2} I^{3}+U^{3} I^{1}+U^{3} I^{2}+U^{3} I^{3} \end{array} \]

這個用法實現過程也比較簡單,針對雙塔結構,在兩側塔內做 CIN 生成各階結果,再對結果做 sumpooling,最後類似 FM 原理通過內積實現 User-Item 的各階互動。

這個處理方式有一定的缺點:生成的 User-Item 二階及以上的特徵互動,有著和 FM 類似的侷限性(例U1 是由 User 側提供的多個特徵sumpooling所得結果,U1 與 Item 側的結果內積計算,受限於sum pooling的計算,每個 User 特徵在這裡重要度就變成一樣的了)。

4.3.2 CIN( CIN(User) , CIN(Item) )

第2種處理方式是:雙塔每側塔內生成 User、Item 的多階 CIN 結果後,對 User、Item 的 CIN 結果再次兩兩使用 CIN 顯式互動(而非 sum pooling 後計算內積),並轉成雙塔內積,如下圖所示:

下圖為 CIN 計算的公式表示,多個卷積結果做 sum pooling 後形式保持不變(兩兩 hadamard 積加權求和)

CIN 的形式和 FFM 類似,同樣可以通過 『重新排列+拼接』 操作轉換成雙塔內積形式,生成的雙塔寬度也非常大(萬級別),但與 FFM 不同的是:CIN 的所有特徵互動,底層使用的 feature Embedding 是共享的,而 FFM 對每個二階互動都有獨立的 Embedding。因此騰訊QQ瀏覽器團隊的實踐嘗試中基本沒有出現過擬合問題,實驗效果上第②種方式第①種用法略好。

五、騰訊業務效果

以下為騰訊QQ瀏覽器小說推薦業務上的方法實驗效果(對比各種單CTR模型和並聯雙塔結構):

5.1 團隊給出的一些分析如下

  • CIN2 在單結構的雙塔模型中的效果是最好的,其次是 DCN 和 CIN1的雙塔結構;
  • 並聯的雙塔結構相比於單一的雙塔結構在效果上也有明顯提升;
  • 並聯方案二使用了 CIN2 的結構,雙塔寬度達到了 2萬+,對線上 serving 的效能有一定的挑戰,綜合考慮效果和部署效率可以選擇並聯雙塔方案一。

5.2 團隊給出的一些訓練細節和經驗

  • 考慮到FM/FFM/CIN 等結構的計算複雜度,都只在精選特徵子集上面訓練,選取維度更高的 category 特徵為主,比如使用者id、行為歷史id、小說id、標籤id 等,還有少量統計特徵,User 側、Item 側大概各選了不到 20 個特徵field;
  • 並聯的各雙塔結構,各模型不共享底層 feature Embedding,分別訓練自己的 Embedding;
  • feature Embedding 維度選擇,MLP/DCN 對 category 特徵維度為16,非 category特徵維度是32
  • FM/FFM/CIN 的 feature Embedding 維度統一為32。

六、騰訊團隊實驗效果

在小說推薦場景的粗排階段上線了 A/B Test 實驗,實驗組的點選率、閱讀轉化率模型使用了『並聯雙塔方案一』,對照組為 『MLP 雙塔模型』,如下圖所示,有明顯的業務指標提升:

  • 點選率+6.8752%
  • 閱讀轉化率+6.2250%
  • 加書轉化率+6.5775%
  • 閱讀時長+3.3796%

七、相關程式碼實現參考

實現程式碼

文中涉及到的DCN / FM / DeepFM / FFM / CIN(xDeepFM) 等 CTR預估方法實現 ,請前往 GitHub 進行檢視:https://github.com/ShowMeAI-Hub/ctr-prediction

論文下載 & 資料集下載

相關資料請在公眾號(AI演算法研究所)回覆 『推薦與CTR資料集』 ,獲取下載連結。

八、參考文獻

  • [1] Huang, Po-Sen, et al. "Learning deep structured semantic models for web search using clickthrough data." Proceedings of the 22nd ACM international conference on Information & Knowledge Management. 2013.
  • [2] S. Rendle, “Factorization machines,” in Proceedings of IEEE International Conference on Data Mining (ICDM), pp. 995–1000, 2010.
  • [3] Yuchin Juan, et al. "Field-aware Factorization Machines for CTR Prediction." Proceedings of the 10th ACM Conference on Recommender SystemsSeptember 2016 Pages 43–
  • [4] Jianxun Lian, et al. "xDeepFM: Combining Explicit and Implicit Feature Interactions for Recommender Systems" Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery & Data MiningJuly 2018 Pages 1754–1763
  • [5] Ruoxi Wang, et al. "Deep & Cross Network for Ad Click Predictions" Proceedings of the ADKDD'17August 2017 Article No.: 12Pages 1–
  • [6] Wang, Ruoxi, et al. "DCN V2: Improved Deep & Cross Network and Practical Lessons for Webscale Learning to Rank Systems" In Proceedings of the Web Conference 2021 (WWW '21); doi:10.1145/3442381.3450078

資料下載

實現程式碼

文中涉及到的DCN / FM / DeepFM / FFM / CIN(xDeepFM) 等CTR預估方法實現,請前往 GitHub 進行檢視:https://github.com/ShowMeAI-Hub/ctr-prediction

論文下載 & 資料集下載

相關資料請在公眾號(AI演算法研究所)回覆『推薦與CTR資料集』,獲取下載連結。

CTR預估中的多工多目標建模的大廠技術實現,歡迎檢視同系列相關文章

相關文章