Skip to content

Commit 718a7f3

Browse files
author
TensorFlow Recommenders Authors
committed
Enable tfrs models to handle non-scalar regularization losses by applying reduce_sum instead of sum on regularization losses.
PiperOrigin-RevId: 504961825
1 parent 5557f09 commit 718a7f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+54
-49
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/examples/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/examples/movielens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/examples/nbtool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/layers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/layers/embedding/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/layers/embedding/partial_tpu_embedding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/layers/embedding/partial_tpu_embedding_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/models/ranking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/models/ranking_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/optimizers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/optimizers/clippy_adagrad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/optimizers/clippy_adagrad_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/optimizers/composite_optimizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/experimental/optimizers/composite_optimizer_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/embedding/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/embedding/tpu_embedding_layer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/embedding/tpu_embedding_layer_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/factorized_top_k.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/factorized_top_k_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/feature_interaction/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/feature_interaction/dcn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/feature_interaction/dcn_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/feature_interaction/dot_interaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/feature_interaction/dot_interaction_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/loss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/layers/loss_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/metrics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/metrics/factorized_top_k.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/metrics/factorized_top_k_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/models/base.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -59,7 +59,8 @@ def compute_loss(self, inputs, training: bool = False) -> tf.Tensor:
5959
"""
6060

6161
raise NotImplementedError(
62-
"Implementers must implement the `compute_loss` method.")
62+
"Implementers must implement the `compute_loss` method."
63+
)
6364

6465
def train_step(self, inputs):
6566
"""Custom train step using the `compute_loss` method."""
@@ -68,7 +69,9 @@ def train_step(self, inputs):
6869
loss = self.compute_loss(inputs, training=True)
6970

7071
# Handle regularization losses as well.
71-
regularization_loss = sum(self.losses)
72+
regularization_loss = tf.reduce_sum(
73+
[tf.reduce_sum(loss) for loss in self.losses]
74+
)
7275

7376
total_loss = loss + regularization_loss
7477

@@ -88,7 +91,9 @@ def test_step(self, inputs):
8891
loss = self.compute_loss(inputs, training=False)
8992

9093
# Handle regularization losses as well.
91-
regularization_loss = sum(self.losses)
94+
regularization_loss = tf.reduce_sum(
95+
[tf.reduce_sum(loss) for loss in self.losses]
96+
)
9297

9398
total_loss = loss + regularization_loss
9499

tensorflow_recommenders/models/base_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/tasks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/tasks/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/tasks/ranking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/tasks/ranking_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/tasks/retrieval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/tasks/retrieval_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tensorflow_recommenders/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tools/build_api_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 The TensorFlow Recommenders Authors.
1+
# Copyright 2023 The TensorFlow Recommenders Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)