Merge pull request #44498 from jpodivin:logredef

PiperOrigin-RevId: 342186442
Change-Id: I898125b5e03203d1a9cf455ad8c20abf1fc6c537
This commit is contained in:
TensorFlower Gardener 2020-11-12 20:14:40 -08:00
commit b79e133965
2 changed files with 65 additions and 54 deletions

View File

@ -18,7 +18,6 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import logging
import sys import sys
import numpy as np import numpy as np
@ -72,10 +71,20 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
dataset = dataset.batch(10) dataset = dataset.batch(10)
# Call fit with validation data # Call fit with validation data
model.fit(dataset, epochs=1, steps_per_epoch=2, verbose=0, model.fit(
validation_data=dataset, validation_steps=2) dataset,
model.fit(dataset, epochs=1, steps_per_epoch=2, verbose=0, epochs=1,
validation_data=dataset, validation_steps=2) steps_per_epoch=2,
verbose=0,
validation_data=dataset,
validation_steps=2)
model.fit(
dataset,
epochs=1,
steps_per_epoch=2,
verbose=0,
validation_data=dataset,
validation_steps=2)
@keras_parameterized.run_with_all_model_types @keras_parameterized.run_with_all_model_types
@keras_parameterized.run_all_keras_modes @keras_parameterized.run_all_keras_modes
@ -101,14 +110,23 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
model.predict(dataset, steps=2) model.predict(dataset, steps=2)
# Test with validation data # Test with validation data
model.fit(dataset, epochs=1, steps_per_epoch=2, verbose=0, model.fit(
validation_data=dataset, validation_steps=2) dataset,
epochs=1,
steps_per_epoch=2,
verbose=0,
validation_data=dataset,
validation_steps=2)
# Test with validation split # Test with validation split
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
model.fit(dataset, model.fit(
epochs=1, steps_per_epoch=2, verbose=0, dataset,
validation_split=0.5, validation_steps=2) epochs=1,
steps_per_epoch=2,
verbose=0,
validation_split=0.5,
validation_steps=2)
# Test with sample weight. # Test with sample weight.
sample_weight = np.random.random((10,)) sample_weight = np.random.random((10,))
@ -124,8 +142,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
with self.assertRaisesRegex( with self.assertRaisesRegex(
ValueError, '(you should not specify a target)|' ValueError, '(you should not specify a target)|'
'(`y` argument is not supported when using dataset as input.)'): '(`y` argument is not supported when using dataset as input.)'):
model.fit(dataset, dataset, model.fit(dataset, dataset, epochs=1, steps_per_epoch=2, verbose=0)
epochs=1, steps_per_epoch=2, verbose=0)
# With an infinite dataset, `steps_per_epoch`/`steps` argument is required. # With an infinite dataset, `steps_per_epoch`/`steps` argument is required.
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -157,8 +174,8 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
output_e_np = np.random.random((10, 4)).astype(dtype=np.float32) output_e_np = np.random.random((10, 4)).astype(dtype=np.float32)
# Test with tuples # Test with tuples
dataset_tuple = dataset_ops.Dataset.from_tensor_slices(( dataset_tuple = dataset_ops.Dataset.from_tensor_slices(
(input_a_np, input_b_np), (output_d_np, output_e_np))) ((input_a_np, input_b_np), (output_d_np, output_e_np)))
dataset_tuple = dataset_tuple.repeat(100) dataset_tuple = dataset_tuple.repeat(100)
dataset_tuple = dataset_tuple.batch(10) dataset_tuple = dataset_tuple.batch(10)
@ -172,16 +189,15 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
else: else:
output_dict = {'dense': output_d_np, 'dropout': output_e_np} output_dict = {'dense': output_d_np, 'dropout': output_e_np}
dataset_dict = dataset_ops.Dataset.from_tensor_slices(( dataset_dict = dataset_ops.Dataset.from_tensor_slices(
input_dict, output_dict)) (input_dict, output_dict))
dataset_dict = dataset_dict.repeat(100) dataset_dict = dataset_dict.repeat(100)
dataset_dict = dataset_dict.batch(10) dataset_dict = dataset_dict.batch(10)
model.fit(dataset_dict, epochs=1, steps_per_epoch=2, verbose=1) model.fit(dataset_dict, epochs=1, steps_per_epoch=2, verbose=1)
model.evaluate(dataset_dict, steps=2, verbose=1) model.evaluate(dataset_dict, steps=2, verbose=1)
predict_dataset_dict = dataset_ops.Dataset.from_tensor_slices( predict_dataset_dict = dataset_ops.Dataset.from_tensor_slices(input_dict)
input_dict)
predict_dataset_dict = predict_dataset_dict.repeat(100) predict_dataset_dict = predict_dataset_dict.repeat(100)
predict_dataset_dict = predict_dataset_dict.batch(10) predict_dataset_dict = predict_dataset_dict.batch(10)
model.predict(predict_dataset_dict, steps=1) model.predict(predict_dataset_dict, steps=1)
@ -202,8 +218,8 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
inputs = np.zeros((10, 3), np.float32) inputs = np.zeros((10, 3), np.float32)
targets = np.zeros((10, 4), np.float32) targets = np.zeros((10, 4), np.float32)
sample_weights = np.ones((10), np.float32) sample_weights = np.ones((10), np.float32)
dataset = dataset_ops.Dataset.from_tensor_slices((inputs, targets, dataset = dataset_ops.Dataset.from_tensor_slices(
sample_weights)) (inputs, targets, sample_weights))
dataset = dataset.repeat(100) dataset = dataset.repeat(100)
dataset = dataset.batch(10) dataset = dataset.batch(10)
@ -216,7 +232,8 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
def test_dataset_with_sample_weights_correctness(self): def test_dataset_with_sample_weights_correctness(self):
x = keras.layers.Input(shape=(1,), name='input') x = keras.layers.Input(shape=(1,), name='input')
y = keras.layers.Dense( y = keras.layers.Dense(
1, kernel_initializer='ones', bias_initializer='zeros', name='dense')(x) 1, kernel_initializer='ones', bias_initializer='zeros', name='dense')(
x)
model = keras.Model(x, y) model = keras.Model(x, y)
optimizer = 'rmsprop' optimizer = 'rmsprop'
loss = 'mse' loss = 'mse'
@ -224,8 +241,8 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
inputs = np.array([[0], [1], [2], [3]], np.float32) inputs = np.array([[0], [1], [2], [3]], np.float32)
targets = np.array([[2], [4], [6], [8]], np.float32) targets = np.array([[2], [4], [6], [8]], np.float32)
sample_weights = np.array([0.25, 0.5, 0.75, 1], np.float32) sample_weights = np.array([0.25, 0.5, 0.75, 1], np.float32)
ds = dataset_ops.Dataset.from_tensor_slices((inputs, targets, ds = dataset_ops.Dataset.from_tensor_slices(
sample_weights)).batch(2) (inputs, targets, sample_weights)).batch(2)
result = model.evaluate(ds, verbose=1) result = model.evaluate(ds, verbose=1)
# The per sample loss is multipled by the corresponding sample weight. The # The per sample loss is multipled by the corresponding sample weight. The
# average of these weighted losses is the return value of the `evaluate` # average of these weighted losses is the return value of the `evaluate`
@ -255,6 +272,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
@keras_parameterized.run_all_keras_modes @keras_parameterized.run_all_keras_modes
def test_dataset_fit_correctness(self): def test_dataset_fit_correctness(self):
class SumLayer(keras.layers.Layer): class SumLayer(keras.layers.Layer):
def build(self, _): def build(self, _):
@ -265,9 +283,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
model = keras.Sequential([SumLayer(input_shape=(2,))]) model = keras.Sequential([SumLayer(input_shape=(2,))])
model.compile( model.compile(
'rmsprop', 'rmsprop', loss='mae', run_eagerly=testing_utils.should_run_eagerly())
loss='mae',
run_eagerly=testing_utils.should_run_eagerly())
inputs = np.zeros((40, 2), dtype=np.float32) inputs = np.zeros((40, 2), dtype=np.float32)
inputs[10:20, :] = 2 inputs[10:20, :] = 2
@ -280,9 +296,13 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
(inputs, targets)).batch(10) (inputs, targets)).batch(10)
val_dataset = dataset_ops.Dataset.from_tensor_slices( val_dataset = dataset_ops.Dataset.from_tensor_slices(
(inputs, targets)).batch(10) (inputs, targets)).batch(10)
history = model.fit(train_dataset, history = model.fit(
epochs=2, steps_per_epoch=2, verbose=1, train_dataset,
validation_data=val_dataset, validation_steps=2) epochs=2,
steps_per_epoch=2,
verbose=1,
validation_data=val_dataset,
validation_steps=2)
self.assertAllClose(history.history['loss'], self.assertAllClose(history.history['loss'],
[inputs[:20].sum() / 20, inputs[20:].sum() / 20]) [inputs[:20].sum() / 20, inputs[20:].sum() / 20])
# The validation dataset will be reset at the end of each validation run. # The validation dataset will be reset at the end of each validation run.
@ -294,8 +314,8 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
(inputs, targets)).batch(10) (inputs, targets)).batch(10)
val_dataset = dataset_ops.Dataset.from_tensor_slices( val_dataset = dataset_ops.Dataset.from_tensor_slices(
(inputs, targets)).batch(10) (inputs, targets)).batch(10)
history = model.fit(train_dataset, history = model.fit(
epochs=2, verbose=1, validation_data=val_dataset) train_dataset, epochs=2, verbose=1, validation_data=val_dataset)
self.assertAllClose( self.assertAllClose(
history.history['loss'], history.history['loss'],
[inputs.sum() / 40, inputs.sum() / 40]) [inputs.sum() / 40, inputs.sum() / 40])
@ -336,9 +356,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
def test_finite_dataset_known_cardinality_no_steps_arg(self): def test_finite_dataset_known_cardinality_no_steps_arg(self):
model = testing_utils.get_small_mlp(1, 4, input_dim=3) model = testing_utils.get_small_mlp(1, 4, input_dim=3)
model.compile( model.compile(
'rmsprop', 'rmsprop', 'mse', run_eagerly=testing_utils.should_run_eagerly())
'mse',
run_eagerly=testing_utils.should_run_eagerly())
inputs = np.zeros((100, 3), dtype=np.float32) inputs = np.zeros((100, 3), dtype=np.float32)
targets = np.random.randint(0, 4, size=100, dtype=np.int32) targets = np.random.randint(0, 4, size=100, dtype=np.int32)
@ -359,16 +377,15 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
def test_finite_dataset_unknown_cardinality_no_steps_arg(self): def test_finite_dataset_unknown_cardinality_no_steps_arg(self):
model = testing_utils.get_small_mlp(1, 4, input_dim=3) model = testing_utils.get_small_mlp(1, 4, input_dim=3)
model.compile( model.compile(
'rmsprop', 'rmsprop', 'mse', run_eagerly=testing_utils.should_run_eagerly())
'mse',
run_eagerly=testing_utils.should_run_eagerly())
inputs = np.zeros((100, 3), dtype=np.float32) inputs = np.zeros((100, 3), dtype=np.float32)
targets = np.random.randint(0, 4, size=100, dtype=np.int32) targets = np.random.randint(0, 4, size=100, dtype=np.int32)
dataset = dataset_ops.Dataset.from_tensor_slices((inputs, targets)) dataset = dataset_ops.Dataset.from_tensor_slices((inputs, targets))
dataset = dataset.filter(lambda x, y: True).batch(10) dataset = dataset.filter(lambda x, y: True).batch(10)
self.assertEqual(keras.backend.get_value(cardinality.cardinality(dataset)), self.assertEqual(
cardinality.UNKNOWN) keras.backend.get_value(cardinality.cardinality(dataset)),
cardinality.UNKNOWN)
batch_counter = BatchCounterCallback() batch_counter = BatchCounterCallback()
history = model.fit(dataset, epochs=2, verbose=1, callbacks=[batch_counter]) history = model.fit(dataset, epochs=2, verbose=1, callbacks=[batch_counter])
@ -398,9 +415,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
model = testing_utils.get_small_mlp(1, 4, input_dim=3) model = testing_utils.get_small_mlp(1, 4, input_dim=3)
model.compile( model.compile(
'rmsprop', 'rmsprop', 'mse', run_eagerly=testing_utils.should_run_eagerly())
'mse',
run_eagerly=testing_utils.should_run_eagerly())
inputs = np.zeros((100, 3), dtype=np.float32) inputs = np.zeros((100, 3), dtype=np.float32)
targets = np.random.randint(0, 4, size=100, dtype=np.int32) targets = np.random.randint(0, 4, size=100, dtype=np.int32)
@ -434,9 +449,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
def test_finite_dataset_unknown_cardinality_out_of_data(self): def test_finite_dataset_unknown_cardinality_out_of_data(self):
model = testing_utils.get_small_mlp(1, 4, input_dim=3) model = testing_utils.get_small_mlp(1, 4, input_dim=3)
model.compile( model.compile(
'rmsprop', 'rmsprop', 'mse', run_eagerly=testing_utils.should_run_eagerly())
'mse',
run_eagerly=testing_utils.should_run_eagerly())
inputs = np.zeros((100, 3), dtype=np.float32) inputs = np.zeros((100, 3), dtype=np.float32)
targets = np.random.randint(0, 4, size=100, dtype=np.int32) targets = np.random.randint(0, 4, size=100, dtype=np.int32)
@ -456,8 +469,8 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
verbose=1, verbose=1,
callbacks=[batch_counter], callbacks=[batch_counter],
steps_per_epoch=200) steps_per_epoch=200)
self.assertIn( self.assertIn('ran out of data; interrupting training.',
'ran out of data; interrupting training.', str(mock_log.call_args)) str(mock_log.call_args))
self.assertIn( self.assertIn(
'can generate at least ' 'can generate at least '
'`steps_per_epoch * epochs` batches (in this case, 400 batches). ' '`steps_per_epoch * epochs` batches (in this case, 400 batches). '
@ -490,8 +503,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
out = keras.layers.Dense(2)(inp) out = keras.layers.Dense(2)(inp)
model = keras.Model(inp, out) model = keras.Model(inp, out)
model.compile( model.compile(
'rmsprop', loss='mse', 'rmsprop', loss='mse', run_eagerly=testing_utils.should_run_eagerly())
run_eagerly=testing_utils.should_run_eagerly())
inputs = np.zeros((100, 4), dtype=np.float32) inputs = np.zeros((100, 4), dtype=np.float32)
targets = np.random.randint(0, 2, size=100, dtype=np.int32) targets = np.random.randint(0, 2, size=100, dtype=np.int32)
@ -505,6 +517,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
for _ in range(100): for _ in range(100):
yield (np.zeros(4, dtype=np.float32), yield (np.zeros(4, dtype=np.float32),
np.random.randint(0, 2, size=1, dtype=np.int32)) np.random.randint(0, 2, size=1, dtype=np.int32))
eval_ds = dataset_ops.Dataset.from_generator( eval_ds = dataset_ops.Dataset.from_generator(
generator=gen, generator=gen,
output_types=('float64', 'int32'), output_types=('float64', 'int32'),
@ -516,8 +529,7 @@ class TestTrainingWithDataset(keras_parameterized.TestCase):
steps_per_epoch=10, steps_per_epoch=10,
epochs=10, epochs=10,
validation_data=eval_ds, validation_data=eval_ds,
callbacks=[batch_counter] callbacks=[batch_counter])
)
# Expect 10 batch from training per epoch. # Expect 10 batch from training per epoch.
self.assertEqual(batch_counter.batch_end_count, 100) self.assertEqual(batch_counter.batch_end_count, 100)
@ -529,8 +541,8 @@ class TestMetricsWithDatasets(keras_parameterized.TestCase):
@keras_parameterized.run_all_keras_modes @keras_parameterized.run_all_keras_modes
def test_metrics_correctness_with_dataset(self): def test_metrics_correctness_with_dataset(self):
layers = [ layers = [
keras.layers.Dense(8, activation='relu', input_dim=4, keras.layers.Dense(
kernel_initializer='ones'), 8, activation='relu', input_dim=4, kernel_initializer='ones'),
keras.layers.Dense(1, activation='sigmoid', kernel_initializer='ones') keras.layers.Dense(1, activation='sigmoid', kernel_initializer='ones')
] ]

View File

@ -18,7 +18,6 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
import logging
import pickle import pickle
import types import types