From c475edf9513d6bceae992775869fb9dd0b2c848a Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 24 Sep 2018 05:43:36 +0000 Subject: [PATCH 1/4] Fix for tf.keras.regularizers.{l1,l2}(0.) with tf.get_variable This fix tries to address the issue in 22470 where tf.keras.regularizers.{l1,l2}(l=0.) with tf.get_variable returns ``` AttributeError: 'float' object has no attribute 'name' ``` The issue only happens when `l=0.` as in that case, `regularization = 0.` was returned directly (and `0.` does not have a `name` attribute as a float number) This fix convert regularization = 0. to tensor. This fix fixes 22470. Signed-off-by: Yong Tang --- tensorflow/python/keras/regularizers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow/python/keras/regularizers.py b/tensorflow/python/keras/regularizers.py index 28b6ad4c65a..0d139d748cf 100644 --- a/tensorflow/python/keras/regularizers.py +++ b/tensorflow/python/keras/regularizers.py @@ -20,6 +20,7 @@ from __future__ import print_function import six +from tensorflow.python.framework import ops from tensorflow.python.keras import backend as K from tensorflow.python.keras.utils.generic_utils import deserialize_keras_object from tensorflow.python.keras.utils.generic_utils import serialize_keras_object @@ -54,7 +55,7 @@ class L1L2(Regularizer): self.l2 = K.cast_to_floatx(l2) def __call__(self, x): - regularization = 0. + regularization = ops.convert_to_tensor(0.) if self.l1: regularization += math_ops.reduce_sum(self.l1 * math_ops.abs(x)) if self.l2: From 7f51123b0e8ab0532af04a5ccad3ab9605128db4 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 24 Sep 2018 05:51:09 +0000 Subject: [PATCH 2/4] Add dtype to convert_to_tensor Signed-off-by: Yong Tang --- tensorflow/python/keras/regularizers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/keras/regularizers.py b/tensorflow/python/keras/regularizers.py index 0d139d748cf..fd0748f3ae2 100644 --- a/tensorflow/python/keras/regularizers.py +++ b/tensorflow/python/keras/regularizers.py @@ -55,7 +55,7 @@ class L1L2(Regularizer): self.l2 = K.cast_to_floatx(l2) def __call__(self, x): - regularization = ops.convert_to_tensor(0.) + regularization = ops.convert_to_tensor(0., dtype=K.floatx()) if self.l1: regularization += math_ops.reduce_sum(self.l1 * math_ops.abs(x)) if self.l2: From 7efb78e55c9993068fdc82df3d2df9d989d111e4 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 10 Oct 2018 14:22:32 +0000 Subject: [PATCH 3/4] Add test case for tf.keras.regularizers.{l1,l2}(0.) with tf.get_variable Signed-off-by: Yong Tang --- tensorflow/python/keras/integration_test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tensorflow/python/keras/integration_test.py b/tensorflow/python/keras/integration_test.py index 3c0f73b1c3a..0e6fc79dd36 100644 --- a/tensorflow/python/keras/integration_test.py +++ b/tensorflow/python/keras/integration_test.py @@ -26,6 +26,7 @@ from tensorflow.python.keras import testing_utils from tensorflow.python.layers import core as tf_core_layers from tensorflow.python.ops import nn from tensorflow.python.ops import rnn_cell +from tensorflow.python.ops import variable_scope from tensorflow.python.platform import test @@ -312,6 +313,15 @@ class KerasIntegrationTest(test.TestCase): verbose=0) self.assertGreater(history.history['val_acc'][-1], 0.7) + def test_regularizers_with_get_variable(self): + # Test case for GitHub issue 22470. + with self.cached_session(): + v = variable_scope.get_variable( + "v", + shape = [4, 4], + initializer=keras.initializers.glorot_uniform(), + regularizer=keras.regularizers.l2(0.)) + if __name__ == '__main__': test.main() From 1994b259d998c0a5c73107beb9750ef43b3797c5 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 10 Oct 2018 14:24:13 +0000 Subject: [PATCH 4/4] Update to avoid useless node in the graph based on review Signed-off-by: Yong Tang --- tensorflow/python/keras/integration_test.py | 2 +- tensorflow/python/keras/regularizers.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tensorflow/python/keras/integration_test.py b/tensorflow/python/keras/integration_test.py index 0e6fc79dd36..dfe62276feb 100644 --- a/tensorflow/python/keras/integration_test.py +++ b/tensorflow/python/keras/integration_test.py @@ -318,7 +318,7 @@ class KerasIntegrationTest(test.TestCase): with self.cached_session(): v = variable_scope.get_variable( "v", - shape = [4, 4], + shape=[4, 4], initializer=keras.initializers.glorot_uniform(), regularizer=keras.regularizers.l2(0.)) diff --git a/tensorflow/python/keras/regularizers.py b/tensorflow/python/keras/regularizers.py index fd0748f3ae2..cbcdae214f9 100644 --- a/tensorflow/python/keras/regularizers.py +++ b/tensorflow/python/keras/regularizers.py @@ -55,12 +55,14 @@ class L1L2(Regularizer): self.l2 = K.cast_to_floatx(l2) def __call__(self, x): - regularization = ops.convert_to_tensor(0., dtype=K.floatx()) - if self.l1: - regularization += math_ops.reduce_sum(self.l1 * math_ops.abs(x)) - if self.l2: - regularization += math_ops.reduce_sum(self.l2 * math_ops.square(x)) - return regularization + if self.l1 or self.l2: + regularization = ops.convert_to_tensor(0., dtype=K.floatx()) + if self.l1: + regularization += math_ops.reduce_sum(self.l1 * math_ops.abs(x)) + if self.l2: + regularization += math_ops.reduce_sum(self.l2 * math_ops.square(x)) + return regularization + return None def get_config(self): return {'l1': float(self.l1), 'l2': float(self.l2)}