Skip to content

Commit

Permalink
add multi layer DCN as a building block for DCN V2 (DLRM_DCN) model a…
Browse files Browse the repository at this point in the history
…nd add the option to not concat dense before top stack

PiperOrigin-RevId: 601465868
  • Loading branch information
ZhaoyueCheng authored and TensorFlow Recommenders Authors committed Feb 16, 2024
1 parent 1e45dd4 commit 5f0a39f
Show file tree
Hide file tree
Showing 49 changed files with 351 additions and 58 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/examples/movielens.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/examples/nbtool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/experimental/layers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/experimental/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
39 changes: 28 additions & 11 deletions tensorflow_recommenders/experimental/models/ranking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,7 @@ def __init__(
bottom_stack: Optional[tf.keras.layers.Layer] = None,
feature_interaction: Optional[tf.keras.layers.Layer] = None,
top_stack: Optional[tf.keras.layers.Layer] = None,
concat_dense: bool = True,
task: Optional[tasks.Task] = None) -> None:
"""Initializes the model.
Expand All @@ -80,20 +81,32 @@ def __init__(
top_stack: The `top_stack` layer is applied to the `feature_interaction`
output. The output of top_stack should be in the range [0, 1]. If it is
None, MLP with layer sizes [512, 256, 1] is used.
concat_dense: Weather to concatenate the interaction output with dense
embedding vector again before feeding into the top stack
task: The task which the model should optimize for. Defaults to a
`tfrs.tasks.Ranking` task with a binary cross-entropy loss, suitable
for tasks like click prediction.
`tfrs.tasks.Ranking` task with a binary cross-entropy loss, suitable for
tasks like click prediction.
"""

super().__init__()

self._embedding_layer = embedding_layer
self._bottom_stack = bottom_stack if bottom_stack else layers.blocks.MLP(
units=[256, 64, 16], final_activation="relu")
self._top_stack = top_stack if top_stack else layers.blocks.MLP(
units=[512, 256, 1], final_activation="sigmoid")
self._feature_interaction = (feature_interaction if feature_interaction
else feature_interaction_lib.DotInteraction())
self._concat_dense = concat_dense
self._bottom_stack = (
bottom_stack
if bottom_stack
else layers.blocks.MLP(units=[256, 64, 16], final_activation="relu")
)
self._top_stack = (
top_stack
if top_stack
else layers.blocks.MLP(units=[512, 256, 1], final_activation="sigmoid")
)
self._feature_interaction = (
feature_interaction
if feature_interaction
else feature_interaction_lib.DotInteraction()
)

if task is not None:
self._task = task
Expand Down Expand Up @@ -211,8 +224,12 @@ def call(self, inputs: Dict[str, tf.Tensor]) -> tf.Tensor:

interaction_args = sparse_embedding_vecs + [dense_embedding_vec]
interaction_output = self._feature_interaction(interaction_args)
feature_interaction_output = tf.concat(
[dense_embedding_vec, interaction_output], axis=1)
if self._concat_dense:
feature_interaction_output = tf.concat(
[dense_embedding_vec, interaction_output], axis=1
)
else:
feature_interaction_output = interaction_output

prediction = self._top_stack(feature_interaction_output)

Expand Down
8 changes: 6 additions & 2 deletions tensorflow_recommenders/experimental/models/ranking_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -127,6 +127,8 @@ class RankingTest(tf.test.TestCase, parameterized.TestCase):
# Top stack.
(lambda: None, lambda: tfrs.layers.blocks.MLP(
units=[40, 20, 1], final_activation="sigmoid")),
# Concat Dense.
(True, False),
# Use weights.
(True, False),
# Size threshold.
Expand All @@ -135,6 +137,7 @@ def test_ranking_model(self,
feature_interaction_layer,
bottom_stack,
top_stack,
concat_dense=True,
use_weights=False,
size_threshold=10):
"""Tests a ranking model."""
Expand All @@ -151,7 +154,8 @@ def test_ranking_model(self,
size_threshold=size_threshold),
bottom_stack=bottom_stack(),
feature_interaction=feature_interaction_layer(),
top_stack=top_stack())
top_stack=top_stack(),
concat_dense=concat_dense)
model.compile(optimizer=optimizer, steps_per_execution=5)

dataset = _generate_synthetic_data(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/layers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/layers/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/layers/embedding/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/layers/factorized_top_k.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_recommenders/layers/factorized_top_k_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,3 +16,4 @@

from tensorflow_recommenders.layers.feature_interaction.dcn import Cross
from tensorflow_recommenders.layers.feature_interaction.dot_interaction import DotInteraction
from tensorflow_recommenders.layers.feature_interaction.multi_layer_dcn import MultiLayerDCN
2 changes: 1 addition & 1 deletion tensorflow_recommenders/layers/feature_interaction/dcn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The TensorFlow Recommenders Authors.
# Copyright 2024 The TensorFlow Recommenders Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 5f0a39f

Please sign in to comment.