Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EpochIterator, TensorFlowTrainer : refactoring and fixes #20396

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions keras/src/backend/jax/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def fit(
)

self._symbolic_build(iterator=epoch_iterator)
epoch_iterator.reset()

# Container that configures and calls callbacks.
if not isinstance(callbacks, callbacks_module.CallbackList):
Expand All @@ -405,7 +406,7 @@ def fit(
callbacks.on_epoch_begin(epoch)

self._jax_state_synced = True
for step, data in epoch_iterator.enumerate_epoch():
for step, data in epoch_iterator:
# Callbacks
callbacks.on_train_batch_begin(step)

Expand Down Expand Up @@ -539,6 +540,7 @@ def evaluate(
)

self._symbolic_build(iterator=epoch_iterator)
epoch_iterator.reset()

# Container that configures and calls callbacks.
if not isinstance(callbacks, callbacks_module.CallbackList):
Expand All @@ -560,7 +562,7 @@ def evaluate(
self.reset_metrics()

self._jax_state_synced = True
for step, data in epoch_iterator.enumerate_epoch():
for step, data in epoch_iterator:
callbacks.on_test_batch_begin(step)

if self._jax_state_synced:
Expand Down Expand Up @@ -625,7 +627,7 @@ def predict(

if not all(layer.built for layer in self._flatten_layers()):
# Build the model on one batch of data.
for _, data in epoch_iterator.enumerate_epoch():
for _, data in epoch_iterator:
# Build model
x, _, _ = data_adapter_utils.unpack_x_y_sample_weight(data[0])
with backend.StatelessScope():
Expand Down Expand Up @@ -667,7 +669,7 @@ def append_to_outputs(batch_outputs, outputs):
self._jax_state_synced = True
outputs = None
non_trainable_variables = None
for step, x in epoch_iterator.enumerate_epoch():
for step, x in epoch_iterator:
callbacks.on_predict_batch_begin(step)
if self._jax_state_synced:
# The state may have been synced by a callback.
Expand Down
6 changes: 3 additions & 3 deletions keras/src/backend/numpy/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def append_to_outputs(batch_outputs, outputs):
self.stop_predicting = False
callbacks.on_predict_begin()
outputs = None
for step, data in epoch_iterator.enumerate_epoch():
for step, data in epoch_iterator:
callbacks.on_predict_batch_begin(step)
batch_outputs = self.predict_function(data)
outputs = append_to_outputs(batch_outputs, outputs)
Expand Down Expand Up @@ -256,7 +256,7 @@ def evaluate(

if not all(layer.built for layer in self._flatten_layers()):
# Build the model on one batch of data.
for _, data in epoch_iterator.enumerate_epoch():
for _, data in epoch_iterator:
data_batch = data[0]
self._symbolic_build(data_batch)
break
Expand All @@ -278,7 +278,7 @@ def evaluate(
callbacks.on_test_begin()
logs = {}
self.reset_metrics()
for step, data in epoch_iterator.enumerate_epoch():
for step, data in epoch_iterator:
callbacks.on_test_batch_begin(step)
logs = self.test_function(data)
callbacks.on_test_batch_end(step, logs)
Expand Down
2 changes: 1 addition & 1 deletion keras/src/backend/tensorflow/distribute_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_epoch_iterator(self):
distribute_strategy=strategy,
)
steps_seen = []
for step, data_iterator in epoch_iterator.enumerate_epoch():
for step, data_iterator in epoch_iterator:
steps_seen.append(step)
batch = next(data_iterator)
self.assertEqual(len(batch), 3)
Expand Down
Loading