Simplify Keras unit tests by removing unnecessary session scopes and introducing a utility function for repeated code.
PiperOrigin-RevId: 209523944
This commit is contained in:
parent
c5f27df3a5
commit
d29759fa53
@ -235,11 +235,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
num_classes=NUM_CLASSES)
|
num_classes=NUM_CLASSES)
|
||||||
y_test = keras.utils.to_categorical(y_test)
|
y_test = keras.utils.to_categorical(y_test)
|
||||||
y_train = keras.utils.to_categorical(y_train)
|
y_train = keras.utils.to_categorical(y_train)
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(
|
num_hidden=NUM_HIDDEN, num_classes=NUM_CLASSES, input_dim=INPUT_DIM)
|
||||||
keras.layers.Dense(
|
|
||||||
NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
|
|
||||||
model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
|
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
optimizer='rmsprop',
|
optimizer='rmsprop',
|
||||||
@ -298,9 +295,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
test_samples=50,
|
test_samples=50,
|
||||||
input_shape=(1,),
|
input_shape=(1,),
|
||||||
num_classes=NUM_CLASSES)
|
num_classes=NUM_CLASSES)
|
||||||
model = keras.models.Sequential((keras.layers.Dense(
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
1, input_dim=1, activation='relu'), keras.layers.Dense(
|
num_hidden=1, num_classes=1, input_dim=1)
|
||||||
1, activation='sigmoid'),))
|
|
||||||
model.compile(
|
model.compile(
|
||||||
optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])
|
optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])
|
||||||
|
|
||||||
@ -334,11 +330,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
num_classes=NUM_CLASSES)
|
num_classes=NUM_CLASSES)
|
||||||
y_test = keras.utils.to_categorical(y_test)
|
y_test = keras.utils.to_categorical(y_test)
|
||||||
y_train = keras.utils.to_categorical(y_train)
|
y_train = keras.utils.to_categorical(y_train)
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(
|
num_hidden=NUM_HIDDEN, num_classes=NUM_CLASSES, input_dim=INPUT_DIM)
|
||||||
keras.layers.Dense(
|
|
||||||
NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
|
|
||||||
model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
|
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
optimizer='sgd',
|
optimizer='sgd',
|
||||||
@ -388,12 +381,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
def make_model():
|
def make_model():
|
||||||
random_seed.set_random_seed(1234)
|
random_seed.set_random_seed(1234)
|
||||||
np.random.seed(1337)
|
np.random.seed(1337)
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(
|
num_hidden=NUM_HIDDEN, num_classes=NUM_CLASSES, input_dim=INPUT_DIM)
|
||||||
keras.layers.Dense(
|
|
||||||
NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
|
|
||||||
model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
|
|
||||||
|
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
optimizer=keras.optimizers.SGD(lr=0.1),
|
optimizer=keras.optimizers.SGD(lr=0.1),
|
||||||
@ -498,12 +487,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
|
|
||||||
def make_model():
|
def make_model():
|
||||||
np.random.seed(1337)
|
np.random.seed(1337)
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(
|
num_hidden=NUM_HIDDEN, num_classes=NUM_CLASSES, input_dim=INPUT_DIM)
|
||||||
keras.layers.Dense(
|
|
||||||
NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
|
|
||||||
model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
|
|
||||||
|
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
optimizer=keras.optimizers.SGD(lr=0.1),
|
optimizer=keras.optimizers.SGD(lr=0.1),
|
||||||
@ -985,9 +970,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
yield x, y
|
yield x, y
|
||||||
|
|
||||||
with self.test_session():
|
with self.test_session():
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(keras.layers.Dense(10, input_dim=100, activation='relu'))
|
num_hidden=10, num_classes=10, input_dim=100)
|
||||||
model.add(keras.layers.Dense(10, activation='softmax'))
|
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
optimizer='sgd',
|
optimizer='sgd',
|
||||||
@ -1083,11 +1067,8 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
y_test = keras.utils.to_categorical(y_test)
|
y_test = keras.utils.to_categorical(y_test)
|
||||||
y_train = keras.utils.to_categorical(y_train)
|
y_train = keras.utils.to_categorical(y_train)
|
||||||
|
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(
|
num_hidden=NUM_HIDDEN, num_classes=NUM_CLASSES, input_dim=INPUT_DIM)
|
||||||
keras.layers.Dense(
|
|
||||||
NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
|
|
||||||
model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
|
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
|
loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
|
||||||
|
|
||||||
@ -1179,40 +1160,36 @@ class KerasCallbacksTest(test.TestCase):
|
|||||||
|
|
||||||
@test_util.run_in_graph_and_eager_modes
|
@test_util.run_in_graph_and_eager_modes
|
||||||
def test_Tensorboard_eager(self):
|
def test_Tensorboard_eager(self):
|
||||||
with self.test_session():
|
temp_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
|
||||||
temp_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
|
self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
|
||||||
self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
|
|
||||||
|
|
||||||
(x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
|
(x_train, y_train), (x_test, y_test) = testing_utils.get_test_data(
|
||||||
train_samples=TRAIN_SAMPLES,
|
train_samples=TRAIN_SAMPLES,
|
||||||
test_samples=TEST_SAMPLES,
|
test_samples=TEST_SAMPLES,
|
||||||
input_shape=(INPUT_DIM,),
|
input_shape=(INPUT_DIM,),
|
||||||
num_classes=NUM_CLASSES)
|
num_classes=NUM_CLASSES)
|
||||||
y_test = keras.utils.to_categorical(y_test)
|
y_test = keras.utils.to_categorical(y_test)
|
||||||
y_train = keras.utils.to_categorical(y_train)
|
y_train = keras.utils.to_categorical(y_train)
|
||||||
|
|
||||||
model = keras.models.Sequential()
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
model.add(
|
num_hidden=NUM_HIDDEN, num_classes=NUM_CLASSES, input_dim=INPUT_DIM)
|
||||||
keras.layers.Dense(
|
model.compile(
|
||||||
NUM_HIDDEN, input_dim=INPUT_DIM, activation='relu'))
|
loss='binary_crossentropy',
|
||||||
model.add(keras.layers.Dense(NUM_CLASSES, activation='softmax'))
|
optimizer=adam.AdamOptimizer(0.01),
|
||||||
model.compile(
|
metrics=['accuracy'])
|
||||||
loss='binary_crossentropy',
|
|
||||||
optimizer=adam.AdamOptimizer(0.01),
|
|
||||||
metrics=['accuracy'])
|
|
||||||
|
|
||||||
cbks = [keras.callbacks.TensorBoard(log_dir=temp_dir)]
|
cbks = [keras.callbacks.TensorBoard(log_dir=temp_dir)]
|
||||||
|
|
||||||
model.fit(
|
model.fit(
|
||||||
x_train,
|
x_train,
|
||||||
y_train,
|
y_train,
|
||||||
batch_size=BATCH_SIZE,
|
batch_size=BATCH_SIZE,
|
||||||
validation_data=(x_test, y_test),
|
validation_data=(x_test, y_test),
|
||||||
callbacks=cbks,
|
callbacks=cbks,
|
||||||
epochs=2,
|
epochs=2,
|
||||||
verbose=0)
|
verbose=0)
|
||||||
|
|
||||||
self.assertTrue(os.path.exists(temp_dir))
|
self.assertTrue(os.path.exists(temp_dir))
|
||||||
|
|
||||||
def test_RemoteMonitorWithJsonPayload(self):
|
def test_RemoteMonitorWithJsonPayload(self):
|
||||||
if requests is None:
|
if requests is None:
|
||||||
|
@ -25,22 +25,12 @@ from tensorflow.python import keras
|
|||||||
from tensorflow.python.data.ops import dataset_ops
|
from tensorflow.python.data.ops import dataset_ops
|
||||||
from tensorflow.python.eager import function
|
from tensorflow.python.eager import function
|
||||||
from tensorflow.python.framework import test_util as tf_test_util
|
from tensorflow.python.framework import test_util as tf_test_util
|
||||||
|
from tensorflow.python.keras import testing_utils
|
||||||
from tensorflow.python.ops import array_ops
|
from tensorflow.python.ops import array_ops
|
||||||
from tensorflow.python.platform import test
|
from tensorflow.python.platform import test
|
||||||
from tensorflow.python.training import rmsprop
|
from tensorflow.python.training import rmsprop
|
||||||
|
|
||||||
|
|
||||||
def _get_small_mlp(num_hidden, num_classes, input_dim=None):
|
|
||||||
model = keras.models.Sequential()
|
|
||||||
if input_dim:
|
|
||||||
model.add(keras.layers.Dense(num_hidden, activation='relu',
|
|
||||||
input_dim=input_dim))
|
|
||||||
else:
|
|
||||||
model.add(keras.layers.Dense(num_hidden, activation='relu'))
|
|
||||||
model.add(keras.layers.Dense(num_classes, activation='softmax'))
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
class TestSequential(test.TestCase, parameterized.TestCase):
|
class TestSequential(test.TestCase, parameterized.TestCase):
|
||||||
"""Most Sequential model API tests are covered in `training_test.py`.
|
"""Most Sequential model API tests are covered in `training_test.py`.
|
||||||
"""
|
"""
|
||||||
@ -63,7 +53,8 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
batch_size = 5
|
batch_size = 5
|
||||||
num_classes = 2
|
num_classes = 2
|
||||||
|
|
||||||
model = _get_small_mlp(num_hidden, num_classes, input_dim)
|
model = testing_utils.get_small_sequential_mlp(
|
||||||
|
num_hidden, num_classes, input_dim)
|
||||||
model.compile(loss='mse', optimizer=rmsprop.RMSPropOptimizer(1e-3))
|
model.compile(loss='mse', optimizer=rmsprop.RMSPropOptimizer(1e-3))
|
||||||
x = np.random.random((batch_size, input_dim))
|
x = np.random.random((batch_size, input_dim))
|
||||||
y = np.random.random((batch_size, num_classes))
|
y = np.random.random((batch_size, num_classes))
|
||||||
@ -94,7 +85,7 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
batch_size = 5
|
batch_size = 5
|
||||||
num_classes = 2
|
num_classes = 2
|
||||||
|
|
||||||
model = _get_small_mlp(num_hidden, num_classes)
|
model = testing_utils.get_small_sequential_mlp(num_hidden, num_classes)
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='mse',
|
loss='mse',
|
||||||
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
||||||
@ -118,7 +109,7 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
num_samples = 50
|
num_samples = 50
|
||||||
steps_per_epoch = 10
|
steps_per_epoch = 10
|
||||||
|
|
||||||
model = _get_small_mlp(num_hidden, num_classes)
|
model = testing_utils.get_small_sequential_mlp(num_hidden, num_classes)
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='mse',
|
loss='mse',
|
||||||
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
||||||
@ -145,9 +136,9 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
|
|
||||||
def get_model():
|
def get_model():
|
||||||
if deferred:
|
if deferred:
|
||||||
model = _get_small_mlp(10, 4)
|
model = testing_utils.get_small_sequential_mlp(10, 4)
|
||||||
else:
|
else:
|
||||||
model = _get_small_mlp(10, 4, input_dim=3)
|
model = testing_utils.get_small_sequential_mlp(10, 4, input_dim=3)
|
||||||
model.compile(
|
model.compile(
|
||||||
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
||||||
loss='categorical_crossentropy',
|
loss='categorical_crossentropy',
|
||||||
@ -262,7 +253,7 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
batch_size = 5
|
batch_size = 5
|
||||||
num_classes = 2
|
num_classes = 2
|
||||||
|
|
||||||
model = _get_small_mlp(num_hidden, num_classes)
|
model = testing_utils.get_small_sequential_mlp(num_hidden, num_classes)
|
||||||
model.compile(
|
model.compile(
|
||||||
loss='mse',
|
loss='mse',
|
||||||
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
optimizer=rmsprop.RMSPropOptimizer(1e-3),
|
||||||
@ -284,21 +275,21 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
|
|
||||||
@tf_test_util.run_in_graph_and_eager_modes
|
@tf_test_util.run_in_graph_and_eager_modes
|
||||||
def test_sequential_shape_inference_deferred(self):
|
def test_sequential_shape_inference_deferred(self):
|
||||||
model = _get_small_mlp(4, 5)
|
model = testing_utils.get_small_sequential_mlp(4, 5)
|
||||||
output_shape = model.compute_output_shape((None, 7))
|
output_shape = model.compute_output_shape((None, 7))
|
||||||
self.assertEqual(tuple(output_shape.as_list()), (None, 5))
|
self.assertEqual(tuple(output_shape.as_list()), (None, 5))
|
||||||
|
|
||||||
@tf_test_util.run_in_graph_and_eager_modes
|
@tf_test_util.run_in_graph_and_eager_modes
|
||||||
def test_sequential_build_deferred(self):
|
def test_sequential_build_deferred(self):
|
||||||
model = _get_small_mlp(4, 5)
|
model = testing_utils.get_small_sequential_mlp(4, 5)
|
||||||
|
|
||||||
model.build((None, 10))
|
model.build((None, 10))
|
||||||
self.assertTrue(model.built)
|
self.assertTrue(model.built)
|
||||||
self.assertEqual(len(model.weights), 4)
|
self.assertEqual(len(model.weights), 4)
|
||||||
|
|
||||||
# Test with nested model
|
# Test with nested model
|
||||||
model = _get_small_mlp(4, 3)
|
model = testing_utils.get_small_sequential_mlp(4, 3)
|
||||||
inner_model = _get_small_mlp(4, 5)
|
inner_model = testing_utils.get_small_sequential_mlp(4, 5)
|
||||||
model.add(inner_model)
|
model.add(inner_model)
|
||||||
|
|
||||||
model.build((None, 10))
|
model.build((None, 10))
|
||||||
@ -308,8 +299,8 @@ class TestSequential(test.TestCase, parameterized.TestCase):
|
|||||||
|
|
||||||
@tf_test_util.run_in_graph_and_eager_modes
|
@tf_test_util.run_in_graph_and_eager_modes
|
||||||
def test_sequential_nesting(self):
|
def test_sequential_nesting(self):
|
||||||
model = _get_small_mlp(4, 3)
|
model = testing_utils.get_small_sequential_mlp(4, 3)
|
||||||
inner_model = _get_small_mlp(4, 5)
|
inner_model = testing_utils.get_small_sequential_mlp(4, 5)
|
||||||
model.add(inner_model)
|
model.add(inner_model)
|
||||||
|
|
||||||
model.compile(loss='mse', optimizer=rmsprop.RMSPropOptimizer(1e-3))
|
model.compile(loss='mse', optimizer=rmsprop.RMSPropOptimizer(1e-3))
|
||||||
@ -353,7 +344,7 @@ class TestSequentialEagerIntegration(test.TestCase):
|
|||||||
@tf_test_util.run_in_graph_and_eager_modes
|
@tf_test_util.run_in_graph_and_eager_modes
|
||||||
def test_build_before_fit(self):
|
def test_build_before_fit(self):
|
||||||
# Fix for b/112433577
|
# Fix for b/112433577
|
||||||
model = _get_small_mlp(4, 5)
|
model = testing_utils.get_small_sequential_mlp(4, 5)
|
||||||
model.compile(loss='mse', optimizer=rmsprop.RMSPropOptimizer(1e-3))
|
model.compile(loss='mse', optimizer=rmsprop.RMSPropOptimizer(1e-3))
|
||||||
|
|
||||||
model.build((None, 6))
|
model.build((None, 6))
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -184,3 +184,22 @@ def layer_test(layer_cls, kwargs=None, input_shape=None, input_dtype=None,
|
|||||||
# for further checks in the caller function
|
# for further checks in the caller function
|
||||||
return actual_output
|
return actual_output
|
||||||
|
|
||||||
|
|
||||||
|
def get_small_sequential_mlp(num_hidden, num_classes, input_dim=None):
|
||||||
|
model = keras.models.Sequential()
|
||||||
|
if input_dim:
|
||||||
|
model.add(keras.layers.Dense(num_hidden, activation='relu',
|
||||||
|
input_dim=input_dim))
|
||||||
|
else:
|
||||||
|
model.add(keras.layers.Dense(num_hidden, activation='relu'))
|
||||||
|
activation = 'sigmoid' if num_classes == 1 else 'softmax'
|
||||||
|
model.add(keras.layers.Dense(num_classes, activation=activation))
|
||||||
|
return model
|
||||||
|
|
||||||
|
|
||||||
|
def get_small_functional_mlp(num_hidden, num_classes, input_dim):
|
||||||
|
inputs = keras.Input(shape=(input_dim,))
|
||||||
|
outputs = keras.layers.Dense(num_hidden, activation='relu')(inputs)
|
||||||
|
activation = 'sigmoid' if num_classes == 1 else 'softmax'
|
||||||
|
outputs = keras.layers.Dense(num_classes, activation=activation)(outputs)
|
||||||
|
return keras.Model(inputs, outputs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user