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)}