Remove test combination and annotation for keras_tensor.

keras_tensor is turned on by default and already release. We didn't expose any knob to let end user to turn off this behavior. Remove the annotation will reduce the number of tests by default setting.

Leave some test case for use_keras_tensors_scope(False) unchanged since it might trying to assert certain behavior in graph mode.

PiperOrigin-RevId: 353042056
Change-Id: Ifbab8d4b5f71102846bbd0d48372bb85158ad2f1
This commit is contained in:
Scott Zhu 2021-01-21 09:56:54 -08:00 committed by TensorFlower Gardener
parent f48ae3edac
commit e5c5a0aba8
9 changed files with 260 additions and 415 deletions

View File

@ -61,10 +61,6 @@ def keras_model_type_combinations():
return combinations.combine(model_type=KERAS_MODEL_TYPES)
def keras_tensor_combinations():
return combinations.combine(use_keras_tensors=['True', 'False'])
class KerasModeCombination(test_combinations.TestCombination):
"""Combination for Keras test mode.
@ -104,32 +100,11 @@ class KerasModelTypeCombination(test_combinations.TestCombination):
return [test_combinations.OptionalParameter('model_type')]
class KerasTensorCombination(test_combinations.TestCombination):
"""Combination for whether KerasTensors are being used or not.
It by default includes `True` and `False`:
running Keras's functional API with KerasTensors
as the inputs, and without.
"""
def context_managers(self, kwargs):
use_keras_tensors = kwargs.pop('use_keras_tensors', None)
if use_keras_tensors is not None:
return [testing_utils.use_keras_tensors_scope(use_keras_tensors)]
else:
return []
def parameter_modifiers(self):
return [test_combinations.OptionalParameter('use_keras_tensors')]
_defaults = combinations.generate.keywords['test_combinations']
generate = functools.partial(
combinations.generate,
test_combinations=_defaults +
(KerasModeCombination(), KerasModelTypeCombination(),
KerasTensorCombination()))
(KerasModeCombination(), KerasModelTypeCombination()))
combine = test_combinations.combine
times = test_combinations.times
NamedObject = test_combinations.NamedObject

View File

@ -93,9 +93,7 @@ class BaseLayerTest(keras_parameterized.TestCase):
self.assertTrue(layer._instrumented_keras_layer_class)
self.assertFalse(layer._instrumented_keras_model_class)
@combinations.generate(combinations.times(
combinations.keras_model_type_combinations(),
combinations.keras_tensor_combinations()))
@combinations.generate(combinations.keras_model_type_combinations())
def test_dynamic_layer(self):
model = testing_utils.get_model_from_layers([DynamicLayer(dynamic=True)],
input_shape=(3,))
@ -104,9 +102,7 @@ class BaseLayerTest(keras_parameterized.TestCase):
self.assertEqual(model.run_eagerly, True)
model.train_on_batch(np.random.random((2, 3)), np.random.random((2, 3)))
@combinations.generate(combinations.times(
combinations.keras_model_type_combinations(),
combinations.keras_tensor_combinations()))
@combinations.generate(combinations.keras_model_type_combinations())
def test_dynamic_layer_error(self):
# Functional Models hit the `dyanamic=True` error during construction.
# Subclass Models should just throw the original autograph error during
@ -125,9 +121,7 @@ class BaseLayerTest(keras_parameterized.TestCase):
raised_error = True
self.assertTrue(raised_error)
@combinations.generate(combinations.times(
combinations.keras_model_type_combinations(),
combinations.keras_tensor_combinations()))
@combinations.generate(combinations.keras_model_type_combinations())
def test_dynamic_layer_error_running_in_graph_mode(self):
with ops.get_default_graph().as_default():
model = testing_utils.get_model_from_layers([DynamicLayer(dynamic=True)],
@ -292,7 +286,6 @@ class BaseLayerTest(keras_parameterized.TestCase):
@combinations.generate(
combinations.times(
combinations.keras_model_type_combinations(),
combinations.keras_tensor_combinations(),
combinations.combine(mode=['graph', 'eager'])))
def test_build_with_numpy_data(self):
model_layers = [
@ -393,8 +386,7 @@ class BaseLayerTest(keras_parameterized.TestCase):
# b/124459427: can't test with `run_eagerly=True` for now.
@combinations.generate(
combinations.times(combinations.keras_mode_combinations(),
combinations.keras_model_type_combinations(),
combinations.keras_tensor_combinations()))
combinations.keras_model_type_combinations()))
def test_training_arg_in_defun(self):
layer = self._get_layer_with_training_arg()
model = testing_utils.get_model_from_layers([layer], input_shape=(1,))
@ -419,8 +411,7 @@ class BaseLayerTest(keras_parameterized.TestCase):
@combinations.generate(
combinations.times(combinations.keras_mode_combinations(),
combinations.keras_model_type_combinations(),
combinations.keras_tensor_combinations()))
combinations.keras_model_type_combinations()))
def test_raw_variable_assignment(self):
class RawVariableLayer(base_layer.Layer):
@ -488,65 +479,47 @@ class BaseLayerTest(keras_parameterized.TestCase):
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
def test_layer_names(self):
with testing_utils.use_keras_tensors_scope(False):
inputs = input_layer.Input(shape=[2])
add1 = inputs + inputs
add2 = layers.Add()([inputs, inputs])
add3 = inputs + inputs
add4 = layers.Add()([inputs, inputs])
model = training_lib.Model(
inputs=[inputs], outputs=[add1, add2, add3, add4])
actual_names = [l.name for l in model.layers]
graph_names = [
'input_1', 'tf_op_layer_AddV2', 'add', 'tf_op_layer_AddV2_1', 'add_1'
]
eager_names = [
'input_1', 'tf_op_layer_add', 'add', 'tf_op_layer_add_2', 'add_1'
]
for actual, eager, graph in zip(actual_names, graph_names, eager_names):
self.assertIn(actual, {eager, graph})
if context.executing_eagerly():
backend.clear_session()
with testing_utils.use_keras_tensors_scope(True):
inputs = input_layer.Input(shape=[2])
add1 = inputs + inputs
add2 = layers.Add()([inputs, inputs])
add3 = inputs + inputs
add4 = layers.Add()([inputs, inputs])
model = training_lib.Model(
inputs=[inputs], outputs=[add1, add2, add3, add4])
actual_names = [l.name for l in model.layers]
expected_names = [
'input_1', 'tf.__operators__.add', 'add', 'tf.__operators__.add_1',
'add_1'
]
self.assertAllEqual(actual_names, expected_names)
inputs = input_layer.Input(shape=[2])
add1 = inputs + inputs
add2 = layers.Add()([inputs, inputs])
add3 = inputs + inputs
add4 = layers.Add()([inputs, inputs])
model = training_lib.Model(inputs=[inputs],
outputs=[add1, add2, add3, add4])
actual_names = [l.name for l in model.layers]
graph_names = [
'input_1', 'tf_op_layer_add', 'add', 'tf_op_layer_add_2', 'add_1'
]
eager_names = [
'input_1', 'tf.__operators__.add', 'add', 'tf.__operators__.add_1',
'add_1'
]
for actual, eager, graph in zip(actual_names, graph_names, eager_names):
self.assertIn(actual, {eager, graph})
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
@combinations.generate(combinations.combine(mode=['eager']))
def test_layer_names_after_loading(self):
if context.executing_eagerly():
backend.clear_session()
with testing_utils.use_keras_tensors_scope(True):
# Mimic loading a model that already contained add layers with
# name = 'add_1' and 'tf.__operators__.add'
layers.Add(name='add_1')
layers.Add(name='tf.__operators__.add')
backend.clear_session()
# Mimic loading a model that already contained add layers with
# name = 'add_1' and 'tf.__operators__.add'
layers.Add(name='add_1')
layers.Add(name='tf.__operators__.add')
inputs = input_layer.Input(shape=[2])
add1 = inputs + inputs
add2 = layers.Add()([inputs, inputs])
add3 = inputs + inputs
add4 = layers.Add()([inputs, inputs])
model = training_lib.Model(
inputs=[inputs], outputs=[add1, add2, add3, add4])
actual_names = [l.name for l in model.layers]
# The generated op layer names should have avoided layer names seen in
# the loaded model. (This avoiance should not apply to non-op-layers)
expected_names = [
'input_1', 'tf.__operators__.add_1',
'add', 'tf.__operators__.add_2', 'add_1'
]
self.assertAllEqual(actual_names, expected_names)
inputs = input_layer.Input(shape=[2])
add1 = inputs + inputs
add2 = layers.Add()([inputs, inputs])
add3 = inputs + inputs
add4 = layers.Add()([inputs, inputs])
model = training_lib.Model(
inputs=[inputs], outputs=[add1, add2, add3, add4])
actual_names = [l.name for l in model.layers]
# The generated op layer names should have avoided layer names seen in
# the loaded model. (This avoiance should not apply to non-op-layers)
expected_names = [
'input_1', 'tf.__operators__.add_1',
'add', 'tf.__operators__.add_2', 'add_1'
]
self.assertAllEqual(actual_names, expected_names)
def test_add_trainable_weight_on_frozen_layer(self):

View File

@ -111,33 +111,31 @@ class OpLayerTest(keras_parameterized.TestCase):
_ = keras.Model(int_values, float_values)
def test_ragged_op_layer_keras_tensors(self):
with testing_utils.use_keras_tensors_scope(True):
int_values = keras.Input(shape=(None,), dtype=dtypes.int32, ragged=True)
float_values = math_ops.cast(int_values, dtypes.float32)
model = keras.Model(int_values, float_values)
model.compile(loss='mse')
int_values = keras.Input(shape=(None,), dtype=dtypes.int32, ragged=True)
float_values = math_ops.cast(int_values, dtypes.float32)
model = keras.Model(int_values, float_values)
model.compile(loss='mse')
input_data = ragged_factory_ops.constant(
[[1, 2], [3, 4]], dtype=np.int32)
expected = [[1.0, 2.0], [3.0, 4.0]]
output = model.predict(input_data)
self.assertIsInstance(output, ragged_tensor.RaggedTensor)
self.assertAllClose(expected, output)
input_data = ragged_factory_ops.constant(
[[1, 2], [3, 4]], dtype=np.int32)
expected = [[1.0, 2.0], [3.0, 4.0]]
output = model.predict(input_data)
self.assertIsInstance(output, ragged_tensor.RaggedTensor)
self.assertAllClose(expected, output)
def test_sparse_op_layer_keras_tensors(self):
with testing_utils.use_keras_tensors_scope(True):
int_values = keras.Input(shape=(None,), dtype=dtypes.int32, sparse=True)
float_values = math_ops.cast(int_values, dtypes.float32)
_ = keras.Model(int_values, float_values)
model = keras.Model(int_values, float_values)
model.compile(loss='mse')
int_values = keras.Input(shape=(None,), dtype=dtypes.int32, sparse=True)
float_values = math_ops.cast(int_values, dtypes.float32)
_ = keras.Model(int_values, float_values)
model = keras.Model(int_values, float_values)
model.compile(loss='mse')
input_data = sparse_ops.from_dense(
np.array([[1, 2], [3, 4]], dtype=np.int32))
expected = [[1.0, 2.0], [3.0, 4.0]]
output = model.predict(input_data)
self.assertIsInstance(output, sparse_tensor.SparseTensor)
self.assertAllClose(expected, sparse_ops.sparse_tensor_to_dense(output))
input_data = sparse_ops.from_dense(
np.array([[1, 2], [3, 4]], dtype=np.int32))
expected = [[1.0, 2.0], [3.0, 4.0]]
output = model.predict(input_data)
self.assertIsInstance(output, sparse_tensor.SparseTensor)
self.assertAllClose(expected, sparse_ops.sparse_tensor_to_dense(output))
if __name__ == '__main__':

View File

@ -944,29 +944,7 @@ class NetworkConstructionTest(keras_parameterized.TestCase):
# Check that second input was correctly added to first.
self.assertEqual(history.history['loss'][0], 0.0)
@combinations.generate(combinations.times(
combinations.keras_mode_combinations(mode='eager'),
combinations.combine(use_keras_tensors=False)))
def test_only_some_in_first_arg_derived_from_keras_layer(self):
class MyAddAll(layers.Layer):
def call(self, inputs):
x = inputs[0]
for inp in inputs[1:]:
if inp is not None:
x = x + inp
return x
input1 = input_layer_lib.Input(10)
input2 = input_layer_lib.Input(10)
layer = MyAddAll()
with self.assertRaisesRegexp(ValueError, 'construct a functional'):
layer([0.0, input1, None, input2, None])
@combinations.generate(combinations.times(
combinations.keras_mode_combinations(mode='eager'),
combinations.combine(use_keras_tensors=True)))
@combinations.generate(combinations.keras_mode_combinations(mode='eager'),)
def test_only_some_in_first_arg_derived_from_keras_layer_keras_tensors(self):
# This functionality is unsupported in v1 graphs
@ -1146,7 +1124,6 @@ class NetworkConstructionTest(keras_parameterized.TestCase):
@combinations.generate(
combinations.times(
combinations.keras_mode_combinations(),
combinations.keras_tensor_combinations(),
combinations.combine(share_already_used_layer=[True, False])))
def test_call_kwarg_derived_from_keras_layer_and_first_arg_is_constant(
self, share_already_used_layer):
@ -1246,9 +1223,7 @@ class NetworkConstructionTest(keras_parameterized.TestCase):
# Check that second input was correctly added to first.
self.assertEqual(history.history['loss'][0], 0.0)
@combinations.generate(combinations.times(
combinations.keras_mode_combinations(mode='eager'),
combinations.keras_tensor_combinations()))
@combinations.generate(combinations.keras_mode_combinations(mode='eager'))
def test_call_some_not_all_nested_in_first_arg_derived_from_keras_layer(self):
# This functionality is unsupported in v1 graphs

View File

@ -174,210 +174,201 @@ class InputLayerTest(keras_parameterized.TestCase):
@combinations.generate(combinations.combine(mode=['eager']))
def testInputTensorArgInTFFunction(self):
with testing_utils.use_keras_tensors_scope(True):
# We use a mutable model container instead of a model python variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
# We use a mutable model container instead of a model python variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
x = input_layer_lib.Input(tensor=array_ops.zeros((10, 16)))
self.assertAllEqual(x.shape.as_list(), [10, 16])
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
x = input_layer_lib.Input(tensor=array_ops.zeros((10, 16)))
self.assertAllEqual(x.shape.as_list(), [10, 16])
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3.0)
return model_container['model'](inp)
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3.0)
return model_container['model'](inp)
self.assertAllEqual(run_model(array_ops.ones((10, 16))),
array_ops.ones((10, 16)) * 3.0)
self.assertAllEqual(run_model(array_ops.ones((10, 16))),
array_ops.ones((10, 16)) * 3.0)
@combinations.generate(combinations.combine(mode=['eager']))
def testCompositeInputTensorArg(self):
with testing_utils.use_keras_tensors_scope(True):
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(tensor=rt)
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(tensor=rt)
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, x * 2)
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, x * 2)
# And that the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(model(rt), rt * 2)
# And that the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(model(rt), rt * 2)
@combinations.generate(combinations.combine(mode=['eager']))
def testCompositeInputTensorArgInTFFunction(self):
with testing_utils.use_keras_tensors_scope(True):
# We use a mutable model container instead of a model python variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
# We use a mutable model container instead of a model python variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(tensor=rt)
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(tensor=rt)
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3)
return model_container['model'](inp)
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3)
return model_container['model'](inp)
# And verify the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(run_model(rt), rt * 3)
# And verify the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(run_model(rt), rt * 3)
@combinations.generate(combinations.combine(mode=['eager']))
def testNoMixingArgsWithTypeSpecArg(self):
with testing_utils.use_keras_tensors_scope(True):
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
shape=(4, 7),
type_spec=tensor_spec.TensorSpec((2, 7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
batch_size=4,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
dtype=dtypes.int64,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
sparse=True,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
ragged=True,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
shape=(4, 7),
type_spec=tensor_spec.TensorSpec((2, 7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
batch_size=4,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
dtype=dtypes.int64,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
sparse=True,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
with self.assertRaisesRegexp(
ValueError, 'all other args except `name` must be None'):
input_layer_lib.Input(
ragged=True,
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
@combinations.generate(combinations.combine(mode=['eager']))
def testTypeSpecArg(self):
with testing_utils.use_keras_tensors_scope(True):
# Create a Keras Input
x = input_layer_lib.Input(
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
self.assertAllEqual(x.shape.as_list(), [7, 32])
# Create a Keras Input
x = input_layer_lib.Input(
type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
self.assertAllEqual(x.shape.as_list(), [7, 32])
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, x * 2.0)
self.assertAllEqual(model(array_ops.ones(x.shape)),
array_ops.ones(x.shape) * 2.0)
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, x * 2.0)
self.assertAllEqual(model(array_ops.ones(x.shape)),
array_ops.ones(x.shape) * 2.0)
# Test serialization / deserialization
model = functional.Functional.from_config(model.get_config())
self.assertAllEqual(model(array_ops.ones(x.shape)),
array_ops.ones(x.shape) * 2.0)
# Test serialization / deserialization
model = functional.Functional.from_config(model.get_config())
self.assertAllEqual(model(array_ops.ones(x.shape)),
array_ops.ones(x.shape) * 2.0)
model = model_config.model_from_json(model.to_json())
self.assertAllEqual(model(array_ops.ones(x.shape)),
array_ops.ones(x.shape) * 2.0)
model = model_config.model_from_json(model.to_json())
self.assertAllEqual(model(array_ops.ones(x.shape)),
array_ops.ones(x.shape) * 2.0)
@combinations.generate(combinations.combine(mode=['eager']))
def testTypeSpecArgInTFFunction(self):
with testing_utils.use_keras_tensors_scope(True):
# We use a mutable model container instead of a model python variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
# We use a mutable model container instead of a model python variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
x = input_layer_lib.Input(
type_spec=tensor_spec.TensorSpec((10, 16), dtypes.float32))
self.assertAllEqual(x.shape.as_list(), [10, 16])
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
x = input_layer_lib.Input(
type_spec=tensor_spec.TensorSpec((10, 16), dtypes.float32))
self.assertAllEqual(x.shape.as_list(), [10, 16])
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3.0)
return model_container['model'](inp)
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3.0)
return model_container['model'](inp)
self.assertAllEqual(run_model(array_ops.ones((10, 16))),
array_ops.ones((10, 16)) * 3.0)
self.assertAllEqual(run_model(array_ops.ones((10, 16))),
array_ops.ones((10, 16)) * 3.0)
@combinations.generate(combinations.combine(mode=['eager']))
def testCompositeTypeSpecArg(self):
with testing_utils.use_keras_tensors_scope(True):
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(type_spec=rt._type_spec)
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(type_spec=rt._type_spec)
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, x * 2)
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, x * 2)
# And that the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(model(rt), rt * 2)
# And that the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(model(rt), rt * 2)
# Test serialization / deserialization
model = functional.Functional.from_config(model.get_config())
self.assertAllEqual(model(rt), rt * 2)
model = model_config.model_from_json(model.to_json())
self.assertAllEqual(model(rt), rt * 2)
# Test serialization / deserialization
model = functional.Functional.from_config(model.get_config())
self.assertAllEqual(model(rt), rt * 2)
model = model_config.model_from_json(model.to_json())
self.assertAllEqual(model(rt), rt * 2)
@combinations.generate(combinations.combine(mode=['eager']))
def testCompositeTypeSpecArgInTFFunction(self):
with testing_utils.use_keras_tensors_scope(True):
# We use a mutable model container instead of a model pysthon variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
# We use a mutable model container instead of a model pysthon variable,
# because python 2.7 does not have `nonlocal`
model_container = {}
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(type_spec=rt._type_spec)
@def_function.function
def run_model(inp):
if not model_container:
# Create a Keras Input
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
x = input_layer_lib.Input(type_spec=rt._type_spec)
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3)
return model_container['model'](inp)
# Verify you can construct and use a model w/ this input
model_container['model'] = functional.Functional(x, x * 3)
return model_container['model'](inp)
# And verify the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(run_model(rt), rt * 3)
# And verify the model works
rt = ragged_tensor.RaggedTensor.from_row_splits(
values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
self.assertAllEqual(run_model(rt), rt * 3)
@combinations.generate(combinations.combine(mode=['eager']))
def testCompositeTypeSpecArgWithoutDtype(self):
with testing_utils.use_keras_tensors_scope(True):
for assign_variant_dtype in [False, True]:
# Create a Keras Input
spec = TwoTensorsSpecNoOneDtype(
(1, 2, 3), dtypes.float32, (1, 2, 3), dtypes.int64,
assign_variant_dtype=assign_variant_dtype)
x = input_layer_lib.Input(type_spec=spec)
for assign_variant_dtype in [False, True]:
# Create a Keras Input
spec = TwoTensorsSpecNoOneDtype(
(1, 2, 3), dtypes.float32, (1, 2, 3), dtypes.int64,
assign_variant_dtype=assign_variant_dtype)
x = input_layer_lib.Input(type_spec=spec)
def lambda_fn(tensors):
return (math_ops.cast(tensors.x, dtypes.float64)
+ math_ops.cast(tensors.y, dtypes.float64))
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, core.Lambda(lambda_fn)(x))
def lambda_fn(tensors):
return (math_ops.cast(tensors.x, dtypes.float64)
+ math_ops.cast(tensors.y, dtypes.float64))
# Verify you can construct and use a model w/ this input
model = functional.Functional(x, core.Lambda(lambda_fn)(x))
# And that the model works
two_tensors = TwoTensors(array_ops.ones((1, 2, 3)) * 2.0,
array_ops.ones(1, 2, 3))
self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
# And that the model works
two_tensors = TwoTensors(array_ops.ones((1, 2, 3)) * 2.0,
array_ops.ones(1, 2, 3))
self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
# Test serialization / deserialization
model = functional.Functional.from_config(model.get_config())
self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
model = model_config.model_from_json(model.to_json())
self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
# Test serialization / deserialization
model = functional.Functional.from_config(model.get_config())
self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
model = model_config.model_from_json(model.to_json())
self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
if __name__ == '__main__':

View File

@ -24,7 +24,6 @@ from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_spec
from tensorflow.python.keras import layers
from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.engine import keras_tensor
from tensorflow.python.ops import array_ops
from tensorflow.python.platform import test
@ -62,39 +61,38 @@ class KerasTensorTest(test.TestCase):
self.assertEqual(expected_str, str(kt))
self.assertEqual(expected_repr, repr(kt))
with testing_utils.use_keras_tensors_scope(True):
inp = layers.Input(shape=(3, 5))
kt = layers.Dense(10)(inp)
expected_str = (
"KerasTensor(type_spec=TensorSpec(shape=(None, 3, 10), "
"dtype=tf.float32, name=None), name='dense/BiasAdd:0', "
"description=\"created by layer 'dense'\")")
expected_repr = (
"<KerasTensor: shape=(None, 3, 10) dtype=float32 (created "
"by layer 'dense')>")
self.assertEqual(expected_str, str(kt))
self.assertEqual(expected_repr, repr(kt))
inp = layers.Input(shape=(3, 5))
kt = layers.Dense(10)(inp)
expected_str = (
"KerasTensor(type_spec=TensorSpec(shape=(None, 3, 10), "
"dtype=tf.float32, name=None), name='dense/BiasAdd:0', "
"description=\"created by layer 'dense'\")")
expected_repr = (
"<KerasTensor: shape=(None, 3, 10) dtype=float32 (created "
"by layer 'dense')>")
self.assertEqual(expected_str, str(kt))
self.assertEqual(expected_repr, repr(kt))
kt = array_ops.reshape(kt, shape=(3, 5, 2))
expected_str = (
"KerasTensor(type_spec=TensorSpec(shape=(3, 5, 2), dtype=tf.float32, "
"name=None), name='tf.reshape/Reshape:0', description=\"created "
"by layer 'tf.reshape'\")")
expected_repr = ("<KerasTensor: shape=(3, 5, 2) dtype=float32 (created "
"by layer 'tf.reshape')>")
self.assertEqual(expected_str, str(kt))
self.assertEqual(expected_repr, repr(kt))
kt = array_ops.reshape(kt, shape=(3, 5, 2))
expected_str = (
"KerasTensor(type_spec=TensorSpec(shape=(3, 5, 2), dtype=tf.float32, "
"name=None), name='tf.reshape/Reshape:0', description=\"created "
"by layer 'tf.reshape'\")")
expected_repr = ("<KerasTensor: shape=(3, 5, 2) dtype=float32 (created "
"by layer 'tf.reshape')>")
self.assertEqual(expected_str, str(kt))
self.assertEqual(expected_repr, repr(kt))
kts = array_ops.unstack(kt)
for i in range(3):
expected_str = (
"KerasTensor(type_spec=TensorSpec(shape=(5, 2), dtype=tf.float32, "
"name=None), name='tf.unstack/unstack:%s', description=\"created "
"by layer 'tf.unstack'\")" % (i,))
expected_repr = ("<KerasTensor: shape=(5, 2) dtype=float32 "
"(created by layer 'tf.unstack')>")
self.assertEqual(expected_str, str(kts[i]))
self.assertEqual(expected_repr, repr(kts[i]))
kts = array_ops.unstack(kt)
for i in range(3):
expected_str = (
"KerasTensor(type_spec=TensorSpec(shape=(5, 2), dtype=tf.float32, "
"name=None), name='tf.unstack/unstack:%s', description=\"created "
"by layer 'tf.unstack'\")" % (i,))
expected_repr = ("<KerasTensor: shape=(5, 2) dtype=float32 "
"(created by layer 'tf.unstack')>")
self.assertEqual(expected_str, str(kts[i]))
self.assertEqual(expected_repr, repr(kts[i]))
if __name__ == "__main__":
ops.enable_eager_execution()

View File

@ -28,7 +28,6 @@ from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import tensor_shape
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras import layers
from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.engine import training
from tensorflow.python.ops.ragged import ragged_factory_ops
from tensorflow.python.ops.ragged import ragged_tensor
@ -53,14 +52,13 @@ class RaggedKerasTensorTest(keras_parameterized.TestCase):
{'batch_size': 12, 'shape': (2, 3, None, 4, 5, None), 'ragged_rank': 6},
)
def test_to_placeholder(self, shape, batch_size, ragged_rank):
with testing_utils.use_keras_tensors_scope(True):
inp = layers.Input(shape=shape, batch_size=batch_size, ragged=True)
self.assertEqual(inp.ragged_rank, ragged_rank)
self.assertAllEqual(inp.shape, [batch_size] + list(shape))
with func_graph.FuncGraph('test').as_default():
placeholder = inp._to_placeholder()
self.assertEqual(placeholder.ragged_rank, ragged_rank)
self.assertAllEqual(placeholder.shape, [batch_size] + list(shape))
inp = layers.Input(shape=shape, batch_size=batch_size, ragged=True)
self.assertEqual(inp.ragged_rank, ragged_rank)
self.assertAllEqual(inp.shape, [batch_size] + list(shape))
with func_graph.FuncGraph('test').as_default():
placeholder = inp._to_placeholder()
self.assertEqual(placeholder.ragged_rank, ragged_rank)
self.assertAllEqual(placeholder.shape, [batch_size] + list(shape))
def test_add(self):
inp = layers.Input(shape=[None], ragged=True)

View File

@ -386,14 +386,10 @@ def run_all_keras_modes(test_or_class=None,
ImportError: If abseil parameterized is not installed or not included as
a target dependency.
"""
skip_keras_tensors = kwargs.pop('skip_keras_tensors', False)
if kwargs:
raise ValueError('Unrecognized keyword args: {}'.format(kwargs))
params = [('_v2_function', 'v2_function')]
if not skip_keras_tensors:
params.append(('_v2_function_use_keras_tensors',
'v2_function_use_keras_tensors'))
if not always_skip_eager:
params.append(('_v2_eager', 'v2_eager'))
if not (always_skip_v1 or tf2.enabled()):
@ -413,8 +409,6 @@ def run_all_keras_modes(test_or_class=None,
_v2_eager_test(f, self, *args, **kwargs)
elif run_mode == 'v2_function':
_v2_function_test(f, self, *args, **kwargs)
elif run_mode == 'v2_function_use_keras_tensors':
_v2_function_and_kerastensors_test(f, self, *args, **kwargs)
else:
return ValueError('Unknown run mode %s' % run_mode)
@ -442,13 +436,6 @@ def _v2_function_test(f, test_or_class, *args, **kwargs):
f(test_or_class, *args, **kwargs)
def _v2_function_and_kerastensors_test(f, test_or_class, *args, **kwargs):
with context.eager_mode():
with testing_utils.run_eagerly_scope(False):
with testing_utils.use_keras_tensors_scope(True):
f(test_or_class, *args, **kwargs)
def _test_or_class_decorator(test_or_class, single_method_decorator):
"""Decorate a test or class with a decorator intended for one method.

View File

@ -27,7 +27,6 @@ from tensorflow.python import tf2
from tensorflow.python.eager import context
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.engine import keras_tensor
from tensorflow.python.platform import test
@ -207,7 +206,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
def runTest(self):
pass
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes()
def testBody(self):
mode = "eager" if context.executing_eagerly() else "graph"
should_run_eagerly = testing_utils.should_run_eagerly()
@ -243,54 +242,6 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
ts.run(res)
self.assertLen(l, 4)
def test_run_all_keras_modes_include_keras_tensors(self):
l = []
class ExampleTest(keras_parameterized.TestCase):
def runTest(self):
pass
@keras_parameterized.run_all_keras_modes()
def testBody(self):
mode = "eager" if context.executing_eagerly() else "graph"
should_run_eagerly = testing_utils.should_run_eagerly()
l.append((mode, should_run_eagerly,
keras_tensor.keras_tensors_enabled()))
e = ExampleTest()
if not tf2.enabled():
e.testBody_v1_session()
e.testBody_v2_eager()
e.testBody_v2_function()
e.testBody_v2_function_use_keras_tensors()
if not tf2.enabled():
self.assertLen(l, 4)
self.assertAllEqual(l, [
("graph", False, False),
("eager", True, keras_tensor._KERAS_TENSORS_ENABLED),
("eager", False, keras_tensor._KERAS_TENSORS_ENABLED),
("eager", False, True),
])
ts = unittest.makeSuite(ExampleTest)
res = unittest.TestResult()
ts.run(res)
self.assertLen(l, 8)
else:
self.assertLen(l, 3)
self.assertAllEqual(l, [
("eager", True, keras_tensor._KERAS_TENSORS_ENABLED),
("eager", False, keras_tensor._KERAS_TENSORS_ENABLED),
("eager", False, True),
])
ts = unittest.makeSuite(ExampleTest)
res = unittest.TestResult()
ts.run(res)
self.assertLen(l, 6)
def test_run_all_keras_modes_extra_params(self):
l = []
@ -299,7 +250,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
def runTest(self):
pass
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes()
@parameterized.named_parameters(
[dict(testcase_name="_0", with_brackets=True),
dict(testcase_name="_1", with_brackets=False)])
@ -349,8 +300,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
def runTest(self):
pass
@keras_parameterized.run_all_keras_modes(always_skip_v1=True,
skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
def testBody(self):
mode = "eager" if context.executing_eagerly() else "graph"
should_run_eagerly = testing_utils.should_run_eagerly()
@ -380,7 +330,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
pass
@keras_parameterized.run_with_all_model_types
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes
def testBody(self):
mode = "eager" if context.executing_eagerly() else "graph"
should_run_eagerly = testing_utils.should_run_eagerly()
@ -432,7 +382,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
def runTest(self):
pass
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes
@keras_parameterized.run_with_all_model_types
def testBody(self):
mode = "eager" if context.executing_eagerly() else "graph"
@ -481,7 +431,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
l = []
@keras_parameterized.run_with_all_model_types
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes
class ExampleTest(keras_parameterized.TestCase):
def runTest(self):
@ -541,7 +491,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
def runTest(self):
pass
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes
@parameterized.named_parameters(dict(testcase_name="_arg",
arg=True))
def testBody(self, arg):
@ -587,7 +537,7 @@ class KerasParameterizedTest(keras_parameterized.TestCase):
self.assertLen(l, len(expected_combinations) * 2)
@keras_parameterized.run_all_keras_modes(skip_keras_tensors=True)
@keras_parameterized.run_all_keras_modes
@parameterized.named_parameters(dict(testcase_name="argument",
arg=True))
def test_run_all_keras_modes_extra_params_2(self, arg):