• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for Keras regularizers."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21from absl.testing import parameterized
22import numpy as np
23
24from tensorflow.python import keras
25from tensorflow.python.eager import context
26from tensorflow.python.keras import keras_parameterized
27from tensorflow.python.keras import regularizers
28from tensorflow.python.keras import testing_utils
29from tensorflow.python.keras.utils import np_utils
30from tensorflow.python.ops import math_ops
31from tensorflow.python.platform import test
32
33
34DATA_DIM = 5
35NUM_CLASSES = 2
36
37
38class KerasRegularizersTest(keras_parameterized.TestCase,
39                            parameterized.TestCase):
40
41  def create_model(self, kernel_regularizer=None, activity_regularizer=None):
42    model = keras.models.Sequential()
43    model.add(keras.layers.Dense(NUM_CLASSES,
44                                 kernel_regularizer=kernel_regularizer,
45                                 activity_regularizer=activity_regularizer,
46                                 input_shape=(DATA_DIM,)))
47    return model
48
49  def get_data(self):
50    (x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
51        train_samples=10,
52        test_samples=10,
53        input_shape=(DATA_DIM,),
54        num_classes=NUM_CLASSES)
55    y_train = np_utils.to_categorical(y_train, NUM_CLASSES)
56    y_test = np_utils.to_categorical(y_test, NUM_CLASSES)
57    return (x_train, y_train), (x_test, y_test)
58
59  def create_multi_input_model_from(self, layer1, layer2):
60    input_1 = keras.layers.Input(shape=(DATA_DIM,))
61    input_2 = keras.layers.Input(shape=(DATA_DIM,))
62    out1 = layer1(input_1)
63    out2 = layer2(input_2)
64    out = keras.layers.Average()([out1, out2])
65    model = keras.models.Model([input_1, input_2], out)
66    model.add_loss(keras.backend.mean(out2))
67    model.add_loss(math_ops.reduce_sum(input_1))
68    return model
69
70  @keras_parameterized.run_all_keras_modes
71  @parameterized.named_parameters([
72      ('l1', regularizers.l1()),
73      ('l2', regularizers.l2()),
74      ('l1_l2', regularizers.l1_l2()),
75  ])
76  def test_kernel_regularization(self, regularizer):
77    (x_train, y_train), _ = self.get_data()
78    model = self.create_model(kernel_regularizer=regularizer)
79    model.compile(
80        loss='categorical_crossentropy',
81        optimizer='sgd',
82        run_eagerly=testing_utils.should_run_eagerly(),
83        experimental_run_tf_function=testing_utils.should_run_tf_function())
84    self.assertEqual(len(model.losses), 1)
85    model.fit(x_train, y_train, batch_size=10, epochs=1, verbose=0)
86
87  @keras_parameterized.run_all_keras_modes
88  @parameterized.named_parameters([
89      ('l1', regularizers.l1()),
90      ('l2', regularizers.l2()),
91      ('l1_l2', regularizers.l1_l2()),
92      ('l2_zero', keras.regularizers.l2(0.)),
93  ])
94  def test_activity_regularization(self, regularizer):
95    (x_train, y_train), _ = self.get_data()
96    model = self.create_model(activity_regularizer=regularizer)
97    model.compile(
98        loss='categorical_crossentropy',
99        optimizer='sgd',
100        run_eagerly=testing_utils.should_run_eagerly(),
101        experimental_run_tf_function=testing_utils.should_run_tf_function())
102    self.assertEqual(len(model.losses), 1 if context.executing_eagerly() else 1)
103    model.fit(x_train, y_train, batch_size=10, epochs=1, verbose=0)
104
105  @keras_parameterized.run_all_keras_modes
106  @keras_parameterized.run_with_all_model_types
107  def test_zero_regularization(self):
108    # Verifies that training with zero regularization works.
109    x, y = np.ones((10, 10)), np.ones((10, 3))
110    model = testing_utils.get_model_from_layers(
111        [keras.layers.Dense(3, kernel_regularizer=keras.regularizers.l2(0))],
112        input_shape=(10,))
113    model.compile(
114        'sgd',
115        'mse',
116        run_eagerly=testing_utils.should_run_eagerly(),
117        experimental_run_tf_function=testing_utils.should_run_tf_function())
118    model.fit(x, y, batch_size=5, epochs=1)
119
120  def test_custom_regularizer_saving(self):
121
122    def my_regularizer(weights):
123      return math_ops.reduce_sum(math_ops.abs(weights))
124
125    inputs = keras.Input((10,))
126    outputs = keras.layers.Dense(1, kernel_regularizer=my_regularizer)(inputs)
127    model = keras.Model(inputs, outputs)
128    model2 = model.from_config(
129        model.get_config(), custom_objects={'my_regularizer': my_regularizer})
130    self.assertEqual(model2.layers[1].kernel_regularizer, my_regularizer)
131
132  @keras_parameterized.run_all_keras_modes
133  @parameterized.named_parameters([
134      ('l1', regularizers.l1()),
135      ('l2', regularizers.l2()),
136      ('l1_l2', regularizers.l1_l2()),
137  ])
138  def test_regularization_shared_layer(self, regularizer):
139    dense_layer = keras.layers.Dense(
140        NUM_CLASSES,
141        kernel_regularizer=regularizer,
142        activity_regularizer=regularizer)
143    model = self.create_multi_input_model_from(dense_layer, dense_layer)
144    model.compile(
145        loss='categorical_crossentropy',
146        optimizer='sgd',
147        run_eagerly=testing_utils.should_run_eagerly(),
148        experimental_run_tf_function=testing_utils.should_run_tf_function())
149    self.assertLen(model.losses, 5)
150
151  @keras_parameterized.run_all_keras_modes
152  @parameterized.named_parameters([
153      ('l1', regularizers.l1()),
154      ('l2', regularizers.l2()),
155      ('l1_l2', regularizers.l1_l2()),
156  ])
157  def test_regularization_shared_model(self, regularizer):
158    dense_layer = keras.layers.Dense(
159        NUM_CLASSES,
160        kernel_regularizer=regularizer,
161        activity_regularizer=regularizer)
162
163    input_tensor = keras.layers.Input(shape=(DATA_DIM,))
164    dummy_model = keras.models.Model(input_tensor, dense_layer(input_tensor))
165
166    model = self.create_multi_input_model_from(dummy_model, dummy_model)
167    model.compile(
168        loss='categorical_crossentropy',
169        optimizer='sgd',
170        run_eagerly=testing_utils.should_run_eagerly(),
171        experimental_run_tf_function=testing_utils.should_run_tf_function())
172    self.assertLen(model.losses, 6)
173
174  @keras_parameterized.run_all_keras_modes
175  @parameterized.named_parameters([
176      ('l1', regularizers.l1()),
177      ('l2', regularizers.l2()),
178      ('l1_l2', regularizers.l1_l2()),
179  ])
180  def test_regularization_shared_layer_in_different_models(self, regularizer):
181    shared_dense = keras.layers.Dense(
182        NUM_CLASSES,
183        kernel_regularizer=regularizer,
184        activity_regularizer=regularizer)
185    models = []
186    for _ in range(2):
187      input_tensor = keras.layers.Input(shape=(DATA_DIM,))
188      unshared_dense = keras.layers.Dense(
189          NUM_CLASSES, kernel_regularizer=regularizer)
190      out = unshared_dense(shared_dense(input_tensor))
191      models.append(keras.models.Model(input_tensor, out))
192
193    model = self.create_multi_input_model_from(
194        layer1=models[0], layer2=models[1])
195    model.compile(
196        loss='categorical_crossentropy',
197        optimizer='sgd',
198        run_eagerly=testing_utils.should_run_eagerly(),
199        experimental_run_tf_function=testing_utils.should_run_tf_function())
200
201    # We expect to see 9 losses on the model:
202    # - 2 from the 2 add_loss calls on the outer model.
203    # - 3 from the weight regularizers on the shared_dense layer, unshared_dense
204    # in inner model 1, unshared_dense in inner model 2.
205    # - 4 from activity regularizers on the shared_dense layer.
206    self.assertLen(model.losses, 9)
207
208
209if __name__ == '__main__':
210  test.main()
211