Update tests under keras.engine to use combinations.

1. Change all test_util.run_all_in_graph_and_eager_modes to combination.
2. Replace import tensorflow.python.keras with explicit module import.
3. Update BUILD file to not rely on the overall Keras target.

PiperOrigin-RevId: 300985437
Change-Id: Icb9e891619814704996649176360d8fee1d7d863
This commit is contained in:
Scott Zhu 2020-03-14 21:38:49 -07:00 committed by TensorFlower Gardener
parent 344c3289c9
commit 75f75fc3bd
4 changed files with 426 additions and 362 deletions

View File

@ -230,8 +230,12 @@ cuda_py_test(
"nomac", # TODO(mihaimaruseac): b/127695564
],
deps = [
":engine",
"//tensorflow/python:client_testlib",
"//tensorflow/python/keras",
"//tensorflow/python:framework_test_lib",
"//tensorflow/python/keras:backend",
"//tensorflow/python/keras:combinations",
"//tensorflow/python/keras/layers:convolutional",
"//third_party/py/numpy",
"@absl_py//absl/testing:parameterized",
],
@ -282,8 +286,31 @@ tf_py_test(
"notsan",
],
deps = [
":engine",
"//tensorflow/python:array_ops",
"//tensorflow/python:client_testlib",
"//tensorflow/python/keras",
"//tensorflow/python:framework_ops",
"//tensorflow/python:framework_test_lib",
"//tensorflow/python:math_ops",
"//tensorflow/python:nn_ops",
"//tensorflow/python:resource_variable_ops",
"//tensorflow/python:sparse_ops",
"//tensorflow/python:state_ops",
"//tensorflow/python:tensor_shape",
"//tensorflow/python:training_lib",
"//tensorflow/python:variables",
"//tensorflow/python/data/ops:dataset_ops",
"//tensorflow/python/eager:context",
"//tensorflow/python/eager:function",
"//tensorflow/python/keras:backend",
"//tensorflow/python/keras:callbacks",
"//tensorflow/python/keras:combinations",
"//tensorflow/python/keras:losses",
"//tensorflow/python/keras:metrics",
"//tensorflow/python/keras:testing_utils",
"//tensorflow/python/keras/layers",
"//tensorflow/python/keras/utils:data_utils",
"//tensorflow/python/keras/utils:np_utils",
"//third_party/py/numpy",
"@absl_py//absl/testing:parameterized",
],
@ -352,8 +379,20 @@ tf_py_test(
"notsan",
],
deps = [
":engine",
"//tensorflow/python:client_testlib",
"//tensorflow/python:util",
"//tensorflow/python/data/ops:dataset_ops",
"//tensorflow/python/data/ops:iterator_ops",
"//tensorflow/python/eager:context",
"//tensorflow/python/keras",
"//tensorflow/python/keras:combinations",
"//tensorflow/python/keras:losses",
"//tensorflow/python/keras:metrics",
"//tensorflow/python/keras:testing_utils",
"//tensorflow/python/keras/layers",
"//tensorflow/python/keras/optimizer_v2",
"//tensorflow/python/keras/utils:data_utils",
"//third_party/py/numpy",
"@absl_py//absl/testing:parameterized",
],

View File

@ -23,14 +23,17 @@ import itertools
from absl.testing import parameterized
import numpy as np
from tensorflow.python import keras
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.data.ops import iterator_ops
from tensorflow.python.eager import context
from tensorflow.python.framework import test_util as tf_test_util
from tensorflow.python.keras import combinations
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras import layers as layers_module
from tensorflow.python.keras import losses
from tensorflow.python.keras import metrics as metrics_module
from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.engine import input_layer
from tensorflow.python.keras.engine import training
from tensorflow.python.keras.engine import training_generator
from tensorflow.python.keras.optimizer_v2 import rmsprop
from tensorflow.python.keras.utils import data_utils
@ -367,11 +370,13 @@ class TestGeneratorMethods(keras_parameterized.TestCase):
yield pack_and_pad(queue)
model = testing_utils.get_model_from_layers([
keras.layers.Embedding(input_dim=len(vocab) + 1, output_dim=4),
keras.layers.SimpleRNN(units=1),
keras.layers.Activation('sigmoid')], input_shape=(None,))
layers_module.Embedding(input_dim=len(vocab) + 1, output_dim=4),
layers_module.SimpleRNN(units=1),
layers_module.Activation('sigmoid')
],
input_shape=(None,))
model.compile(loss=keras.losses.binary_crossentropy, optimizer='sgd')
model.compile(loss=losses.binary_crossentropy, optimizer='sgd')
model.fit(data_gen(), epochs=1, steps_per_epoch=5)
@ -471,16 +476,16 @@ class TestGeneratorMethodsWithSequences(keras_parameterized.TestCase):
def on_epoch_end(self):
self.epochs += 1
inputs = keras.Input(10)
outputs = keras.layers.Dense(1)(inputs)
model = keras.Model(inputs, outputs)
inputs = input_layer.Input(10)
outputs = layers_module.Dense(1)(inputs)
model = training.Model(inputs, outputs)
model.compile('sgd', 'mse')
my_seq = MySequence()
model.fit(my_seq, epochs=2)
self.assertEqual(my_seq.epochs, 2)
@tf_test_util.run_all_in_graph_and_eager_modes
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
class TestConvertToGeneratorLike(test.TestCase, parameterized.TestCase):
simple_inputs = (np.ones((10, 10)), np.ones((10, 1)))
nested_inputs = ((np.ones((10, 10)), np.ones((10, 20))), (np.ones((10, 1)),

View File

@ -18,18 +18,21 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl.testing import parameterized
import numpy as np
from tensorflow.python import keras
from tensorflow.python.framework import test_util
from tensorflow.python.keras import backend as K
from tensorflow.python.keras import combinations
from tensorflow.python.keras.engine import input_layer
from tensorflow.python.keras.engine import training
from tensorflow.python.keras.layers.convolutional import Conv2D
from tensorflow.python.platform import test
class TrainingGPUTest(test.TestCase):
class TrainingGPUTest(test.TestCase, parameterized.TestCase):
@test_util.run_in_graph_and_eager_modes
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
def test_model_with_crossentropy_losses_channels_first(self):
"""Tests use of all crossentropy losses with `channels_first`.
@ -63,8 +66,7 @@ class TrainingGPUTest(test.TestCase):
activation=activation,
kernel_initializer='ones',
bias_initializer='ones')(input_tensor)
simple_model = keras.models.Model(inputs=input_tensor,
outputs=predictions)
simple_model = training.Model(inputs=input_tensor, outputs=predictions)
simple_model.compile(optimizer='rmsprop', loss=loss)
return simple_model
@ -96,7 +98,7 @@ class TrainingGPUTest(test.TestCase):
data = np.moveaxis(data_channels_first, 1, -1)
for index, loss_function in enumerate(losses_to_test):
labels = np.moveaxis(labels_channels_first[index], 1, -1)
inputs = keras.Input(shape=(3, 3, 1))
inputs = input_layer.Input(shape=(3, 3, 1))
model = prepare_simple_model(inputs, loss_function, labels)
loss_channels_last[index] = model.evaluate(x=data, y=labels,
batch_size=1, verbose=0)
@ -107,7 +109,7 @@ class TrainingGPUTest(test.TestCase):
data = data_channels_first
for index, loss_function in enumerate(losses_to_test):
labels = labels_channels_first[index]
inputs = keras.Input(shape=(1, 3, 3))
inputs = input_layer.Input(shape=(1, 3, 3))
model = prepare_simple_model(inputs, loss_function, labels)
loss_channels_first[index] = model.evaluate(x=data, y=labels,
batch_size=1, verbose=0)

File diff suppressed because it is too large Load Diff