From c475edf9513d6bceae992775869fb9dd0b2c848a Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 24 Sep 2018 05:43:36 +0000 Subject: [PATCH] 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: