From 5a3bb771ae218d6cf35ad1476c0f47f38fa7ad82 Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sat, 13 Mar 2021 17:32:54 +0100 Subject: [PATCH 1/8] do not explicitly inherit from object --- tensorflow/python/keras/losses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/keras/losses.py b/tensorflow/python/keras/losses.py index 3f5844e9ed8766..30e5ce172f96ee 100644 --- a/tensorflow/python/keras/losses.py +++ b/tensorflow/python/keras/losses.py @@ -50,7 +50,7 @@ @keras_export('keras.losses.Loss') -class Loss(object): +class Loss: """Loss base class. To be implemented by subclasses: From 3333dd1d9b8ae472e3f590ca20b2fe096629d38e Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sat, 13 Mar 2021 23:34:06 +0100 Subject: [PATCH 2/8] super calls --- tensorflow/python/keras/losses.py | 40 +++++++++++++------------------ 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/tensorflow/python/keras/losses.py b/tensorflow/python/keras/losses.py index 30e5ce172f96ee..c330e629a64f8f 100644 --- a/tensorflow/python/keras/losses.py +++ b/tensorflow/python/keras/losses.py @@ -241,7 +241,7 @@ def __init__(self, name: (Optional) name for the loss. **kwargs: The keyword arguments that are passed on to `fn`. """ - super(LossFunctionWrapper, self).__init__(reduction=reduction, name=name) + super().__init__(reduction=reduction, name=name) self.fn = fn self._fn_kwargs = kwargs @@ -265,7 +265,7 @@ def get_config(self): config = {} for k, v in six.iteritems(self._fn_kwargs): config[k] = K.eval(v) if tf_utils.is_tensor_or_variable(v) else v - base_config = super(LossFunctionWrapper, self).get_config() + base_config = super().get_config() return dict(list(base_config.items()) + list(config.items())) @@ -324,8 +324,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'mean_squared_error'. """ - super(MeanSquaredError, self).__init__( - mean_squared_error, name=name, reduction=reduction) + super().__init__(mean_squared_error, name=name, reduction=reduction) @keras_export('keras.losses.MeanAbsoluteError') @@ -383,8 +382,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'mean_absolute_error'. """ - super(MeanAbsoluteError, self).__init__( - mean_absolute_error, name=name, reduction=reduction) + super().__init__(mean_absolute_error, name=name, reduction=reduction) @keras_export('keras.losses.MeanAbsolutePercentageError') @@ -444,7 +442,7 @@ def __init__(self, name: Optional name for the op. Defaults to 'mean_absolute_percentage_error'. """ - super(MeanAbsolutePercentageError, self).__init__( + super().__init__( mean_absolute_percentage_error, name=name, reduction=reduction) @@ -505,7 +503,7 @@ def __init__(self, name: Optional name for the op. Defaults to 'mean_squared_logarithmic_error'. """ - super(MeanSquaredLogarithmicError, self).__init__( + super().__init__( mean_squared_logarithmic_error, name=name, reduction=reduction) @@ -600,7 +598,7 @@ def __init__(self, more details. name: (Optional) Name for the op. Defaults to 'binary_crossentropy'. """ - super(BinaryCrossentropy, self).__init__( + super().__init__( binary_crossentropy, name=name, reduction=reduction, @@ -680,7 +678,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'categorical_crossentropy'. """ - super(CategoricalCrossentropy, self).__init__( + super().__init__( categorical_crossentropy, name=name, reduction=reduction, @@ -758,7 +756,7 @@ def __init__(self, name: Optional name for the op. Defaults to 'sparse_categorical_crossentropy'. """ - super(SparseCategoricalCrossentropy, self).__init__( + super().__init__( sparse_categorical_crossentropy, name=name, reduction=reduction, @@ -821,7 +819,7 @@ def __init__(self, reduction=losses_utils.ReductionV2.AUTO, name='hinge'): more details. name: Optional name for the op. Defaults to 'hinge'. """ - super(Hinge, self).__init__(hinge, name=name, reduction=reduction) + super().__init__(hinge, name=name, reduction=reduction) @keras_export('keras.losses.SquaredHinge') @@ -882,8 +880,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'squared_hinge'. """ - super(SquaredHinge, self).__init__( - squared_hinge, name=name, reduction=reduction) + super().__init__(squared_hinge, name=name, reduction=reduction) @keras_export('keras.losses.CategoricalHinge') @@ -942,8 +939,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'categorical_hinge'. """ - super(CategoricalHinge, self).__init__( - categorical_hinge, name=name, reduction=reduction) + super().__init__(categorical_hinge, name=name, reduction=reduction) @keras_export('keras.losses.Poisson') @@ -999,7 +995,7 @@ def __init__(self, reduction=losses_utils.ReductionV2.AUTO, name='poisson'): more details. name: Optional name for the op. Defaults to 'poisson'. """ - super(Poisson, self).__init__(poisson, name=name, reduction=reduction) + super().__init__(poisson, name=name, reduction=reduction) @keras_export('keras.losses.LogCosh') @@ -1056,7 +1052,7 @@ def __init__(self, reduction=losses_utils.ReductionV2.AUTO, name='log_cosh'): more details. name: Optional name for the op. Defaults to 'log_cosh'. """ - super(LogCosh, self).__init__(log_cosh, name=name, reduction=reduction) + super().__init__(log_cosh, name=name, reduction=reduction) @keras_export('keras.losses.KLDivergence') @@ -1116,8 +1112,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'kl_divergence'. """ - super(KLDivergence, self).__init__( - kl_divergence, name=name, reduction=reduction) + super().__init__(kl_divergence, name=name, reduction=reduction) @keras_export('keras.losses.Huber') @@ -1184,8 +1179,7 @@ def __init__(self, more details. name: Optional name for the op. Defaults to 'huber_loss'. """ - super(Huber, self).__init__( - huber, name=name, reduction=reduction, delta=delta) + super().__init__(huber, name=name, reduction=reduction, delta=delta) @keras_export('keras.metrics.mean_squared_error', 'keras.metrics.mse', @@ -1955,7 +1949,7 @@ def __init__(self, axis=-1, reduction=losses_utils.ReductionV2.AUTO, name='cosine_similarity'): - super(CosineSimilarity, self).__init__( + super().__init__( cosine_similarity, reduction=reduction, name=name, axis=axis) From 98dd2fbddbc5048984ccf36890109d7cadff497a Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sun, 14 Mar 2021 20:35:12 +0100 Subject: [PATCH 3/8] remove __future__ imports --- tensorflow/python/keras/losses.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tensorflow/python/keras/losses.py b/tensorflow/python/keras/losses.py index c330e629a64f8f..6d19ce8c20515d 100644 --- a/tensorflow/python/keras/losses.py +++ b/tensorflow/python/keras/losses.py @@ -13,9 +13,6 @@ # limitations under the License. # ============================================================================== """Built-in loss functions.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function import abc import functools From f314cff7c92478cc4d225cee152a9b5f9c052ba9 Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sun, 14 Mar 2021 20:45:03 +0100 Subject: [PATCH 4/8] remove six --- tensorflow/python/keras/losses.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tensorflow/python/keras/losses.py b/tensorflow/python/keras/losses.py index 6d19ce8c20515d..8be1c1cc23ab49 100644 --- a/tensorflow/python/keras/losses.py +++ b/tensorflow/python/keras/losses.py @@ -17,8 +17,6 @@ import abc import functools -import six - from tensorflow.python.autograph.core import ag_ctx from tensorflow.python.autograph.impl import api as autograph from tensorflow.python.distribute import distribution_strategy_context @@ -260,7 +258,7 @@ def call(self, y_true, y_pred): def get_config(self): config = {} - for k, v in six.iteritems(self._fn_kwargs): + for k, v in self._fn_kwargs.items(): config[k] = K.eval(v) if tf_utils.is_tensor_or_variable(v) else v base_config = super().get_config() return dict(list(base_config.items()) + list(config.items())) @@ -2040,8 +2038,7 @@ def get(identifier): """ if identifier is None: return None - if isinstance(identifier, six.string_types): - identifier = str(identifier) + if isinstance(identifier, str): return deserialize(identifier) if isinstance(identifier, dict): return deserialize(identifier) From aaa7cd2d2723fc98c450a43a74c234f11c8264d7 Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sun, 14 Mar 2021 20:46:31 +0100 Subject: [PATCH 5/8] simplify structure of if ... return chain --- tensorflow/python/keras/losses.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tensorflow/python/keras/losses.py b/tensorflow/python/keras/losses.py index 8be1c1cc23ab49..064bf695de2f46 100644 --- a/tensorflow/python/keras/losses.py +++ b/tensorflow/python/keras/losses.py @@ -2038,15 +2038,12 @@ def get(identifier): """ if identifier is None: return None - if isinstance(identifier, str): + if isinstance(identifier, str) or isinstance(identifier, dict): return deserialize(identifier) - if isinstance(identifier, dict): - return deserialize(identifier) - elif callable(identifier): + if callable(identifier): return identifier - else: - raise ValueError( - 'Could not interpret loss function identifier: {}'.format(identifier)) + raise ValueError( + 'Could not interpret loss function identifier: {}'.format(identifier)) LABEL_DTYPES_FOR_LOSSES = { From 389e5b0f75c8a9dc12bef5ab8e2e6d914b38fcdf Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sun, 14 Mar 2021 20:59:29 +0100 Subject: [PATCH 6/8] use f-string --- tensorflow/python/keras/losses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/keras/losses.py b/tensorflow/python/keras/losses.py index 064bf695de2f46..2fe8c77189c3e4 100644 --- a/tensorflow/python/keras/losses.py +++ b/tensorflow/python/keras/losses.py @@ -2043,7 +2043,7 @@ def get(identifier): if callable(identifier): return identifier raise ValueError( - 'Could not interpret loss function identifier: {}'.format(identifier)) + f'Could not interpret loss function identifier: {identifier}') LABEL_DTYPES_FOR_LOSSES = { From 8d43ce47ac8084b3da950ad56941e7d34963d189 Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sun, 14 Mar 2021 21:01:20 +0100 Subject: [PATCH 7/8] remove __future__ imports --- tensorflow/python/keras/losses_test.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tensorflow/python/keras/losses_test.py b/tensorflow/python/keras/losses_test.py index e5a7146d0dd0ac..03716d07d095e0 100644 --- a/tensorflow/python/keras/losses_test.py +++ b/tensorflow/python/keras/losses_test.py @@ -14,10 +14,6 @@ # ============================================================================== """Tests for Keras loss functions.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - from absl.testing import parameterized import numpy as np From 4c13335ce6fc57e86124ec32d5b3623ba9c37106 Mon Sep 17 00:00:00 2001 From: Zeno Gantner Date: Sun, 14 Mar 2021 21:04:42 +0100 Subject: [PATCH 8/8] modernize super() call --- tensorflow/python/keras/losses_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/keras/losses_test.py b/tensorflow/python/keras/losses_test.py index 03716d07d095e0..e5b1e59dd8410f 100644 --- a/tensorflow/python/keras/losses_test.py +++ b/tensorflow/python/keras/losses_test.py @@ -1783,7 +1783,7 @@ def test_loss_with_non_default_dtype(self): class BinaryTruePositivesViaControlFlow(losses.Loss): def __init__(self, reduction=losses_utils.ReductionV2.AUTO): - super(BinaryTruePositivesViaControlFlow, self).__init__(reduction=reduction) + super().__init__(reduction=reduction) def call(self, y_true, y_pred): y_true = math_ops.cast(y_true, dtypes.bool)