|
| 1 | +# Copyright 2021 Alibaba Group Holding Limited. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================= |
| 15 | + |
| 16 | +r'''Layers for ranking model. |
| 17 | +''' |
| 18 | + |
| 19 | +from __future__ import absolute_import |
| 20 | +from __future__ import division |
| 21 | +from __future__ import print_function |
| 22 | + |
| 23 | +import math |
| 24 | +import tensorflow as tf |
| 25 | + |
| 26 | + |
| 27 | +class DotInteract(tf.layers.Layer): |
| 28 | + r'''DLRM: Deep Learning Recommendation Model for Personalization and |
| 29 | + Recommendation Systems. |
| 30 | +
|
| 31 | + See https://github.com/facebookresearch/dlrm for more information. |
| 32 | + ''' |
| 33 | + def call(self, x): |
| 34 | + r'''Call the DLRM dot interact layer. |
| 35 | + ''' |
| 36 | + x2 = tf.matmul(x, x, transpose_b=True) |
| 37 | + x2_dim = x2.shape[-1] |
| 38 | + x2_ones = tf.ones_like(x2) |
| 39 | + x2_mask = tf.linalg.band_part(x2_ones, 0, -1) |
| 40 | + y = tf.boolean_mask(x2, x2_ones - x2_mask) |
| 41 | + y = tf.reshape(y, [-1, x2_dim * (x2_dim - 1) // 2]) |
| 42 | + return y |
| 43 | + |
| 44 | + |
| 45 | +class Cross(tf.layers.Layer): |
| 46 | + r'''DCN V2: Improved Deep & Cross Network and Practical Lessons for Web-scale |
| 47 | + Learning to Rank Systems. |
| 48 | +
|
| 49 | + See https://arxiv.org/abs/2008.13535 for more information. |
| 50 | + ''' |
| 51 | + def call(self, x): |
| 52 | + r'''Call the DCN cross layer. |
| 53 | + ''' |
| 54 | + x2 = tf.layers.dense( |
| 55 | + x, x.shape[-1], |
| 56 | + activation=tf.nn.relu, |
| 57 | + kernel_initializer=tf.truncated_normal_initializer(), |
| 58 | + bias_initializer=tf.zeros_initializer()) |
| 59 | + y = x * x2 + x |
| 60 | + y = tf.reshape(y, [-1, x.shape[1] * x.shape[2]]) |
| 61 | + return y |
| 62 | + |
| 63 | + |
| 64 | +class Ranking(tf.layers.Layer): |
| 65 | + r'''A simple ranking model. |
| 66 | + ''' |
| 67 | + def __init__( |
| 68 | + self, |
| 69 | + embedding_columns, |
| 70 | + bottom_mlp=None, |
| 71 | + top_mlp=None, |
| 72 | + feature_interaction=None, |
| 73 | + **kwargs): |
| 74 | + r'''Constructor. |
| 75 | +
|
| 76 | + Args: |
| 77 | + embedding_columns: List of embedding columns. |
| 78 | + bottom_mlp: List of bottom MLP dimensions. |
| 79 | + top_mlp: List of top MLP dimensions. |
| 80 | + feature_interaction: Feature interaction layer class. |
| 81 | + **kwargs: keyword named properties. |
| 82 | + ''' |
| 83 | + super().__init__(**kwargs) |
| 84 | + |
| 85 | + if bottom_mlp is None: |
| 86 | + bottom_mlp = [512, 256, 64] |
| 87 | + self.bottom_mlp = bottom_mlp |
| 88 | + if top_mlp is None: |
| 89 | + top_mlp = [1024, 1024, 512, 256, 1] |
| 90 | + self.top_mlp = top_mlp |
| 91 | + if feature_interaction is None: |
| 92 | + feature_interaction = DotInteract |
| 93 | + self.feature_interaction = feature_interaction |
| 94 | + self.embedding_columns = embedding_columns |
| 95 | + dimensions = {c.dimension for c in embedding_columns} |
| 96 | + if len(dimensions) > 1: |
| 97 | + raise ValueError('Only one dimension supported') |
| 98 | + self.dimension = list(dimensions)[0] |
| 99 | + |
| 100 | + def call(self, values, embeddings): |
| 101 | + r'''Call the dlrm model |
| 102 | + ''' |
| 103 | + with tf.name_scope('bottom_mlp'): |
| 104 | + bot_mlp_input = tf.math.log(values + 1.) |
| 105 | + for i, d in enumerate(self.bottom_mlp): |
| 106 | + bot_mlp_input = tf.layers.dense( |
| 107 | + bot_mlp_input, d, |
| 108 | + activation=tf.nn.relu, |
| 109 | + kernel_initializer=tf.glorot_normal_initializer(), |
| 110 | + bias_initializer=tf.random_normal_initializer( |
| 111 | + mean=0.0, |
| 112 | + stddev=math.sqrt(1.0 / d)), |
| 113 | + name=f'bottom_mlp_{i}') |
| 114 | + bot_mlp_output = tf.layers.dense( |
| 115 | + bot_mlp_input, self.dimension, |
| 116 | + activation=tf.nn.relu, |
| 117 | + kernel_initializer=tf.glorot_normal_initializer(), |
| 118 | + bias_initializer=tf.random_normal_initializer( |
| 119 | + mean=0.0, |
| 120 | + stddev=math.sqrt(1.0 / self.dimension)), |
| 121 | + name='bottom_mlp_output') |
| 122 | + |
| 123 | + with tf.name_scope('feature_interaction'): |
| 124 | + feat_interact_input = tf.concat([bot_mlp_output] + embeddings, axis=-1) |
| 125 | + feat_interact_input = tf.reshape( |
| 126 | + feat_interact_input, |
| 127 | + [-1, 1 + len(embeddings), self.dimension]) |
| 128 | + feat_interact_output = self.feature_interaction()(feat_interact_input) |
| 129 | + |
| 130 | + with tf.name_scope('top_mlp'): |
| 131 | + top_mlp_input = tf.concat([bot_mlp_output, feat_interact_output], axis=1) |
| 132 | + num_fields = len(self.embedding_columns) |
| 133 | + prev_d = (num_fields * (num_fields + 1)) / 2 + self.dimension |
| 134 | + for i, d in enumerate(self.top_mlp[:-1]): |
| 135 | + top_mlp_input = tf.layers.dense( |
| 136 | + top_mlp_input, d, |
| 137 | + activation=tf.nn.relu, |
| 138 | + kernel_initializer=tf.random_normal_initializer( |
| 139 | + mean=0.0, |
| 140 | + stddev=math.sqrt(2.0 / (prev_d + d))), |
| 141 | + bias_initializer=tf.random_normal_initializer( |
| 142 | + mean=0.0, |
| 143 | + stddev=math.sqrt(1.0 / d)), |
| 144 | + name=f'top_mlp_{i}') |
| 145 | + prev_d = d |
| 146 | + top_mlp_output = tf.layers.dense( |
| 147 | + top_mlp_input, self.top_mlp[-1], |
| 148 | + activation=tf.nn.sigmoid, |
| 149 | + kernel_initializer=tf.random_normal_initializer( |
| 150 | + mean=0.0, |
| 151 | + stddev=math.sqrt(2.0 / (prev_d + self.top_mlp[-1]))), |
| 152 | + bias_initializer=tf.random_normal_initializer( |
| 153 | + mean=0.0, |
| 154 | + stddev=math.sqrt(1.0 / self.top_mlp[-1])), |
| 155 | + name=f'top_mlp_{len(self.top_mlp) - 1}') |
| 156 | + return top_mlp_output |
0 commit comments