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 <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2018-09-24 05:43:36 +00:00
parent 646b3c237d
commit c475edf951

View File

@ -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: