Remove the usage of eager context in test code, and replace them with test combinations.
PiperOrigin-RevId: 329947449 Change-Id: Ie513d6547c6ba458515be31e4df84976893cfd3d
This commit is contained in:
parent
23555a1f53
commit
ab3601d340
tensorflow/python/keras
callbacks_test.py
engine
feature_column
layers
legacy_tf_layers
metrics_functional_test.pymetrics_test.pymixed_precision/experimental
optimizer_v2
adadelta_test.pyadagrad_test.pyadam_test.pyadamax_test.pygradient_descent_test.pylearning_rate_schedule_test.pyoptimizer_v2_test.pyrmsprop_test.py
premade
tests
@ -934,33 +934,33 @@ class KerasCallbacksTest(keras_parameterized.TestCase):
|
||||
steps=10,
|
||||
verbose=0)
|
||||
|
||||
with context.eager_mode():
|
||||
tensor = ops.convert_to_tensor_v2_with_dispatch(1.)
|
||||
tensor = ops.convert_to_tensor_v2_with_dispatch(1.)
|
||||
|
||||
def mock_numpy():
|
||||
raise RuntimeError(
|
||||
'If this error is seen, ModelCheckpoint is causing a blocking '
|
||||
'NumPy conversion even when not checkpointing.')
|
||||
|
||||
with test.mock.patch.object(tensor, 'numpy', mock_numpy):
|
||||
logs = {'metric': tensor}
|
||||
tensor.numpy = mock_numpy
|
||||
|
||||
cb_list.on_train_begin(logs)
|
||||
cb_list.on_epoch_begin(0, logs)
|
||||
cb_list.on_train_batch_begin(0, logs)
|
||||
cb_list.on_train_batch_end(0, logs)
|
||||
cb_list.on_epoch_end(0, logs)
|
||||
cb_list.on_train_end(logs)
|
||||
logs = {'metric': tensor}
|
||||
|
||||
cb_list.on_test_begin(logs)
|
||||
cb_list.on_test_batch_begin(0, logs)
|
||||
cb_list.on_test_batch_end(0, logs)
|
||||
cb_list.on_test_end(logs)
|
||||
cb_list.on_train_begin(logs)
|
||||
cb_list.on_epoch_begin(0, logs)
|
||||
cb_list.on_train_batch_begin(0, logs)
|
||||
cb_list.on_train_batch_end(0, logs)
|
||||
cb_list.on_epoch_end(0, logs)
|
||||
cb_list.on_train_end(logs)
|
||||
|
||||
cb_list.on_predict_begin(logs)
|
||||
cb_list.on_predict_batch_begin(logs)
|
||||
cb_list.on_predict_batch_end(logs)
|
||||
cb_list.on_predict_end(logs)
|
||||
cb_list.on_test_begin(logs)
|
||||
cb_list.on_test_batch_begin(0, logs)
|
||||
cb_list.on_test_batch_end(0, logs)
|
||||
cb_list.on_test_end(logs)
|
||||
|
||||
cb_list.on_predict_begin(logs)
|
||||
cb_list.on_predict_batch_begin(logs)
|
||||
cb_list.on_predict_batch_end(logs)
|
||||
cb_list.on_predict_end(logs)
|
||||
|
||||
def test_ProgbarLogger_verbose_2_nonblocking(self):
|
||||
# Should only cause a sync block on epoch end methods.
|
||||
@ -974,31 +974,30 @@ class KerasCallbacksTest(keras_parameterized.TestCase):
|
||||
steps=10,
|
||||
verbose=2)
|
||||
|
||||
with context.eager_mode():
|
||||
tensor = ops.convert_to_tensor_v2_with_dispatch(1.)
|
||||
tensor = ops.convert_to_tensor_v2_with_dispatch(1.)
|
||||
|
||||
def mock_numpy():
|
||||
raise RuntimeError(
|
||||
'If this error is seen, ModelCheckpoint is causing a blocking '
|
||||
'NumPy conversion even when not checkpointing.')
|
||||
|
||||
with test.mock.patch.object(tensor, 'numpy', mock_numpy):
|
||||
logs = {'metric': tensor}
|
||||
tensor.numpy = mock_numpy
|
||||
logs = {'metric': tensor}
|
||||
|
||||
cb_list.on_train_begin(logs)
|
||||
cb_list.on_epoch_begin(0, logs)
|
||||
cb_list.on_train_batch_begin(0, logs)
|
||||
cb_list.on_train_batch_end(0, logs)
|
||||
cb_list.on_train_begin(logs)
|
||||
cb_list.on_epoch_begin(0, logs)
|
||||
cb_list.on_train_batch_begin(0, logs)
|
||||
cb_list.on_train_batch_end(0, logs)
|
||||
|
||||
cb_list.on_test_begin(logs)
|
||||
cb_list.on_test_batch_begin(0, logs)
|
||||
cb_list.on_test_batch_end(0, logs)
|
||||
cb_list.on_test_end(logs)
|
||||
cb_list.on_test_begin(logs)
|
||||
cb_list.on_test_batch_begin(0, logs)
|
||||
cb_list.on_test_batch_end(0, logs)
|
||||
cb_list.on_test_end(logs)
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, 'NumPy conversion'):
|
||||
# on_epoch_end should still block.
|
||||
cb_list.on_epoch_end(0, logs)
|
||||
cb_list.on_train_end(logs)
|
||||
with self.assertRaisesRegex(RuntimeError, 'NumPy conversion'):
|
||||
# on_epoch_end should still block.
|
||||
cb_list.on_epoch_end(0, logs)
|
||||
cb_list.on_train_end(logs)
|
||||
|
||||
def test_EarlyStopping(self):
|
||||
with self.cached_session():
|
||||
|
@ -26,7 +26,6 @@ import numpy as np
|
||||
from tensorflow.python import keras
|
||||
from tensorflow.python.data.experimental.ops import cardinality
|
||||
from tensorflow.python.data.ops import dataset_ops
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.framework import constant_op
|
||||
from tensorflow.python.framework import ops
|
||||
from tensorflow.python.framework import sparse_tensor
|
||||
@ -265,92 +264,92 @@ class TensorLikeDataAdapterTest(DataAdapterTestBase):
|
||||
self.assertEqual(adapter.get_size(), 10)
|
||||
self.assertFalse(adapter.has_partial_batch())
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_shuffle_correctness(self):
|
||||
with context.eager_mode():
|
||||
num_samples = 100
|
||||
batch_size = 32
|
||||
x = np.arange(num_samples)
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle=True, epochs=2)
|
||||
num_samples = 100
|
||||
batch_size = 32
|
||||
x = np.arange(num_samples)
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle=True, epochs=2)
|
||||
|
||||
def _get_epoch(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter).numpy())
|
||||
return np.concatenate(ds_data)
|
||||
def _get_epoch(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter).numpy())
|
||||
return np.concatenate(ds_data)
|
||||
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
|
||||
# First epoch.
|
||||
epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
# First epoch.
|
||||
epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
|
||||
# Second epoch.
|
||||
second_epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
# Second epoch.
|
||||
second_epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_batch_shuffle_correctness(self):
|
||||
with context.eager_mode():
|
||||
num_samples = 100
|
||||
batch_size = 6
|
||||
x = np.arange(num_samples)
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle='batch', epochs=2)
|
||||
num_samples = 100
|
||||
batch_size = 6
|
||||
x = np.arange(num_samples)
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle='batch', epochs=2)
|
||||
|
||||
def _get_epoch_batches(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter)[0].numpy())
|
||||
return ds_data
|
||||
def _get_epoch_batches(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter)[0].numpy())
|
||||
return ds_data
|
||||
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
|
||||
# First epoch.
|
||||
epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
epoch_data = np.concatenate(epoch_batch_data)
|
||||
# First epoch.
|
||||
epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
epoch_data = np.concatenate(epoch_batch_data)
|
||||
|
||||
def _verify_batch(batch):
|
||||
# Verify that a batch contains only contiguous data, and that it has
|
||||
# been shuffled.
|
||||
shuffled_batch = np.sort(batch)
|
||||
self.assertNotAllClose(batch, shuffled_batch)
|
||||
for i in range(1, len(batch)):
|
||||
self.assertEqual(shuffled_batch[i-1] + 1, shuffled_batch[i])
|
||||
def _verify_batch(batch):
|
||||
# Verify that a batch contains only contiguous data, and that it has
|
||||
# been shuffled.
|
||||
shuffled_batch = np.sort(batch)
|
||||
self.assertNotAllClose(batch, shuffled_batch)
|
||||
for i in range(1, len(batch)):
|
||||
self.assertEqual(shuffled_batch[i-1] + 1, shuffled_batch[i])
|
||||
|
||||
# Assert that the data within each batch remains contiguous
|
||||
for batch in epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
# Assert that the data within each batch remains contiguous
|
||||
for batch in epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
|
||||
# Check that individual batches are unshuffled
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
# Check that individual batches are unshuffled
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
|
||||
# Second epoch.
|
||||
second_epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
second_epoch_data = np.concatenate(second_epoch_batch_data)
|
||||
# Second epoch.
|
||||
second_epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
second_epoch_data = np.concatenate(second_epoch_batch_data)
|
||||
|
||||
# Assert that the data within each batch remains contiguous
|
||||
for batch in second_epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
# Assert that the data within each batch remains contiguous
|
||||
for batch in second_epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
|
||||
@parameterized.named_parameters(
|
||||
('batch_size_5', 5, None, 5),
|
||||
@ -494,92 +493,92 @@ class GenericArrayLikeDataAdapterTest(DataAdapterTestBase):
|
||||
self.model.evaluate(self.arraylike_input,
|
||||
self.tensor_target, batch_size=5)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_shuffle_correctness(self):
|
||||
with context.eager_mode():
|
||||
num_samples = 100
|
||||
batch_size = 32
|
||||
x = DummyArrayLike(np.arange(num_samples))
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle=True, epochs=2)
|
||||
num_samples = 100
|
||||
batch_size = 32
|
||||
x = DummyArrayLike(np.arange(num_samples))
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle=True, epochs=2)
|
||||
|
||||
def _get_epoch(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter).numpy())
|
||||
return np.concatenate(ds_data)
|
||||
def _get_epoch(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter).numpy())
|
||||
return np.concatenate(ds_data)
|
||||
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
|
||||
# First epoch.
|
||||
epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
# First epoch.
|
||||
epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
|
||||
# Second epoch.
|
||||
second_epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
# Second epoch.
|
||||
second_epoch_data = _get_epoch(ds_iter)
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_batch_shuffle_correctness(self):
|
||||
with context.eager_mode():
|
||||
num_samples = 100
|
||||
batch_size = 6
|
||||
x = DummyArrayLike(np.arange(num_samples))
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle='batch', epochs=2)
|
||||
num_samples = 100
|
||||
batch_size = 6
|
||||
x = DummyArrayLike(np.arange(num_samples))
|
||||
np.random.seed(99)
|
||||
adapter = self.adapter_cls(
|
||||
x, y=None, batch_size=batch_size, shuffle='batch', epochs=2)
|
||||
|
||||
def _get_epoch_batches(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter)[0].numpy())
|
||||
return ds_data
|
||||
def _get_epoch_batches(ds_iter):
|
||||
ds_data = []
|
||||
for _ in range(int(math.ceil(num_samples / batch_size))):
|
||||
ds_data.append(next(ds_iter)[0].numpy())
|
||||
return ds_data
|
||||
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
ds_iter = iter(adapter.get_dataset())
|
||||
|
||||
# First epoch.
|
||||
epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
epoch_data = np.concatenate(epoch_batch_data)
|
||||
# First epoch.
|
||||
epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
epoch_data = np.concatenate(epoch_batch_data)
|
||||
|
||||
def _verify_batch(batch):
|
||||
# Verify that a batch contains only contiguous data, but that it has
|
||||
# been shuffled.
|
||||
shuffled_batch = np.sort(batch)
|
||||
self.assertNotAllClose(batch, shuffled_batch)
|
||||
for i in range(1, len(batch)):
|
||||
self.assertEqual(shuffled_batch[i-1] + 1, shuffled_batch[i])
|
||||
def _verify_batch(batch):
|
||||
# Verify that a batch contains only contiguous data, but that it has
|
||||
# been shuffled.
|
||||
shuffled_batch = np.sort(batch)
|
||||
self.assertNotAllClose(batch, shuffled_batch)
|
||||
for i in range(1, len(batch)):
|
||||
self.assertEqual(shuffled_batch[i-1] + 1, shuffled_batch[i])
|
||||
|
||||
# Assert that the data within each batch is shuffled contiguous data
|
||||
for batch in epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
# Assert that the data within each batch is shuffled contiguous data
|
||||
for batch in epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
|
||||
# Check that individual batches are unshuffled
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
# Check that individual batches are unshuffled
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(epoch_data))
|
||||
|
||||
# Second epoch.
|
||||
second_epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
second_epoch_data = np.concatenate(second_epoch_batch_data)
|
||||
# Second epoch.
|
||||
second_epoch_batch_data = _get_epoch_batches(ds_iter)
|
||||
second_epoch_data = np.concatenate(second_epoch_batch_data)
|
||||
|
||||
# Assert that the data within each batch remains contiguous
|
||||
for batch in second_epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
# Assert that the data within each batch remains contiguous
|
||||
for batch in second_epoch_batch_data:
|
||||
_verify_batch(batch)
|
||||
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
# Check that shuffling occurred.
|
||||
self.assertNotAllClose(x, second_epoch_data)
|
||||
# Check that shuffling is different across epochs.
|
||||
self.assertNotAllClose(epoch_data, second_epoch_data)
|
||||
# Check that each elements appears, and only once.
|
||||
self.assertAllClose(x, np.sort(second_epoch_data))
|
||||
|
||||
@parameterized.named_parameters(
|
||||
('batch_size_5', 5, None, 5),
|
||||
@ -711,15 +710,15 @@ class GeneratorDataAdapterTest(DataAdapterTestBase):
|
||||
self.adapter_cls(
|
||||
self.generator_input, sample_weights=self.generator_input)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_not_shuffled(self):
|
||||
def generator():
|
||||
for i in range(10):
|
||||
yield np.ones((1, 1)) * i
|
||||
|
||||
adapter = self.adapter_cls(generator(), shuffle=True)
|
||||
with context.eager_mode():
|
||||
for i, data in enumerate(adapter.get_dataset()):
|
||||
self.assertEqual(i, data[0].numpy().flatten())
|
||||
for i, data in enumerate(adapter.get_dataset()):
|
||||
self.assertEqual(i, data[0].numpy().flatten())
|
||||
|
||||
|
||||
class KerasSequenceAdapterTest(DataAdapterTestBase):
|
||||
|
@ -987,17 +987,17 @@ class TrainingTest(keras_parameterized.TestCase):
|
||||
# Test with eager execution and iterator
|
||||
model.fit(dataset, epochs=1, steps_per_epoch=2)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_losses_in_defun(self):
|
||||
with context.eager_mode():
|
||||
layer = layers_module.Dense(1, kernel_regularizer='l1')
|
||||
layer(array_ops.ones([1, 10]))
|
||||
layer = layers_module.Dense(1, kernel_regularizer='l1')
|
||||
layer(array_ops.ones([1, 10]))
|
||||
|
||||
@function.defun
|
||||
def get_losses():
|
||||
return layer.losses
|
||||
@function.defun
|
||||
def get_losses():
|
||||
return layer.losses
|
||||
|
||||
self.assertAllEqual(
|
||||
self.evaluate(layer.losses), self.evaluate(get_losses()))
|
||||
self.assertAllEqual(
|
||||
self.evaluate(layer.losses), self.evaluate(get_losses()))
|
||||
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
def test_logging(self):
|
||||
@ -1118,70 +1118,70 @@ class TrainingTest(keras_parameterized.TestCase):
|
||||
self.assertAllEqual([[6], [8], [10], [12]],
|
||||
model.predict(dataset_two, steps=2))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_training_on_sparse_categorical_crossentropy_loss_with_softmax(self):
|
||||
with context.eager_mode():
|
||||
np.random.seed(1337)
|
||||
train_x = np.ones((100, 4))
|
||||
train_y = np.random.randint(0, 1, size=(100, 1))
|
||||
np.random.seed(1337)
|
||||
train_x = np.ones((100, 4))
|
||||
train_y = np.random.randint(0, 1, size=(100, 1))
|
||||
|
||||
reference_model = testing_utils.get_small_sequential_mlp(16, 2,
|
||||
input_dim=4)
|
||||
reference_model.compile(loss='sparse_categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=True)
|
||||
fixed_weights = reference_model.get_weights()
|
||||
reference_model_loss = reference_model.train_on_batch(train_x, train_y)
|
||||
reference_model = testing_utils.get_small_sequential_mlp(16, 2,
|
||||
input_dim=4)
|
||||
reference_model.compile(loss='sparse_categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=True)
|
||||
fixed_weights = reference_model.get_weights()
|
||||
reference_model_loss = reference_model.train_on_batch(train_x, train_y)
|
||||
|
||||
test_model = testing_utils.get_small_sequential_mlp(16, 2, input_dim=4)
|
||||
test_model.compile(loss='sparse_categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=False)
|
||||
test_model.set_weights(fixed_weights)
|
||||
test_model_loss = test_model.train_on_batch(train_x, train_y)
|
||||
self.assertAlmostEqual(test_model_loss, reference_model_loss, places=4)
|
||||
test_model = testing_utils.get_small_sequential_mlp(16, 2, input_dim=4)
|
||||
test_model.compile(loss='sparse_categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=False)
|
||||
test_model.set_weights(fixed_weights)
|
||||
test_model_loss = test_model.train_on_batch(train_x, train_y)
|
||||
self.assertAlmostEqual(test_model_loss, reference_model_loss, places=4)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_training_on_categorical_crossentropy_loss_with_softmax(self):
|
||||
with context.eager_mode():
|
||||
np.random.seed(1337)
|
||||
train_x = np.ones((100, 4))
|
||||
train_y = np_utils.to_categorical(
|
||||
np.random.randint(0, 1, size=(100, 1)), 2)
|
||||
np.random.seed(1337)
|
||||
train_x = np.ones((100, 4))
|
||||
train_y = np_utils.to_categorical(
|
||||
np.random.randint(0, 1, size=(100, 1)), 2)
|
||||
|
||||
reference_model = testing_utils.get_small_sequential_mlp(16, 2,
|
||||
input_dim=4)
|
||||
reference_model.compile(loss='categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=True)
|
||||
fixed_weights = reference_model.get_weights()
|
||||
reference_model_loss = reference_model.train_on_batch(train_x, train_y)
|
||||
reference_model = testing_utils.get_small_sequential_mlp(16, 2,
|
||||
input_dim=4)
|
||||
reference_model.compile(loss='categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=True)
|
||||
fixed_weights = reference_model.get_weights()
|
||||
reference_model_loss = reference_model.train_on_batch(train_x, train_y)
|
||||
|
||||
test_model = testing_utils.get_small_sequential_mlp(16, 2, input_dim=4)
|
||||
test_model.compile(loss='categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=False)
|
||||
test_model.set_weights(fixed_weights)
|
||||
test_model_loss = test_model.train_on_batch(train_x, train_y)
|
||||
self.assertAlmostEqual(test_model_loss, reference_model_loss, places=4)
|
||||
test_model = testing_utils.get_small_sequential_mlp(16, 2, input_dim=4)
|
||||
test_model.compile(loss='categorical_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=False)
|
||||
test_model.set_weights(fixed_weights)
|
||||
test_model_loss = test_model.train_on_batch(train_x, train_y)
|
||||
self.assertAlmostEqual(test_model_loss, reference_model_loss, places=4)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_training_on_binary_crossentropy_loss(self):
|
||||
with context.eager_mode():
|
||||
train_x = np.ones((100, 4), dtype=np.float32)
|
||||
train_y = np.ones((100, 1), dtype=np.float32)
|
||||
reference_model = testing_utils.get_small_sequential_mlp(16, 1,
|
||||
input_dim=4)
|
||||
reference_model.compile(loss='binary_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=True)
|
||||
fixed_weights = reference_model.get_weights()
|
||||
reference_model_loss = reference_model.train_on_batch(train_x, train_y)
|
||||
train_x = np.ones((100, 4), dtype=np.float32)
|
||||
train_y = np.ones((100, 1), dtype=np.float32)
|
||||
reference_model = testing_utils.get_small_sequential_mlp(16, 1,
|
||||
input_dim=4)
|
||||
reference_model.compile(loss='binary_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=True)
|
||||
fixed_weights = reference_model.get_weights()
|
||||
reference_model_loss = reference_model.train_on_batch(train_x, train_y)
|
||||
|
||||
test_model = testing_utils.get_small_sequential_mlp(16, 1, input_dim=4)
|
||||
test_model.compile(loss='binary_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=False)
|
||||
test_model.set_weights(fixed_weights)
|
||||
test_model_loss = test_model.train_on_batch(train_x, train_y)
|
||||
self.assertAlmostEqual(test_model_loss, reference_model_loss, places=4)
|
||||
test_model = testing_utils.get_small_sequential_mlp(16, 1, input_dim=4)
|
||||
test_model.compile(loss='binary_crossentropy',
|
||||
optimizer=RMSPropOptimizer(learning_rate=0.001),
|
||||
run_eagerly=False)
|
||||
test_model.set_weights(fixed_weights)
|
||||
test_model_loss = test_model.train_on_batch(train_x, train_y)
|
||||
self.assertAlmostEqual(test_model_loss, reference_model_loss, places=4)
|
||||
|
||||
@keras_parameterized.run_with_all_model_types
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
|
@ -55,28 +55,28 @@ class ModelInputsTest(test.TestCase):
|
||||
self.assertEqual(backend.floatx(), vals[0].dtype)
|
||||
|
||||
def test_single_thing_eager(self):
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest('Run in eager mode only.')
|
||||
with testing_utils.use_keras_tensors_scope(False):
|
||||
with context.eager_mode():
|
||||
a = np.ones(10, dtype=np.int32)
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1'], model_inputs.get_input_names())
|
||||
val = model_inputs.get_symbolic_inputs()
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(val))
|
||||
vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
|
||||
self.assertEqual(1, len(vals))
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals[0]))
|
||||
self.assertEqual(dtypes.int32, vals[0].dtype)
|
||||
a = np.ones(10, dtype=np.int32)
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1'], model_inputs.get_input_names())
|
||||
val = model_inputs.get_symbolic_inputs()
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(val))
|
||||
vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
|
||||
self.assertEqual(1, len(vals))
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals[0]))
|
||||
self.assertEqual(dtypes.int32, vals[0].dtype)
|
||||
with testing_utils.use_keras_tensors_scope(True):
|
||||
with context.eager_mode():
|
||||
a = np.ones(10, dtype=np.int32)
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1'], model_inputs.get_input_names())
|
||||
val = model_inputs.get_symbolic_inputs()
|
||||
self.assertIsInstance(val, keras_tensor.KerasTensor)
|
||||
vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
|
||||
self.assertEqual(1, len(vals))
|
||||
self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
|
||||
self.assertEqual(dtypes.int32, vals[0].dtype)
|
||||
a = np.ones(10, dtype=np.int32)
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1'], model_inputs.get_input_names())
|
||||
val = model_inputs.get_symbolic_inputs()
|
||||
self.assertIsInstance(val, keras_tensor.KerasTensor)
|
||||
vals = model_inputs.get_symbolic_inputs(return_single_as_list=True)
|
||||
self.assertEqual(1, len(vals))
|
||||
self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
|
||||
self.assertEqual(dtypes.int32, vals[0].dtype)
|
||||
|
||||
def test_list(self):
|
||||
a = [np.ones(10), np.ones(20)]
|
||||
@ -87,22 +87,22 @@ class ModelInputsTest(test.TestCase):
|
||||
self.assertTrue(tensor_util.is_tensor(vals[1]))
|
||||
|
||||
def test_list_eager(self):
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest('Run in eager mode only.')
|
||||
with testing_utils.use_keras_tensors_scope(False):
|
||||
with context.eager_mode():
|
||||
a = [np.ones(10), np.ones(20)]
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1', 'input_2'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals[0]))
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals[1]))
|
||||
a = [np.ones(10), np.ones(20)]
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1', 'input_2'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals[0]))
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals[1]))
|
||||
with testing_utils.use_keras_tensors_scope(True):
|
||||
with context.eager_mode():
|
||||
a = [np.ones(10), np.ones(20)]
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1', 'input_2'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
|
||||
self.assertIsInstance(vals[1], keras_tensor.KerasTensor)
|
||||
a = [np.ones(10), np.ones(20)]
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['input_1', 'input_2'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertIsInstance(vals[0], keras_tensor.KerasTensor)
|
||||
self.assertIsInstance(vals[1], keras_tensor.KerasTensor)
|
||||
|
||||
def test_dict(self):
|
||||
a = {'b': np.ones(10), 'a': np.ones(20)}
|
||||
@ -113,22 +113,22 @@ class ModelInputsTest(test.TestCase):
|
||||
self.assertTrue(tensor_util.is_tensor(vals['b']))
|
||||
|
||||
def test_dict_eager(self):
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest('Run in eager mode only.')
|
||||
with testing_utils.use_keras_tensors_scope(False):
|
||||
with context.eager_mode():
|
||||
a = {'b': np.ones(10), 'a': np.ones(20)}
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['a', 'b'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals['a']))
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals['b']))
|
||||
a = {'b': np.ones(10), 'a': np.ones(20)}
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['a', 'b'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals['a']))
|
||||
self.assertTrue(tf_utils.is_symbolic_tensor(vals['b']))
|
||||
with testing_utils.use_keras_tensors_scope(True):
|
||||
with context.eager_mode():
|
||||
a = {'b': np.ones(10), 'a': np.ones(20)}
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['a', 'b'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertIsInstance(vals['a'], keras_tensor.KerasTensor)
|
||||
self.assertIsInstance(vals['b'], keras_tensor.KerasTensor)
|
||||
a = {'b': np.ones(10), 'a': np.ones(20)}
|
||||
model_inputs = training_utils.ModelInputs(a)
|
||||
self.assertEqual(['a', 'b'], model_inputs.get_input_names())
|
||||
vals = model_inputs.get_symbolic_inputs()
|
||||
self.assertIsInstance(vals['a'], keras_tensor.KerasTensor)
|
||||
self.assertIsInstance(vals['b'], keras_tensor.KerasTensor)
|
||||
|
||||
|
||||
class DatasetUtilsTest(test.TestCase, parameterized.TestCase):
|
||||
|
@ -23,7 +23,6 @@ import numpy as np
|
||||
|
||||
from tensorflow.python.client import session
|
||||
from tensorflow.python.eager import backprop
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.feature_column import feature_column_v2 as fc
|
||||
from tensorflow.python.feature_column import sequence_feature_column as sfc
|
||||
from tensorflow.python.framework import constant_op
|
||||
@ -59,148 +58,148 @@ class DenseFeaturesTest(keras_parameterized.TestCase):
|
||||
inputs = self.evaluate(dense_features(features))
|
||||
self.assertAllClose([[0.]], inputs)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_reuses_variables(self):
|
||||
with context.eager_mode():
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
|
||||
inputs = dense_features(features)
|
||||
variables = dense_features.variables
|
||||
inputs = dense_features(features)
|
||||
variables = dense_features.variables
|
||||
|
||||
# Sanity check: test that the inputs are correct.
|
||||
self.assertAllEqual([[1, 0], [0, 1], [1, 1]], inputs)
|
||||
# Sanity check: test that the inputs are correct.
|
||||
self.assertAllEqual([[1, 0], [0, 1], [1, 1]], inputs)
|
||||
|
||||
# Check that only one variable was created.
|
||||
self.assertEqual(1, len(variables))
|
||||
# Check that only one variable was created.
|
||||
self.assertEqual(1, len(variables))
|
||||
|
||||
# Check that invoking dense_features on the same features does not create
|
||||
# additional variables
|
||||
_ = dense_features(features)
|
||||
self.assertEqual(1, len(variables))
|
||||
self.assertIs(variables[0], dense_features.variables[0])
|
||||
# Check that invoking dense_features on the same features does not create
|
||||
# additional variables
|
||||
_ = dense_features(features)
|
||||
self.assertEqual(1, len(variables))
|
||||
self.assertIs(variables[0], dense_features.variables[0])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_dense_feature_with_partitioner(self):
|
||||
with context.eager_mode():
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0), (3, 0)),
|
||||
values=(0, 1, 3, 2),
|
||||
dense_shape=(4, 4))
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0), (3, 0)),
|
||||
values=(0, 1, 3, 2),
|
||||
dense_shape=(4, 4))
|
||||
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=4)
|
||||
embedding_dimension = 2
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=4)
|
||||
embedding_dimension = 2
|
||||
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
offset = partition_info._var_offset[0]
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
if offset == 0:
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1)) # id 1
|
||||
else:
|
||||
embedding_values = (
|
||||
(1, 1), # id 2
|
||||
(2, 2)) # id 3
|
||||
return embedding_values
|
||||
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
|
||||
dense_features = df.DenseFeatures(
|
||||
[embedding_column],
|
||||
partitioner=partitioned_variables.fixed_size_partitioner(2))
|
||||
features = {'a': sparse_input}
|
||||
|
||||
inputs = dense_features(features)
|
||||
variables = dense_features.variables
|
||||
|
||||
# Sanity check: test that the inputs are correct.
|
||||
self.assertAllEqual([[1, 0], [0, 1], [2, 2], [1, 1]], inputs)
|
||||
|
||||
# Check that only one variable was created.
|
||||
self.assertEqual(2, len(variables))
|
||||
|
||||
# Check that invoking dense_features on the same features does not create
|
||||
# additional variables
|
||||
_ = dense_features(features)
|
||||
self.assertEqual(2, len(variables))
|
||||
self.assertIs(variables[0], dense_features.variables[0])
|
||||
self.assertIs(variables[1], dense_features.variables[1])
|
||||
|
||||
def test_feature_column_dense_features_gradient(self):
|
||||
with context.eager_mode():
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
offset = partition_info._var_offset[0]
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
if offset == 0:
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
(0, 1)) # id 1
|
||||
else:
|
||||
embedding_values = (
|
||||
(1, 1), # id 2
|
||||
(2, 2)) # id 3
|
||||
return embedding_values
|
||||
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
dense_features = df.DenseFeatures(
|
||||
[embedding_column],
|
||||
partitioner=partitioned_variables.fixed_size_partitioner(2))
|
||||
features = {'a': sparse_input}
|
||||
|
||||
def scale_matrix():
|
||||
matrix = dense_features(features)
|
||||
return 2 * matrix
|
||||
inputs = dense_features(features)
|
||||
variables = dense_features.variables
|
||||
|
||||
# Sanity check: Verify that scale_matrix returns the correct output.
|
||||
self.assertAllEqual([[2, 0], [0, 2], [2, 2]], scale_matrix())
|
||||
# Sanity check: test that the inputs are correct.
|
||||
self.assertAllEqual([[1, 0], [0, 1], [2, 2], [1, 1]], inputs)
|
||||
|
||||
# Check that the returned gradient is correct.
|
||||
grad_function = backprop.implicit_grad(scale_matrix)
|
||||
grads_and_vars = grad_function()
|
||||
indexed_slice = grads_and_vars[0][0]
|
||||
gradient = grads_and_vars[0][0].values
|
||||
# Check that only one variable was created.
|
||||
self.assertEqual(2, len(variables))
|
||||
|
||||
self.assertAllEqual([0, 1, 2], indexed_slice.indices)
|
||||
self.assertAllEqual([[2, 2], [2, 2], [2, 2]], gradient)
|
||||
# Check that invoking dense_features on the same features does not create
|
||||
# additional variables
|
||||
_ = dense_features(features)
|
||||
self.assertEqual(2, len(variables))
|
||||
self.assertIs(variables[0], dense_features.variables[0])
|
||||
self.assertIs(variables[1], dense_features.variables[1])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_feature_column_dense_features_gradient(self):
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
|
||||
def scale_matrix():
|
||||
matrix = dense_features(features)
|
||||
return 2 * matrix
|
||||
|
||||
# Sanity check: Verify that scale_matrix returns the correct output.
|
||||
self.assertAllEqual([[2, 0], [0, 2], [2, 2]], scale_matrix())
|
||||
|
||||
# Check that the returned gradient is correct.
|
||||
grad_function = backprop.implicit_grad(scale_matrix)
|
||||
grads_and_vars = grad_function()
|
||||
indexed_slice = grads_and_vars[0][0]
|
||||
gradient = grads_and_vars[0][0].values
|
||||
|
||||
self.assertAllEqual([0, 1, 2], indexed_slice.indices)
|
||||
self.assertAllEqual([[2, 2], [2, 2], [2, 2]], gradient)
|
||||
|
||||
def test_raises_if_empty_feature_columns(self):
|
||||
with self.assertRaisesRegex(ValueError,
|
||||
|
@ -22,7 +22,6 @@ import numpy as np
|
||||
|
||||
from tensorflow.python.client import session
|
||||
from tensorflow.python.eager import backprop
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.feature_column import feature_column_v2 as fc
|
||||
from tensorflow.python.framework import constant_op
|
||||
from tensorflow.python.framework import dtypes
|
||||
@ -54,96 +53,96 @@ class DenseFeaturesTest(keras_parameterized.TestCase):
|
||||
inputs = self.evaluate(dense_features(features))
|
||||
self.assertAllClose([[0.]], inputs)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_reuses_variables(self):
|
||||
with context.eager_mode():
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
|
||||
inputs = dense_features(features)
|
||||
variables = dense_features.variables
|
||||
inputs = dense_features(features)
|
||||
variables = dense_features.variables
|
||||
|
||||
# Sanity check: test that the inputs are correct.
|
||||
self.assertAllEqual([[1, 0], [0, 1], [1, 1]], inputs)
|
||||
# Sanity check: test that the inputs are correct.
|
||||
self.assertAllEqual([[1, 0], [0, 1], [1, 1]], inputs)
|
||||
|
||||
# Check that only one variable was created.
|
||||
self.assertEqual(1, len(variables))
|
||||
# Check that only one variable was created.
|
||||
self.assertEqual(1, len(variables))
|
||||
|
||||
# Check that invoking dense_features on the same features does not create
|
||||
# additional variables
|
||||
_ = dense_features(features)
|
||||
self.assertEqual(1, len(variables))
|
||||
self.assertIs(variables[0], dense_features.variables[0])
|
||||
# Check that invoking dense_features on the same features does not create
|
||||
# additional variables
|
||||
_ = dense_features(features)
|
||||
self.assertEqual(1, len(variables))
|
||||
self.assertIs(variables[0], dense_features.variables[0])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_feature_column_dense_features_gradient(self):
|
||||
with context.eager_mode():
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
sparse_input = sparse_tensor.SparseTensor(
|
||||
indices=((0, 0), (1, 0), (2, 0)),
|
||||
values=(0, 1, 2),
|
||||
dense_shape=(3, 3))
|
||||
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
# Create feature columns (categorical and embedding).
|
||||
categorical_column = fc.categorical_column_with_identity(
|
||||
key='a', num_buckets=3)
|
||||
embedding_dimension = 2
|
||||
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
def _embedding_column_initializer(shape, dtype, partition_info=None):
|
||||
del shape # unused
|
||||
del dtype # unused
|
||||
del partition_info # unused
|
||||
embedding_values = (
|
||||
(1, 0), # id 0
|
||||
(0, 1), # id 1
|
||||
(1, 1)) # id 2
|
||||
return embedding_values
|
||||
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
embedding_column = fc.embedding_column(
|
||||
categorical_column,
|
||||
dimension=embedding_dimension,
|
||||
initializer=_embedding_column_initializer)
|
||||
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
dense_features = df.DenseFeatures([embedding_column])
|
||||
features = {'a': sparse_input}
|
||||
|
||||
def scale_matrix():
|
||||
matrix = dense_features(features)
|
||||
return 2 * matrix
|
||||
def scale_matrix():
|
||||
matrix = dense_features(features)
|
||||
return 2 * matrix
|
||||
|
||||
# Sanity check: Verify that scale_matrix returns the correct output.
|
||||
self.assertAllEqual([[2, 0], [0, 2], [2, 2]], scale_matrix())
|
||||
# Sanity check: Verify that scale_matrix returns the correct output.
|
||||
self.assertAllEqual([[2, 0], [0, 2], [2, 2]], scale_matrix())
|
||||
|
||||
# Check that the returned gradient is correct.
|
||||
grad_function = backprop.implicit_grad(scale_matrix)
|
||||
grads_and_vars = grad_function()
|
||||
indexed_slice = grads_and_vars[0][0]
|
||||
gradient = grads_and_vars[0][0].values
|
||||
# Check that the returned gradient is correct.
|
||||
grad_function = backprop.implicit_grad(scale_matrix)
|
||||
grads_and_vars = grad_function()
|
||||
indexed_slice = grads_and_vars[0][0]
|
||||
gradient = grads_and_vars[0][0].values
|
||||
|
||||
self.assertAllEqual([0, 1, 2], indexed_slice.indices)
|
||||
self.assertAllEqual([[2, 2], [2, 2], [2, 2]], gradient)
|
||||
self.assertAllEqual([0, 1, 2], indexed_slice.indices)
|
||||
self.assertAllEqual([[2, 2], [2, 2], [2, 2]], gradient)
|
||||
|
||||
def test_dense_feature_with_training_arg(self):
|
||||
price1 = fc.numeric_column('price1', shape=2)
|
||||
|
@ -385,10 +385,11 @@ class AttentionTest(test.TestCase, parameterized.TestCase):
|
||||
|
||||
def test_scale_init_eager(self):
|
||||
"""Tests that scale initializes to 1 when use_scale=True."""
|
||||
with context.eager_mode():
|
||||
attention_layer = dense_attention.Attention(use_scale=True)
|
||||
attention_layer.build(input_shape=([1, 1, 1], [1, 1, 1]))
|
||||
self.assertAllClose(1., attention_layer.scale.value())
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest('Only run in eager mode')
|
||||
attention_layer = dense_attention.Attention(use_scale=True)
|
||||
attention_layer.build(input_shape=([1, 1, 1], [1, 1, 1]))
|
||||
self.assertAllClose(1., attention_layer.scale.value())
|
||||
|
||||
def test_scale_init_graph(self):
|
||||
"""Tests that scale initializes to 1 when use_scale=True."""
|
||||
|
@ -22,7 +22,6 @@ from absl.testing import parameterized
|
||||
import numpy as np
|
||||
|
||||
from tensorflow.python import keras
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.eager import def_function
|
||||
from tensorflow.python.eager import wrap_function
|
||||
from tensorflow.python.framework import constant_op
|
||||
@ -210,6 +209,7 @@ class BatchNormalizationTest(keras_parameterized.TestCase):
|
||||
train_loss = model.train_on_batch(test_data, test_targets)
|
||||
self.assertAlmostEqual(test_loss, train_loss)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_eager_batchnorm_in_custom_model_call_with_tf_function(self):
|
||||
|
||||
class MyModel(keras.Model):
|
||||
@ -222,16 +222,15 @@ class BatchNormalizationTest(keras_parameterized.TestCase):
|
||||
def call(self, x, training):
|
||||
return self.bn(x, training=training)
|
||||
|
||||
with context.eager_mode():
|
||||
model = MyModel()
|
||||
model = MyModel()
|
||||
|
||||
for _ in range(10):
|
||||
x = constant_op.constant(0.5, shape=[1, 1])
|
||||
model(x, training=True)
|
||||
for _ in range(10):
|
||||
x = constant_op.constant(0.5, shape=[1, 1])
|
||||
model(x, training=True)
|
||||
|
||||
# Make sure the moving mean and variance have been updated
|
||||
self.assertAllClose(model.bn.moving_mean.numpy(), [0.047], atol=3e-3)
|
||||
self.assertAllClose(model.bn.moving_variance.numpy(), [0.9], atol=3e-2)
|
||||
# Make sure the moving mean and variance have been updated
|
||||
self.assertAllClose(model.bn.moving_mean.numpy(), [0.047], atol=3e-3)
|
||||
self.assertAllClose(model.bn.moving_variance.numpy(), [0.9], atol=3e-2)
|
||||
|
||||
|
||||
class BatchNormalizationV1Test(keras_parameterized.TestCase):
|
||||
|
@ -731,7 +731,8 @@ class AutoLambdaTest(keras_parameterized.TestCase):
|
||||
model.summary()
|
||||
|
||||
|
||||
class InputInEagerTest(test.TestCase):
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
class InputInEagerTest(keras_parameterized.TestCase):
|
||||
"""Tests ops on keras inputs in Eager runtime.
|
||||
|
||||
Input returns graph/symbolic tensors in the Eager runtime (this
|
||||
@ -740,21 +741,19 @@ class InputInEagerTest(test.TestCase):
|
||||
"""
|
||||
|
||||
def test_identity(self):
|
||||
with context.eager_mode():
|
||||
x = keras.Input(shape=(1,))
|
||||
ident = array_ops.identity(x)
|
||||
x = keras.Input(shape=(1,))
|
||||
ident = array_ops.identity(x)
|
||||
|
||||
# This is now a graph tensor, and should be able to continue in graphland
|
||||
self.assertIn('Identity', ident.name)
|
||||
# This is now a graph tensor, and should be able to continue in graphland
|
||||
self.assertIn('Identity', ident.name)
|
||||
|
||||
def test_size(self):
|
||||
with context.eager_mode():
|
||||
x = keras.Input(shape=(3,))
|
||||
self.assertAllEqual(x.get_shape().as_list(), [None, 3])
|
||||
sz = array_ops.size(x)
|
||||
x = keras.Input(shape=(3,))
|
||||
self.assertAllEqual(x.get_shape().as_list(), [None, 3])
|
||||
sz = array_ops.size(x)
|
||||
|
||||
# This is now a graph tensor, and should be able to continue in graphland
|
||||
self.assertIn('Size', sz.name)
|
||||
# This is now a graph tensor, and should be able to continue in graphland
|
||||
self.assertIn('Size', sz.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -290,22 +290,22 @@ class DenseTest(test.TestCase, parameterized.TestCase):
|
||||
self.assertAllClose(weights['scope/dense/bias'].read_value(), np.zeros(
|
||||
(2)))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def testEagerExecution(self):
|
||||
with context.eager_mode():
|
||||
container = variable_scope.EagerVariableStore()
|
||||
x = constant_op.constant([[2.0]])
|
||||
with container.as_default():
|
||||
y = core_layers.dense(
|
||||
x, 1, name='my_dense',
|
||||
kernel_initializer=init_ops.ones_initializer())
|
||||
self.assertAllEqual(y, [[2.0]])
|
||||
self.assertEqual(len(container.variables()), 2)
|
||||
# Recreate the layer to test reuse.
|
||||
with container.as_default():
|
||||
core_layers.dense(
|
||||
x, 1, name='my_dense',
|
||||
kernel_initializer=init_ops.ones_initializer())
|
||||
self.assertEqual(len(container.variables()), 2)
|
||||
container = variable_scope.EagerVariableStore()
|
||||
x = constant_op.constant([[2.0]])
|
||||
with container.as_default():
|
||||
y = core_layers.dense(
|
||||
x, 1, name='my_dense',
|
||||
kernel_initializer=init_ops.ones_initializer())
|
||||
self.assertAllEqual(y, [[2.0]])
|
||||
self.assertEqual(len(container.variables()), 2)
|
||||
# Recreate the layer to test reuse.
|
||||
with container.as_default():
|
||||
core_layers.dense(
|
||||
x, 1, name='my_dense',
|
||||
kernel_initializer=init_ops.ones_initializer())
|
||||
self.assertEqual(len(container.variables()), 2)
|
||||
|
||||
def testFunctionalDenseWithCustomGetter(self):
|
||||
called = [0]
|
||||
|
@ -18,15 +18,16 @@ 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.eager import context
|
||||
from tensorflow.python.keras import backend as K
|
||||
from tensorflow.python.keras import combinations
|
||||
from tensorflow.python.keras import metrics
|
||||
from tensorflow.python.platform import test
|
||||
|
||||
|
||||
class KerasFunctionalMetricsTest(test.TestCase):
|
||||
class KerasFunctionalMetricsTest(test.TestCase, parameterized.TestCase):
|
||||
|
||||
def test_metrics(self):
|
||||
with self.cached_session():
|
||||
@ -68,21 +69,21 @@ class KerasFunctionalMetricsTest(test.TestCase):
|
||||
y_pred = K.variable(np.random.random((6, 7)))
|
||||
self.assertEqual(K.eval(metric(y_true, y_pred)).shape, (6,))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_sparse_categorical_accuracy_eager(self):
|
||||
"""Tests that ints passed in via Eager return results. See b/113504761."""
|
||||
with context.eager_mode():
|
||||
metric = metrics.sparse_categorical_accuracy
|
||||
y_true = np.arange(6).reshape([6, 1])
|
||||
y_pred = np.arange(36).reshape([6, 6])
|
||||
self.assertAllEqual(metric(y_true, y_pred), [0., 0., 0., 0., 0., 1.])
|
||||
metric = metrics.sparse_categorical_accuracy
|
||||
y_true = np.arange(6).reshape([6, 1])
|
||||
y_pred = np.arange(36).reshape([6, 6])
|
||||
self.assertAllEqual(metric(y_true, y_pred), [0., 0., 0., 0., 0., 1.])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_sparse_categorical_accuracy_float_eager(self):
|
||||
"""Tests that floats passed in via Eager return results. See b/113504761."""
|
||||
with context.eager_mode():
|
||||
metric = metrics.sparse_categorical_accuracy
|
||||
y_true = np.arange(6, dtype=np.float32).reshape([6, 1])
|
||||
y_pred = np.arange(36).reshape([6, 6])
|
||||
self.assertAllEqual(metric(y_true, y_pred), [0., 0., 0., 0., 0., 1.])
|
||||
metric = metrics.sparse_categorical_accuracy
|
||||
y_true = np.arange(6, dtype=np.float32).reshape([6, 1])
|
||||
y_pred = np.arange(36).reshape([6, 6])
|
||||
self.assertAllEqual(metric(y_true, y_pred), [0., 0., 0., 0., 0., 1.])
|
||||
|
||||
def test_sparse_top_k_categorical_accuracy(self):
|
||||
with self.cached_session():
|
||||
|
@ -25,7 +25,6 @@ import os
|
||||
from absl.testing import parameterized
|
||||
import numpy as np
|
||||
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.eager import def_function
|
||||
from tensorflow.python.eager import function as eager_function
|
||||
from tensorflow.python.framework import constant_op
|
||||
@ -1487,47 +1486,47 @@ class MeanTensorTest(test.TestCase, parameterized.TestCase):
|
||||
self.assertAllClose(self.evaluate(m.count), [1, 1])
|
||||
self.assertAllClose(self.evaluate(call_metric([20, 2])), [60, 21])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
def test_in_keras_model(self):
|
||||
with context.eager_mode():
|
||||
class ModelWithMetric(Model):
|
||||
class ModelWithMetric(Model):
|
||||
|
||||
def __init__(self):
|
||||
super(ModelWithMetric, self).__init__()
|
||||
self.dense1 = layers.Dense(
|
||||
3, activation='relu', kernel_initializer='ones')
|
||||
self.dense2 = layers.Dense(
|
||||
1, activation='sigmoid', kernel_initializer='ones')
|
||||
self.mean_tensor = metrics.MeanTensor()
|
||||
def __init__(self):
|
||||
super(ModelWithMetric, self).__init__()
|
||||
self.dense1 = layers.Dense(
|
||||
3, activation='relu', kernel_initializer='ones')
|
||||
self.dense2 = layers.Dense(
|
||||
1, activation='sigmoid', kernel_initializer='ones')
|
||||
self.mean_tensor = metrics.MeanTensor()
|
||||
|
||||
def call(self, x):
|
||||
x = self.dense1(x)
|
||||
x = self.dense2(x)
|
||||
self.mean_tensor(self.dense1.kernel)
|
||||
return x
|
||||
def call(self, x):
|
||||
x = self.dense1(x)
|
||||
x = self.dense2(x)
|
||||
self.mean_tensor(self.dense1.kernel)
|
||||
return x
|
||||
|
||||
model = ModelWithMetric()
|
||||
model.compile(
|
||||
loss='mae',
|
||||
optimizer='rmsprop',
|
||||
run_eagerly=True)
|
||||
model = ModelWithMetric()
|
||||
model.compile(
|
||||
loss='mae',
|
||||
optimizer='rmsprop',
|
||||
run_eagerly=True)
|
||||
|
||||
x = np.ones((100, 4))
|
||||
y = np.zeros((100, 1))
|
||||
model.evaluate(x, y, batch_size=50)
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.result()),
|
||||
np.ones((4, 3)))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.total),
|
||||
np.full((4, 3), 2))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.count),
|
||||
np.full((4, 3), 2))
|
||||
x = np.ones((100, 4))
|
||||
y = np.zeros((100, 1))
|
||||
model.evaluate(x, y, batch_size=50)
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.result()),
|
||||
np.ones((4, 3)))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.total),
|
||||
np.full((4, 3), 2))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.count),
|
||||
np.full((4, 3), 2))
|
||||
|
||||
model.evaluate(x, y, batch_size=25)
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.result()),
|
||||
np.ones((4, 3)))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.total),
|
||||
np.full((4, 3), 4))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.count),
|
||||
np.full((4, 3), 4))
|
||||
model.evaluate(x, y, batch_size=25)
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.result()),
|
||||
np.ones((4, 3)))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.total),
|
||||
np.full((4, 3), 4))
|
||||
self.assertAllClose(self.evaluate(model.mean_tensor.count),
|
||||
np.full((4, 3), 4))
|
||||
|
||||
|
||||
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
|
||||
|
@ -189,22 +189,24 @@ class PolicyTest(test.TestCase, parameterized.TestCase):
|
||||
|
||||
@testing_utils.enable_v2_dtype_behavior
|
||||
def test_device_compatibility_warning(self):
|
||||
with context.eager_mode():
|
||||
device_compatibility_check._logged_compatibility_check = False
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest('Run in eager mode only.')
|
||||
|
||||
device_compatibility_check._logged_compatibility_check = False
|
||||
with test.mock.patch.object(tf_logging, 'warn') as mock_warn:
|
||||
mp_policy.Policy('mixed_float16')
|
||||
if config_module.list_physical_devices('GPU'):
|
||||
mock_warn.assert_not_called()
|
||||
else:
|
||||
self.assertRegex(
|
||||
mock_warn.call_args[0][0],
|
||||
r'Mixed precision compatibility check \(mixed_float16\): WARNING.*')
|
||||
|
||||
if config_module.list_physical_devices('GPU'):
|
||||
# Assert message is only logged once
|
||||
with test.mock.patch.object(tf_logging, 'warn') as mock_warn:
|
||||
mp_policy.Policy('mixed_float16')
|
||||
if config_module.list_physical_devices('GPU'):
|
||||
mock_warn.assert_not_called()
|
||||
else:
|
||||
self.assertRegex(
|
||||
mock_warn.call_args[0][0],
|
||||
r'Mixed precision compatibility check \(mixed_float16\): WARNING.*')
|
||||
|
||||
if config_module.list_physical_devices('GPU'):
|
||||
# Assert message is only logged once
|
||||
with test.mock.patch.object(tf_logging, 'warn') as mock_warn:
|
||||
mp_policy.Policy('mixed_float16')
|
||||
mock_warn.assert_not_called()
|
||||
mock_warn.assert_not_called()
|
||||
|
||||
@testing_utils.enable_v2_dtype_behavior
|
||||
def test_policy_scope(self):
|
||||
|
@ -148,9 +148,9 @@ class AdadeltaOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
def testResourceBasic(self):
|
||||
self.doTestBasic(use_resource=True)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testBasicCallableParams(self):
|
||||
with context.eager_mode():
|
||||
self.doTestBasic(use_resource=True, use_callable_params=True)
|
||||
self.doTestBasic(use_resource=True, use_callable_params=True)
|
||||
|
||||
def testMinimizeSparseResourceVariable(self):
|
||||
# TODO(tanzheny, omalleyt): Fix test in eager mode.
|
||||
|
@ -118,9 +118,9 @@ class AdagradOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
def testBasic(self):
|
||||
self.doTestBasic()
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testBasicCallableParams(self):
|
||||
with context.eager_mode():
|
||||
self.doTestBasic(use_callable_params=True)
|
||||
self.doTestBasic(use_callable_params=True)
|
||||
|
||||
def testBasicWithLearningRateDecay(self):
|
||||
for dtype in _DATA_TYPES:
|
||||
|
@ -254,9 +254,9 @@ class AdamOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
def testResourceBasic(self):
|
||||
self.doTestBasic()
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testBasicCallableParams(self):
|
||||
with context.eager_mode():
|
||||
self.doTestBasic(use_callable_params=True)
|
||||
self.doTestBasic(use_callable_params=True)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
def testBasicWithAmsgrad(self):
|
||||
@ -525,16 +525,16 @@ class AdamOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
self.assertAllCloseAccordingToType(var0_np, self.evaluate(var0))
|
||||
self.assertAllCloseAccordingToType(var1_np, self.evaluate(var1))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testSlotsUniqueEager(self):
|
||||
with context.eager_mode():
|
||||
v1 = variables.Variable(1.)
|
||||
v2 = variables.Variable(1.)
|
||||
opt = adam.Adam(1.)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and two unique slot variables for v1 and v2.
|
||||
self.assertEqual(5, len(set(v.ref() for v in opt.variables())))
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
v1 = variables.Variable(1.)
|
||||
v2 = variables.Variable(1.)
|
||||
opt = adam.Adam(1.)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and two unique slot variables for v1 and v2.
|
||||
self.assertLen(set(v.ref() for v in opt.variables()), 5)
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
|
||||
def testSetWeightsFromV1AdamWithoutMinimize(self):
|
||||
keras_v1_adam = optimizers.Adam()
|
||||
@ -708,9 +708,9 @@ class NonFusedAdamOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
def testResourceBasic(self):
|
||||
self.doTestBasic()
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testBasicCallableParams(self):
|
||||
with context.eager_mode():
|
||||
self.doTestBasic(use_callable_params=True)
|
||||
self.doTestBasic(use_callable_params=True)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
def testBasicWithAmsgrad(self):
|
||||
|
@ -350,14 +350,14 @@ class AdamaxOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
self.assertAllCloseAccordingToType(var0_np, var0)
|
||||
self.assertAllCloseAccordingToType(var1_np, var1)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testSlotsUniqueEager(self):
|
||||
with context.eager_mode():
|
||||
v1 = variables.Variable(1.)
|
||||
v2 = variables.Variable(1.)
|
||||
opt = adamax.Adamax(1.)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and two unique slot variables for v1 and v2.
|
||||
self.assertEqual(5, len({id(v) for v in opt.variables()}))
|
||||
v1 = variables.Variable(1.)
|
||||
v2 = variables.Variable(1.)
|
||||
opt = adamax.Adamax(1.)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and two unique slot variables for v1 and v2.
|
||||
self.assertLen({id(v) for v in opt.variables()}, 5)
|
||||
|
||||
def testConstructAdamaxWithLR(self):
|
||||
opt = adamax.Adamax(lr=1.0)
|
||||
|
@ -258,25 +258,25 @@ class GradientDescentOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
self.assertAllCloseAccordingToType(
|
||||
[[3.0], [4.0 - 3.0 * 0.01 - 2.0 * 0.01]], self.evaluate(var1))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testCapturingInDefunWhileExecutingEagerly(self):
|
||||
with context.eager_mode():
|
||||
optimizer = gradient_descent.SGD(1.0)
|
||||
optimizer = gradient_descent.SGD(1.0)
|
||||
|
||||
def step():
|
||||
self.v = variables.Variable(1.0)
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = self.v**2
|
||||
grad = tape.gradient(loss, self.v)
|
||||
optimizer.apply_gradients([(grad, self.v)])
|
||||
return self.v.read_value()
|
||||
def step():
|
||||
self.v = variables.Variable(1.0)
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = self.v**2
|
||||
grad = tape.gradient(loss, self.v)
|
||||
optimizer.apply_gradients([(grad, self.v)])
|
||||
return self.v.read_value()
|
||||
|
||||
compiled_step = function.defun(step)
|
||||
compiled_step = function.defun(step)
|
||||
|
||||
self.assertEqual(float(step()), -1.0)
|
||||
self.assertEqual(float(compiled_step()), -1.0)
|
||||
# This shouldn't fail; in particular, the learning rate tensor should
|
||||
# be an EagerTensor once again, not a graph Tensor.
|
||||
self.assertEqual(float(step()), -1.0)
|
||||
self.assertEqual(float(step()), -1.0)
|
||||
self.assertEqual(float(compiled_step()), -1.0)
|
||||
# This shouldn't fail; in particular, the learning rate tensor should
|
||||
# be an EagerTensor once again, not a graph Tensor.
|
||||
self.assertEqual(float(step()), -1.0)
|
||||
|
||||
def testConstructSGDWithLR(self):
|
||||
opt = gradient_descent.SGD(lr=1.0)
|
||||
|
@ -43,9 +43,6 @@ def _maybe_serialized(lr_decay, serialize_and_deserialize):
|
||||
return lr_decay
|
||||
|
||||
|
||||
# @parameterized.named_parameters(
|
||||
# ("NotSerialized", False),
|
||||
# ("Serialized", True))
|
||||
@combinations.generate(combinations.combine(serialize=[False, True],
|
||||
mode=["graph", "eager"]))
|
||||
class LRDecayTestV2(test_util.TensorFlowTestCase, parameterized.TestCase):
|
||||
@ -123,24 +120,26 @@ class LRDecayTestV2(test_util.TensorFlowTestCase, parameterized.TestCase):
|
||||
self.assertAllClose(self.evaluate(decayed_lr(x)), 0.001, 1e-6)
|
||||
|
||||
def testPiecewiseFunction(self, serialize):
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest("Run on eager mode only.")
|
||||
|
||||
del serialize
|
||||
with context.eager_mode():
|
||||
v = variables.Variable(1.)
|
||||
def loss_fn():
|
||||
return v * v
|
||||
learning_rate = learning_rate_schedule.PiecewiseConstantDecay(
|
||||
[1.], [1., 0.1])
|
||||
opt = gradient_descent.SGD(learning_rate=learning_rate)
|
||||
v = variables.Variable(1.)
|
||||
def loss_fn():
|
||||
return v * v
|
||||
learning_rate = learning_rate_schedule.PiecewiseConstantDecay(
|
||||
[1.], [1., 0.1])
|
||||
opt = gradient_descent.SGD(learning_rate=learning_rate)
|
||||
|
||||
@def_function.function
|
||||
def minimize():
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = loss_fn()
|
||||
g = tape.gradient(loss, [v])
|
||||
opt.apply_gradients(list(zip(g, [v])))
|
||||
@def_function.function
|
||||
def minimize():
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = loss_fn()
|
||||
g = tape.gradient(loss, [v])
|
||||
opt.apply_gradients(list(zip(g, [v])))
|
||||
|
||||
minimize()
|
||||
self.assertAllEqual(v.read_value(), -1.0)
|
||||
minimize()
|
||||
self.assertAllEqual(v.read_value(), -1.0)
|
||||
|
||||
def testPiecewiseConstantEdgeCases(self, serialize):
|
||||
# Test casting boundaries from int32 to int64.
|
||||
|
@ -872,62 +872,60 @@ class OptimizersCompatibilityTest(keras_parameterized.TestCase):
|
||||
|
||||
# Note: These tests are kept in a separate class to avoid bugs in some
|
||||
# distributions of Python that break AutoGraph which is used by tf.function.
|
||||
class OptimizerWithFunctionTest(test.TestCase):
|
||||
@combinations.generate(combinations.combine(mode=['eager']))
|
||||
class OptimizerWithFunctionTest(test.TestCase, parameterized.TestCase):
|
||||
|
||||
def testBasic(self):
|
||||
with context.eager_mode():
|
||||
var = variables.Variable([1.0, 2.0], dtype=dtypes.float32)
|
||||
loss = lambda: 3 * var
|
||||
opt = adam.Adam(learning_rate=1.0)
|
||||
var = variables.Variable([1.0, 2.0], dtype=dtypes.float32)
|
||||
loss = lambda: 3 * var
|
||||
opt = adam.Adam(learning_rate=1.0)
|
||||
|
||||
@def_function.function
|
||||
def fn():
|
||||
opt.minimize(loss, [var])
|
||||
return var
|
||||
@def_function.function
|
||||
def fn():
|
||||
opt.minimize(loss, [var])
|
||||
return var
|
||||
|
||||
self.assertAllClose([0., 1.], fn(), atol=1e-4)
|
||||
self.assertAllClose([-1, 0.], fn(), atol=1e-4)
|
||||
self.assertAllClose([0., 1.], fn(), atol=1e-4)
|
||||
self.assertAllClose([-1, 0.], fn(), atol=1e-4)
|
||||
|
||||
def testVarKeyWithVarCreatedInEager(self):
|
||||
with context.eager_mode():
|
||||
a = variables.Variable([1., 2.], name='var')
|
||||
b = variables.Variable([1.], name='var')
|
||||
a = variables.Variable([1., 2.], name='var')
|
||||
b = variables.Variable([1.], name='var')
|
||||
|
||||
@test_util.also_run_as_tf_function
|
||||
def var_key_test():
|
||||
self.assertFalse(a._in_graph_mode)
|
||||
self.assertFalse(b._in_graph_mode)
|
||||
var_key_a = optimizer_v2._var_key(a)
|
||||
self.assertStartsWith(var_key_a, 'var_')
|
||||
var_key_b = optimizer_v2._var_key(b)
|
||||
self.assertStartsWith(var_key_b, 'var_')
|
||||
self.assertNotEquals(var_key_a, var_key_b)
|
||||
@test_util.also_run_as_tf_function
|
||||
def var_key_test():
|
||||
self.assertFalse(a._in_graph_mode)
|
||||
self.assertFalse(b._in_graph_mode)
|
||||
var_key_a = optimizer_v2._var_key(a)
|
||||
self.assertStartsWith(var_key_a, 'var_')
|
||||
var_key_b = optimizer_v2._var_key(b)
|
||||
self.assertStartsWith(var_key_b, 'var_')
|
||||
self.assertNotEqual(var_key_a, var_key_b)
|
||||
|
||||
var_key_test()
|
||||
var_key_test()
|
||||
|
||||
def testLearningRateDecayUsedInTwoFunctions(self):
|
||||
with context.eager_mode():
|
||||
a = variables.Variable([1., 2.], name='var')
|
||||
b = variables.Variable([1.], name='var')
|
||||
a = variables.Variable([1., 2.], name='var')
|
||||
b = variables.Variable([1.], name='var')
|
||||
|
||||
learning_rate_decay = learning_rate_schedule.InverseTimeDecay(
|
||||
0.5, decay_steps=1.0, decay_rate=0.5)
|
||||
opt = adam.Adam(learning_rate=learning_rate_decay)
|
||||
loss_a = lambda: 3 * a
|
||||
loss_b = lambda: 2 * b
|
||||
learning_rate_decay = learning_rate_schedule.InverseTimeDecay(
|
||||
0.5, decay_steps=1.0, decay_rate=0.5)
|
||||
opt = adam.Adam(learning_rate=learning_rate_decay)
|
||||
loss_a = lambda: 3 * a
|
||||
loss_b = lambda: 2 * b
|
||||
|
||||
@def_function.function
|
||||
def fn_a():
|
||||
opt.minimize(loss_a, [a])
|
||||
return a
|
||||
@def_function.function
|
||||
def fn_a():
|
||||
opt.minimize(loss_a, [a])
|
||||
return a
|
||||
|
||||
@def_function.function
|
||||
def fn_b():
|
||||
opt.minimize(loss_b, [b])
|
||||
return b
|
||||
@def_function.function
|
||||
def fn_b():
|
||||
opt.minimize(loss_b, [b])
|
||||
return b
|
||||
|
||||
fn_a()
|
||||
fn_b()
|
||||
fn_a()
|
||||
fn_b()
|
||||
|
||||
|
||||
_NUM_LEARNERS = 50
|
||||
|
@ -25,7 +25,6 @@ import math
|
||||
from absl.testing import parameterized
|
||||
import numpy as np
|
||||
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.framework import constant_op
|
||||
from tensorflow.python.framework import dtypes
|
||||
from tensorflow.python.framework import ops
|
||||
@ -59,7 +58,7 @@ _TESTPARAMS = [
|
||||
]
|
||||
|
||||
|
||||
class RMSpropOptimizerTest(test.TestCase):
|
||||
class RMSpropOptimizerTest(test.TestCase, parameterized.TestCase):
|
||||
|
||||
def _rmsprop_update_numpy(self, var, g, mg, rms, mom, lr, rho, momentum,
|
||||
epsilon, centered):
|
||||
@ -459,54 +458,54 @@ class RMSpropOptimizerTest(test.TestCase):
|
||||
self.assertAllCloseAccordingToType(var0_np, self.evaluate(var0))
|
||||
self.assertAllCloseAccordingToType(var1_np, self.evaluate(var1))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testCallableParams(self):
|
||||
with context.eager_mode():
|
||||
for dtype in _DATA_TYPES:
|
||||
var0 = variables.Variable([1.0, 2.0], dtype=dtype)
|
||||
var1 = variables.Variable([3.0, 4.0], dtype=dtype)
|
||||
grads0 = constant_op.constant([0.1, 0.1], dtype=dtype)
|
||||
grads1 = constant_op.constant([0.01, 0.01], dtype=dtype)
|
||||
for dtype in _DATA_TYPES:
|
||||
var0 = variables.Variable([1.0, 2.0], dtype=dtype)
|
||||
var1 = variables.Variable([3.0, 4.0], dtype=dtype)
|
||||
grads0 = constant_op.constant([0.1, 0.1], dtype=dtype)
|
||||
grads1 = constant_op.constant([0.01, 0.01], dtype=dtype)
|
||||
|
||||
learning_rate = lambda: 2.0
|
||||
rho = lambda: 0.9
|
||||
momentum = lambda: 0.0
|
||||
epsilon = 1.0
|
||||
opt = rmsprop.RMSprop(learning_rate, rho, momentum, epsilon)
|
||||
learning_rate = lambda: 2.0
|
||||
rho = lambda: 0.9
|
||||
momentum = lambda: 0.0
|
||||
epsilon = 1.0
|
||||
opt = rmsprop.RMSprop(learning_rate, rho, momentum, epsilon)
|
||||
|
||||
# Fetch params to validate initial values
|
||||
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
|
||||
self.assertAllClose([3.0, 4.0], self.evaluate(var1))
|
||||
# Step 1: the rms accumulators where 1. So we should see a normal
|
||||
# update: v -= grad * learning_rate
|
||||
opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
|
||||
# Check the parameters.
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
1.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0)),
|
||||
2.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0))
|
||||
]), self.evaluate(var0))
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
3.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0)),
|
||||
4.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0))
|
||||
]), self.evaluate(var1))
|
||||
# Step 2: the root mean square accumulators contain the previous update.
|
||||
opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
|
||||
# Check the parameters.
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
1.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0)) -
|
||||
(0.1 * 2.0 / math.sqrt(0.001 * 0.9 + 0.001 + 1.0)),
|
||||
2.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0)) -
|
||||
(0.1 * 2.0 / math.sqrt(0.001 * 0.9 + 0.001 + 1.0))
|
||||
]), self.evaluate(var0))
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
3.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0)) -
|
||||
(0.01 * 2.0 / math.sqrt(0.00001 * 0.9 + 1e-5 + 1.0)),
|
||||
4.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0)) -
|
||||
(0.01 * 2.0 / math.sqrt(0.00001 * 0.9 + 1e-5 + 1.0))
|
||||
]), self.evaluate(var1))
|
||||
# Fetch params to validate initial values
|
||||
self.assertAllClose([1.0, 2.0], self.evaluate(var0))
|
||||
self.assertAllClose([3.0, 4.0], self.evaluate(var1))
|
||||
# Step 1: the rms accumulators where 1. So we should see a normal
|
||||
# update: v -= grad * learning_rate
|
||||
opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
|
||||
# Check the parameters.
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
1.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0)),
|
||||
2.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0))
|
||||
]), self.evaluate(var0))
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
3.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0)),
|
||||
4.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0))
|
||||
]), self.evaluate(var1))
|
||||
# Step 2: the root mean square accumulators contain the previous update.
|
||||
opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
|
||||
# Check the parameters.
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
1.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0)) -
|
||||
(0.1 * 2.0 / math.sqrt(0.001 * 0.9 + 0.001 + 1.0)),
|
||||
2.0 - (0.1 * 2.0 / math.sqrt(0.001 + 1.0)) -
|
||||
(0.1 * 2.0 / math.sqrt(0.001 * 0.9 + 0.001 + 1.0))
|
||||
]), self.evaluate(var0))
|
||||
self.assertAllCloseAccordingToType(
|
||||
np.array([
|
||||
3.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0)) -
|
||||
(0.01 * 2.0 / math.sqrt(0.00001 * 0.9 + 1e-5 + 1.0)),
|
||||
4.0 - (0.01 * 2.0 / math.sqrt(0.00001 + 1.0)) -
|
||||
(0.01 * 2.0 / math.sqrt(0.00001 * 0.9 + 1e-5 + 1.0))
|
||||
]), self.evaluate(var1))
|
||||
|
||||
def testConstructRMSpropWithLR(self):
|
||||
opt = rmsprop.RMSprop(lr=1.0)
|
||||
@ -521,31 +520,31 @@ class RMSpropOptimizerTest(test.TestCase):
|
||||
self.assertAllClose(self.evaluate(opt_2.lr), (1.0))
|
||||
self.assertAllClose(self.evaluate(opt_3.lr), (0.1))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testSlotsUniqueEager(self):
|
||||
with context.eager_mode():
|
||||
v1 = variables.Variable(1.)
|
||||
v2 = variables.Variable(1.)
|
||||
v1 = variables.Variable(1.)
|
||||
v2 = variables.Variable(1.)
|
||||
|
||||
opt = rmsprop.RMSprop(1., momentum=0., centered=False)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and one unique slot variable for v1 and v2.
|
||||
self.assertEqual(3, len(set({id(v) for v in opt.variables()})))
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
opt = rmsprop.RMSprop(1., momentum=0., centered=False)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and one unique slot variable for v1 and v2.
|
||||
self.assertLen(set({id(v) for v in opt.variables()}), 3)
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
|
||||
opt = rmsprop.RMSprop(learning_rate=1., momentum=0.2, centered=False)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and two unique slot variables for v1 and v2.
|
||||
self.assertEqual(5, len(set({id(v) for v in opt.variables()})))
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
opt = rmsprop.RMSprop(learning_rate=1., momentum=0.2, centered=False)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and two unique slot variables for v1 and v2.
|
||||
self.assertLen(set({id(v) for v in opt.variables()}), 5)
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
|
||||
opt = rmsprop.RMSprop(learning_rate=1., momentum=0.2, centered=True)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and three unique slot variables for v1 and v2
|
||||
self.assertEqual(7, len(set({id(v) for v in opt.variables()})))
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
opt = rmsprop.RMSprop(learning_rate=1., momentum=0.2, centered=True)
|
||||
opt.minimize(lambda: v1 + v2, var_list=[v1, v2])
|
||||
# There should be iteration, and three unique slot variables for v1 and v2
|
||||
self.assertLen(set({id(v) for v in opt.variables()}), 7)
|
||||
self.assertEqual(
|
||||
self.evaluate(opt.variables()[0]), self.evaluate(opt.iterations))
|
||||
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
|
@ -113,68 +113,66 @@ class LinearModelTest(keras_parameterized.TestCase):
|
||||
indices = []
|
||||
values = []
|
||||
target = np.zeros((batch_size, 1))
|
||||
with context.eager_mode():
|
||||
for i in range(64):
|
||||
rand_int = np.random.randint(3)
|
||||
if rand_int == 0:
|
||||
indices.append((i, 0))
|
||||
val = np.random.uniform(low=-5, high=5)
|
||||
values.append(val)
|
||||
target[i] = 0.3 * val
|
||||
elif rand_int == 1:
|
||||
indices.append((i, 1))
|
||||
val = np.random.uniform(low=-5, high=5)
|
||||
values.append(val)
|
||||
target[i] = 0.2 * val
|
||||
else:
|
||||
indices.append((i, 0))
|
||||
indices.append((i, 1))
|
||||
val_1 = np.random.uniform(low=-5, high=5)
|
||||
val_2 = np.random.uniform(low=-5, high=5)
|
||||
values.append(val_1)
|
||||
values.append(val_2)
|
||||
target[i] = 0.3 * val_1 + 0.2 * val_2
|
||||
for i in range(64):
|
||||
rand_int = np.random.randint(3)
|
||||
if rand_int == 0:
|
||||
indices.append((i, 0))
|
||||
val = np.random.uniform(low=-5, high=5)
|
||||
values.append(val)
|
||||
target[i] = 0.3 * val
|
||||
elif rand_int == 1:
|
||||
indices.append((i, 1))
|
||||
val = np.random.uniform(low=-5, high=5)
|
||||
values.append(val)
|
||||
target[i] = 0.2 * val
|
||||
else:
|
||||
indices.append((i, 0))
|
||||
indices.append((i, 1))
|
||||
val_1 = np.random.uniform(low=-5, high=5)
|
||||
val_2 = np.random.uniform(low=-5, high=5)
|
||||
values.append(val_1)
|
||||
values.append(val_2)
|
||||
target[i] = 0.3 * val_1 + 0.2 * val_2
|
||||
|
||||
indices = np.asarray(indices)
|
||||
values = np.asarray(values)
|
||||
shape = constant_op.constant([batch_size, 2], dtype=dtypes.int64)
|
||||
inp = sparse_tensor.SparseTensor(indices, values, shape)
|
||||
model = linear.LinearModel(use_bias=False)
|
||||
opt = gradient_descent.SGD()
|
||||
for _ in range(20):
|
||||
with backprop.GradientTape() as t:
|
||||
output = model(inp)
|
||||
loss = backend.mean(losses.mean_squared_error(target, output))
|
||||
grads = t.gradient(loss, model.trainable_variables)
|
||||
grads_and_vars = zip(grads, model.trainable_variables)
|
||||
opt.apply_gradients(grads_and_vars)
|
||||
indices = np.asarray(indices)
|
||||
values = np.asarray(values)
|
||||
shape = constant_op.constant([batch_size, 2], dtype=dtypes.int64)
|
||||
inp = sparse_tensor.SparseTensor(indices, values, shape)
|
||||
model = linear.LinearModel(use_bias=False)
|
||||
opt = gradient_descent.SGD()
|
||||
for _ in range(20):
|
||||
with backprop.GradientTape() as t:
|
||||
output = model(inp)
|
||||
loss = backend.mean(losses.mean_squared_error(target, output))
|
||||
grads = t.gradient(loss, model.trainable_variables)
|
||||
grads_and_vars = zip(grads, model.trainable_variables)
|
||||
opt.apply_gradients(grads_and_vars)
|
||||
|
||||
# This test is an example for a regression on categorical inputs, i.e.,
|
||||
# the output is 0.4, 0.6, 0.9 when input is 'alpha', 'beta', 'gamma'
|
||||
# separately.
|
||||
def test_linear_model_with_feature_column(self):
|
||||
with context.eager_mode():
|
||||
vocab_list = ['alpha', 'beta', 'gamma']
|
||||
vocab_val = [0.4, 0.6, 0.9]
|
||||
data = np.random.choice(vocab_list, size=256)
|
||||
y = np.zeros_like(data, dtype=np.float32)
|
||||
for vocab, val in zip(vocab_list, vocab_val):
|
||||
indices = np.where(data == vocab)
|
||||
y[indices] = val + np.random.uniform(
|
||||
low=-0.01, high=0.01, size=indices[0].shape)
|
||||
cat_column = fc.categorical_column_with_vocabulary_list(
|
||||
key='symbol', vocabulary_list=vocab_list)
|
||||
ind_column = fc.indicator_column(cat_column)
|
||||
dense_feature_layer = dense_features_v2.DenseFeatures([ind_column])
|
||||
linear_model = linear.LinearModel(
|
||||
use_bias=False, kernel_initializer='zeros')
|
||||
combined = sequential.Sequential([dense_feature_layer, linear_model])
|
||||
opt = gradient_descent.SGD(learning_rate=0.1)
|
||||
combined.compile(opt, 'mse', [])
|
||||
combined.fit(x={'symbol': data}, y=y, batch_size=32, epochs=10)
|
||||
self.assertAllClose([[0.4], [0.6], [0.9]],
|
||||
combined.layers[1].dense_layers[0].kernel.numpy(),
|
||||
atol=0.01)
|
||||
vocab_list = ['alpha', 'beta', 'gamma']
|
||||
vocab_val = [0.4, 0.6, 0.9]
|
||||
data = np.random.choice(vocab_list, size=256)
|
||||
y = np.zeros_like(data, dtype=np.float32)
|
||||
for vocab, val in zip(vocab_list, vocab_val):
|
||||
indices = np.where(data == vocab)
|
||||
y[indices] = val + np.random.uniform(
|
||||
low=-0.01, high=0.01, size=indices[0].shape)
|
||||
cat_column = fc.categorical_column_with_vocabulary_list(
|
||||
key='symbol', vocabulary_list=vocab_list)
|
||||
ind_column = fc.indicator_column(cat_column)
|
||||
dense_feature_layer = dense_features_v2.DenseFeatures([ind_column])
|
||||
linear_model = linear.LinearModel(
|
||||
use_bias=False, kernel_initializer='zeros')
|
||||
combined = sequential.Sequential([dense_feature_layer, linear_model])
|
||||
opt = gradient_descent.SGD(learning_rate=0.1)
|
||||
combined.compile(opt, 'mse', [])
|
||||
combined.fit(x={'symbol': data}, y=y, batch_size=32, epochs=10)
|
||||
self.assertAllClose([[0.4], [0.6], [0.9]],
|
||||
combined.layers[1].dense_layers[0].kernel.numpy(),
|
||||
atol=0.01)
|
||||
|
||||
def test_config(self):
|
||||
linear_model = linear.LinearModel(units=3, use_bias=True)
|
||||
|
@ -20,7 +20,6 @@ from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
|
||||
from tensorflow.python.eager import context
|
||||
from tensorflow.python.feature_column import feature_column_v2 as fc
|
||||
from tensorflow.python.keras import keras_parameterized
|
||||
from tensorflow.python.keras import testing_utils
|
||||
@ -96,29 +95,28 @@ class WideDeepModelTest(keras_parameterized.TestCase):
|
||||
wide_deep_model.fit(inputs, output, epochs=5)
|
||||
|
||||
def test_wide_deep_model_with_multi_outputs(self):
|
||||
with context.eager_mode():
|
||||
inp = input_layer.Input(shape=(1,), name='linear')
|
||||
l = linear.LinearModel(units=2, use_bias=False)(inp)
|
||||
l1, l2 = array_ops.split(l, num_or_size_splits=2, axis=1)
|
||||
linear_model = training.Model(inp, [l1, l2])
|
||||
linear_model.set_weights([np.asarray([[0.5, 0.3]])])
|
||||
h = core.Dense(units=2, use_bias=False)(inp)
|
||||
h1, h2 = array_ops.split(h, num_or_size_splits=2, axis=1)
|
||||
dnn_model = training.Model(inp, [h1, h2])
|
||||
dnn_model.set_weights([np.asarray([[0.1, -0.5]])])
|
||||
wide_deep_model = wide_deep.WideDeepModel(linear_model, dnn_model)
|
||||
inp_np = np.asarray([[1.]])
|
||||
out1, out2 = wide_deep_model(inp_np)
|
||||
# output should be (0.5 + 0.1), and (0.3 - 0.5)
|
||||
self.assertAllClose([[0.6]], out1)
|
||||
self.assertAllClose([[-0.2]], out2)
|
||||
inp = input_layer.Input(shape=(1,), name='linear')
|
||||
l = linear.LinearModel(units=2, use_bias=False)(inp)
|
||||
l1, l2 = array_ops.split(l, num_or_size_splits=2, axis=1)
|
||||
linear_model = training.Model(inp, [l1, l2])
|
||||
linear_model.set_weights([np.asarray([[0.5, 0.3]])])
|
||||
h = core.Dense(units=2, use_bias=False)(inp)
|
||||
h1, h2 = array_ops.split(h, num_or_size_splits=2, axis=1)
|
||||
dnn_model = training.Model(inp, [h1, h2])
|
||||
dnn_model.set_weights([np.asarray([[0.1, -0.5]])])
|
||||
wide_deep_model = wide_deep.WideDeepModel(linear_model, dnn_model)
|
||||
inp_np = np.asarray([[1.]])
|
||||
out1, out2 = wide_deep_model(inp_np)
|
||||
# output should be (0.5 + 0.1), and (0.3 - 0.5)
|
||||
self.assertAllClose([[0.6]], out1)
|
||||
self.assertAllClose([[-0.2]], out2)
|
||||
|
||||
wide_deep_model = wide_deep.WideDeepModel(
|
||||
linear_model, dnn_model, activation='relu')
|
||||
out1, out2 = wide_deep_model(inp_np)
|
||||
# output should be relu((0.5 + 0.1)), and relu((0.3 - 0.5))
|
||||
self.assertAllClose([[0.6]], out1)
|
||||
self.assertAllClose([[0.]], out2)
|
||||
wide_deep_model = wide_deep.WideDeepModel(
|
||||
linear_model, dnn_model, activation='relu')
|
||||
out1, out2 = wide_deep_model(inp_np)
|
||||
# output should be relu((0.5 + 0.1)), and relu((0.3 - 0.5))
|
||||
self.assertAllClose([[0.6]], out1)
|
||||
self.assertAllClose([[0.]], out2)
|
||||
|
||||
def test_wide_deep_model_with_single_optimizer(self):
|
||||
linear_model = linear.LinearModel(units=1)
|
||||
|
@ -102,48 +102,46 @@ class TestAddLossCorrectness(keras_parameterized.TestCase):
|
||||
history = model.fit(self.x, batch_size=3, epochs=5)
|
||||
self.assertAllClose(history.history['loss'], [0., -.1, -.2, -.3, -.4], 1e-3)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_loss_on_model_ctl(self):
|
||||
with context.eager_mode():
|
||||
def get_model_and_train_step():
|
||||
inputs = Input(shape=(1,))
|
||||
targets = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model([inputs, targets], outputs)
|
||||
model.add_loss(MAE()(targets, outputs))
|
||||
model.add_loss(math_ops.reduce_mean(mae(targets, outputs)))
|
||||
return get_ctl_train_step(model)
|
||||
|
||||
def get_model_and_train_step():
|
||||
inputs = Input(shape=(1,))
|
||||
targets = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model([inputs, targets], outputs)
|
||||
model.add_loss(MAE()(targets, outputs))
|
||||
model.add_loss(math_ops.reduce_mean(mae(targets, outputs)))
|
||||
return get_ctl_train_step(model)
|
||||
train_step = get_model_and_train_step()
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
|
||||
train_step = get_model_and_train_step()
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
|
||||
train_step = def_function.function(get_model_and_train_step())
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
train_step = def_function.function(get_model_and_train_step())
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_loss_callable_on_model_ctl(self):
|
||||
with context.eager_mode():
|
||||
def get_model_and_train_step():
|
||||
inputs = Input(shape=(1,))
|
||||
targets = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model([inputs, targets], outputs)
|
||||
|
||||
def get_model_and_train_step():
|
||||
inputs = Input(shape=(1,))
|
||||
targets = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model([inputs, targets], outputs)
|
||||
def callable_loss():
|
||||
return math_ops.reduce_sum(model.weights)
|
||||
|
||||
def callable_loss():
|
||||
return math_ops.reduce_sum(model.weights)
|
||||
model.add_loss(callable_loss)
|
||||
return get_ctl_train_step(model)
|
||||
|
||||
model.add_loss(callable_loss)
|
||||
return get_ctl_train_step(model)
|
||||
train_step = get_model_and_train_step()
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [0., -0.05, -0.1, -0.15, -0.2], 1e-3)
|
||||
|
||||
train_step = get_model_and_train_step()
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [0., -0.05, -0.1, -0.15, -0.2], 1e-3)
|
||||
|
||||
train_step = def_function.function(get_model_and_train_step())
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [0., -0.05, -0.1, -0.15, -0.2], 1e-3)
|
||||
train_step = def_function.function(get_model_and_train_step())
|
||||
loss = [train_step(self.x, self.y) for _ in range(5)]
|
||||
self.assertAllClose(loss, [0., -0.05, -0.1, -0.15, -0.2], 1e-3)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
def test_loss_with_sample_weight_on_model_fit(self):
|
||||
@ -161,26 +159,25 @@ class TestAddLossCorrectness(keras_parameterized.TestCase):
|
||||
history = model.fit([self.x, self.y, self.w], batch_size=3, epochs=5)
|
||||
self.assertAllClose(history.history['loss'], [4., 3.6, 3.2, 2.8, 2.4], 1e-3)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_loss_with_sample_weight_on_model_ctl(self):
|
||||
with context.eager_mode():
|
||||
def get_model_and_train_step():
|
||||
inputs = Input(shape=(1,))
|
||||
targets = Input(shape=(1,))
|
||||
sw = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model([inputs, targets, sw], outputs)
|
||||
model.add_loss(MAE()(targets, outputs, sw))
|
||||
model.add_loss(math_ops.reduce_mean(sw * mae(targets, outputs)))
|
||||
return get_ctl_train_step(model)
|
||||
|
||||
def get_model_and_train_step():
|
||||
inputs = Input(shape=(1,))
|
||||
targets = Input(shape=(1,))
|
||||
sw = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model([inputs, targets, sw], outputs)
|
||||
model.add_loss(MAE()(targets, outputs, sw))
|
||||
model.add_loss(math_ops.reduce_mean(sw * mae(targets, outputs)))
|
||||
return get_ctl_train_step(model)
|
||||
train_step = get_model_and_train_step()
|
||||
loss = [train_step(self.x, self.y, self.w) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
|
||||
train_step = get_model_and_train_step()
|
||||
loss = [train_step(self.x, self.y, self.w) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
|
||||
train_step = def_function.function(get_model_and_train_step())
|
||||
loss = [train_step(self.x, self.y, self.w) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
train_step = def_function.function(get_model_and_train_step())
|
||||
loss = [train_step(self.x, self.y, self.w) for _ in range(5)]
|
||||
self.assertAllClose(loss, [2., 1.8, 1.6, 1.4, 1.2], 1e-3)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
def test_loss_with_sample_weight_in_model_call(self):
|
||||
@ -429,27 +426,25 @@ class TestAddLossCorrectness(keras_parameterized.TestCase):
|
||||
self.assertEqual(len(model.get_losses_for(x4)), 2)
|
||||
self.assertEqual(len(model.get_losses_for(None)), 1)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_invalid_constant_input(self):
|
||||
with context.eager_mode():
|
||||
inputs = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model(inputs, outputs)
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
'Expected a symbolic Tensors or a callable for the loss value'):
|
||||
model.add_loss(1.)
|
||||
inputs = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model(inputs, outputs)
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
'Expected a symbolic Tensors or a callable for the loss value'):
|
||||
model.add_loss(1.)
|
||||
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
@keras_parameterized.run_all_keras_modes(always_skip_v1=True)
|
||||
def test_invalid_variable_input(self):
|
||||
with context.eager_mode():
|
||||
inputs = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model(inputs, outputs)
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
'Expected a symbolic Tensors or a callable for the loss value'):
|
||||
model.add_loss(model.weights[0])
|
||||
inputs = Input(shape=(1,))
|
||||
outputs = testing_utils.Bias()(inputs)
|
||||
model = Model(inputs, outputs)
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
'Expected a symbolic Tensors or a callable for the loss value'):
|
||||
model.add_loss(model.weights[0])
|
||||
|
||||
@keras_parameterized.run_all_keras_modes
|
||||
def test_add_entropy_loss_on_functional_model(self):
|
||||
|
@ -706,32 +706,33 @@ class CustomCallSignatureTests(test.TestCase, parameterized.TestCase):
|
||||
m.predict_on_batch(x)
|
||||
|
||||
def test_deepcopy(self):
|
||||
with context.eager_mode():
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest('Run in eager mode only.')
|
||||
|
||||
class MyModel(keras.Model):
|
||||
class MyModel(keras.Model):
|
||||
|
||||
def __init__(self):
|
||||
super(MyModel, self).__init__()
|
||||
self.my_variable = variables_lib.Variable(0.0, trainable=False)
|
||||
self.layer = keras.layers.Dense(4)
|
||||
def __init__(self):
|
||||
super(MyModel, self).__init__()
|
||||
self.my_variable = variables_lib.Variable(0.0, trainable=False)
|
||||
self.layer = keras.layers.Dense(4)
|
||||
|
||||
def call(self, obs):
|
||||
return self.layer(obs)
|
||||
def call(self, obs):
|
||||
return self.layer(obs)
|
||||
|
||||
model = MyModel()
|
||||
model.my_variable.assign_add(1.0)
|
||||
model = MyModel()
|
||||
model.my_variable.assign_add(1.0)
|
||||
|
||||
new_model = copy.deepcopy(model)
|
||||
self.assertEqual(model.my_variable.numpy(), 1.0)
|
||||
self.assertEqual(new_model.my_variable.numpy(), 1.0)
|
||||
new_model = copy.deepcopy(model)
|
||||
self.assertEqual(model.my_variable.numpy(), 1.0)
|
||||
self.assertEqual(new_model.my_variable.numpy(), 1.0)
|
||||
|
||||
model.my_variable.assign_add(1.0)
|
||||
self.assertEqual(model.my_variable.numpy(), 2.0)
|
||||
self.assertEqual(new_model.my_variable.numpy(), 1.0)
|
||||
model.my_variable.assign_add(1.0)
|
||||
self.assertEqual(model.my_variable.numpy(), 2.0)
|
||||
self.assertEqual(new_model.my_variable.numpy(), 1.0)
|
||||
|
||||
# Check that Trackable logic still works.
|
||||
self.assertLen(new_model.variables, 1)
|
||||
self.assertLen(new_model.layers, 1)
|
||||
# Check that Trackable logic still works.
|
||||
self.assertLen(new_model.variables, 1)
|
||||
self.assertLen(new_model.layers, 1)
|
||||
|
||||
def test_batch_counters_not_in_variables(self):
|
||||
|
||||
|
@ -177,21 +177,18 @@ class ListTests(keras_parameterized.TestCase):
|
||||
m2(m2.null_input())
|
||||
self.assertLen(m2.trainable_variables, 6)
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
def testUpdatesForwarded(self):
|
||||
with context.graph_mode():
|
||||
model = HasList()
|
||||
model_input = array_ops.ones([32, 2])
|
||||
model(model_input)
|
||||
model = HasList()
|
||||
model_input = array_ops.ones([32, 2])
|
||||
model(model_input)
|
||||
if context.executing_eagerly():
|
||||
self.assertEqual(0, len(model.updates))
|
||||
else:
|
||||
self.assertGreater(len(model.layers_with_updates[0].updates), 0)
|
||||
self.assertEqual(set(model.layers_with_updates[0].updates),
|
||||
set(model.updates))
|
||||
|
||||
with context.eager_mode():
|
||||
model = HasList()
|
||||
model_input = array_ops.ones([32, 2])
|
||||
model(model_input)
|
||||
self.assertEqual(0, len(model.updates))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
def testLossesForwarded(self):
|
||||
model = HasList()
|
||||
|
@ -100,13 +100,15 @@ class InterfaceTests(test.TestCase):
|
||||
checkpoint.save(os.path.join(self.get_temp_dir(), "ckpt"))
|
||||
|
||||
def testObjectMetadata(self):
|
||||
with context.eager_mode():
|
||||
checkpoint_directory = self.get_temp_dir()
|
||||
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
|
||||
dense = core.Dense(1)
|
||||
checkpoint = trackable_utils.Checkpoint(dense=dense)
|
||||
dense(constant_op.constant([[1.]]))
|
||||
save_path = checkpoint.save(checkpoint_prefix)
|
||||
if not context.executing_eagerly():
|
||||
self.skipTest("Run in eager mode only.")
|
||||
|
||||
checkpoint_directory = self.get_temp_dir()
|
||||
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
|
||||
dense = core.Dense(1)
|
||||
checkpoint = trackable_utils.Checkpoint(dense=dense)
|
||||
dense(constant_op.constant([[1.]]))
|
||||
save_path = checkpoint.save(checkpoint_prefix)
|
||||
|
||||
objects = trackable_utils.object_metadata(save_path)
|
||||
all_variable_names = []
|
||||
@ -382,30 +384,30 @@ class CheckpointingTests(keras_parameterized.TestCase):
|
||||
self.assertEqual(training_continuation + 1,
|
||||
self.evaluate(root.save_counter))
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testPartialRestoreWarningObject(self):
|
||||
with context.eager_mode():
|
||||
optimizer = adam.Adam(0.0)
|
||||
original_root = trackable_utils.Checkpoint(v1=variables_lib.Variable(2.),
|
||||
v2=variables_lib.Variable(3.),
|
||||
optimizer=optimizer)
|
||||
# Create a slot variable to save
|
||||
optimizer.minimize(original_root.v1.read_value, [original_root.v1])
|
||||
prefix = os.path.join(self.get_temp_dir(), "ckpt")
|
||||
save_path = original_root.save(prefix)
|
||||
partial_root = trackable_utils.Checkpoint(v1=variables_lib.Variable(0.))
|
||||
weak_partial_root = weakref.ref(partial_root)
|
||||
weak_v1 = weakref.ref(partial_root.v1)
|
||||
partial_root.restore(save_path)
|
||||
self.assertEqual(2., partial_root.v1.numpy())
|
||||
with test.mock.patch.object(logging, "warning") as mock_log:
|
||||
del partial_root
|
||||
self.assertIsNone(weak_partial_root())
|
||||
self.assertIsNone(weak_v1())
|
||||
messages = str(mock_log.call_args_list)
|
||||
self.assertIn("(root).v2'", messages)
|
||||
self.assertIn("(root).optimizer's state 'm' for (root).v1", messages)
|
||||
self.assertNotIn("(root).v1'", messages)
|
||||
self.assertIn("expect_partial()", messages)
|
||||
optimizer = adam.Adam(0.0)
|
||||
original_root = trackable_utils.Checkpoint(v1=variables_lib.Variable(2.),
|
||||
v2=variables_lib.Variable(3.),
|
||||
optimizer=optimizer)
|
||||
# Create a slot variable to save
|
||||
optimizer.minimize(original_root.v1.read_value, [original_root.v1])
|
||||
prefix = os.path.join(self.get_temp_dir(), "ckpt")
|
||||
save_path = original_root.save(prefix)
|
||||
partial_root = trackable_utils.Checkpoint(v1=variables_lib.Variable(0.))
|
||||
weak_partial_root = weakref.ref(partial_root)
|
||||
weak_v1 = weakref.ref(partial_root.v1)
|
||||
partial_root.restore(save_path)
|
||||
self.assertEqual(2., partial_root.v1.numpy())
|
||||
with test.mock.patch.object(logging, "warning") as mock_log:
|
||||
del partial_root
|
||||
self.assertIsNone(weak_partial_root())
|
||||
self.assertIsNone(weak_v1())
|
||||
messages = str(mock_log.call_args_list)
|
||||
self.assertIn("(root).v2'", messages)
|
||||
self.assertIn("(root).optimizer's state 'm' for (root).v1", messages)
|
||||
self.assertNotIn("(root).v1'", messages)
|
||||
self.assertIn("expect_partial()", messages)
|
||||
|
||||
# pylint: disable=cell-var-from-loop
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
@ -450,6 +452,7 @@ class CheckpointingTests(keras_parameterized.TestCase):
|
||||
self.evaluate(root.save_counter))
|
||||
# pylint: enable=cell-var-from-loop
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testAnonymousVarsInInit(self):
|
||||
|
||||
class Model(training.Model):
|
||||
@ -463,21 +466,20 @@ class CheckpointingTests(keras_parameterized.TestCase):
|
||||
def call(self, x):
|
||||
return x * self.w + self.b
|
||||
|
||||
with context.eager_mode():
|
||||
model = Model()
|
||||
optimizer = adam.Adam(learning_rate=0.05)
|
||||
checkpoint_directory = self.get_temp_dir()
|
||||
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
|
||||
checkpoint = trackable_utils.Checkpoint(
|
||||
model=model, optimizer=optimizer)
|
||||
for _ in range(2):
|
||||
checkpoint.save(checkpoint_prefix)
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = (constant_op.constant(1.)
|
||||
- model(constant_op.constant(1.))) ** 2
|
||||
grad = tape.gradient(loss, model.vars)
|
||||
optimizer.apply_gradients(
|
||||
[(g, v) for g, v in zip(grad, model.vars)])
|
||||
model = Model()
|
||||
optimizer = adam.Adam(learning_rate=0.05)
|
||||
checkpoint_directory = self.get_temp_dir()
|
||||
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
|
||||
checkpoint = trackable_utils.Checkpoint(
|
||||
model=model, optimizer=optimizer)
|
||||
for _ in range(2):
|
||||
checkpoint.save(checkpoint_prefix)
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = (constant_op.constant(1.)
|
||||
- model(constant_op.constant(1.))) ** 2
|
||||
grad = tape.gradient(loss, model.vars)
|
||||
optimizer.apply_gradients(
|
||||
[(g, v) for g, v in zip(grad, model.vars)])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
def testDeferredSlotRestoration(self):
|
||||
|
@ -470,6 +470,7 @@ class CheckpointingTests(keras_parameterized.TestCase):
|
||||
pass # Make sure we can use this as an op name if we prefix it.
|
||||
return named_variable.name
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["eager"]))
|
||||
def testAnonymousVarsInInit(self):
|
||||
|
||||
class Model(training.Model):
|
||||
@ -483,21 +484,20 @@ class CheckpointingTests(keras_parameterized.TestCase):
|
||||
def call(self, x):
|
||||
return x * self.w + self.b
|
||||
|
||||
with context.eager_mode():
|
||||
model = Model()
|
||||
optimizer = adam.AdamOptimizer(learning_rate=0.05)
|
||||
checkpoint_directory = self.get_temp_dir()
|
||||
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
|
||||
checkpoint = trackable_utils.Checkpoint(
|
||||
model=model, optimizer=optimizer)
|
||||
for _ in range(2):
|
||||
checkpoint.save(checkpoint_prefix)
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = (constant_op.constant(1.)
|
||||
- model(constant_op.constant(1.))) ** 2
|
||||
grad = tape.gradient(loss, model.vars)
|
||||
optimizer.apply_gradients(
|
||||
[(g, v) for g, v in zip(grad, model.vars)])
|
||||
model = Model()
|
||||
optimizer = adam.AdamOptimizer(learning_rate=0.05)
|
||||
checkpoint_directory = self.get_temp_dir()
|
||||
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
|
||||
checkpoint = trackable_utils.Checkpoint(
|
||||
model=model, optimizer=optimizer)
|
||||
for _ in range(2):
|
||||
checkpoint.save(checkpoint_prefix)
|
||||
with backprop.GradientTape() as tape:
|
||||
loss = (constant_op.constant(1.)
|
||||
- model(constant_op.constant(1.))) ** 2
|
||||
grad = tape.gradient(loss, model.vars)
|
||||
optimizer.apply_gradients(
|
||||
[(g, v) for g, v in zip(grad, model.vars)])
|
||||
|
||||
@combinations.generate(combinations.combine(mode=["graph", "eager"]))
|
||||
def test_initialize_if_not_restoring(self):
|
||||
|
Loading…
Reference in New Issue
Block a user