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

Bring back loss information for multiple outputs #20023

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update torch eval mode
  • Loading branch information
james77777778 committed Jul 21, 2024
commit 1276d2e755b28c948f1f6c127f85cae5f90ed98a
4 changes: 2 additions & 2 deletions keras/src/backend/jax/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def evaluate(
steps_per_execution=self.steps_per_execution,
)

self._symbolic_build(iterator=epoch_iterator, training=False)
self._symbolic_build(iterator=epoch_iterator)

# Container that configures and calls callbacks.
if not isinstance(callbacks, callbacks_module.CallbackList):
Expand Down Expand Up @@ -765,7 +765,7 @@ def test_on_batch(
data = (x, y, sample_weight)
data = _distribute_data(data)
# Maybe build model
self._symbolic_build(data_batch=data, training=False)
self._symbolic_build(data_batch=data)
self._record_training_state_sharding_spec()
self.make_test_function()

Expand Down
8 changes: 4 additions & 4 deletions keras/src/backend/numpy/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def multi_predict_steps(data):

self.predict_function = predict_step

def _symbolic_build(self, data_batch, training=True):
def _symbolic_build(self, data_batch):
model_unbuilt = not all(layer.built for layer in self._flatten_layers())
compile_metrics_unbuilt = (
self._compile_metrics is not None
Expand All @@ -113,7 +113,7 @@ def to_symbolic_input(v):
) = data_adapter_utils.unpack_x_y_sample_weight(data_batch)
# Build all model state with `backend.compute_output_spec`.
try:
y_pred = backend.compute_output_spec(self, x, training=training)
y_pred = backend.compute_output_spec(self, x)
except:
raise RuntimeError(
"Unable to automatically build the model. "
Expand Down Expand Up @@ -246,7 +246,7 @@ def evaluate(
# Build the model on one batch of data.
for _, data in epoch_iterator.enumerate_epoch():
data_batch = data[0]
self._symbolic_build(data_batch, training=False)
self._symbolic_build(data_batch)
break

# Container that configures and calls callbacks.
Expand Down Expand Up @@ -304,7 +304,7 @@ def test_on_batch(
data = (x, y, sample_weight)

# Maybe build model
self._symbolic_build(data, training=False)
self._symbolic_build(data)
self.make_test_function()

logs = self.test_function([data])
Expand Down
4 changes: 2 additions & 2 deletions keras/src/backend/torch/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def evaluate(
steps_per_execution=self.steps_per_execution,
)

self._symbolic_build(iterator=epoch_iterator, training=False)
self._symbolic_build(iterator=epoch_iterator)

# Container that configures and calls callbacks.
if not isinstance(callbacks, callbacks_module.CallbackList):
Expand Down Expand Up @@ -483,7 +483,7 @@ def test_on_batch(
data = (x, y, sample_weight)

# Maybe build model
self._symbolic_build(data_batch=data, training=False)
self._symbolic_build(data_batch=data)
self.make_test_function()

logs = self.test_function([data])
Expand Down
18 changes: 9 additions & 9 deletions keras/src/trainers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ def _assert_compile_called(self, method_name=None):
msg += f"calling `{method_name}()`."
raise ValueError(msg)

def _symbolic_build(self, iterator=None, data_batch=None, training=True):
def _symbolic_build(self, iterator=None, data_batch=None):
model_unbuilt = not all(layer.built for layer in self._flatten_layers())
compile_metrics_unbuilt = (
self._compile_metrics is not None
Expand All @@ -1012,12 +1012,10 @@ def _symbolic_build(self, iterator=None, data_batch=None, training=True):
optimizer_unbuilt = (
self.optimizer is not None and not self.optimizer.built
)
need_build = model_unbuilt or compile_metrics_unbuilt
if backend.backend() != "torch":
# TODO: TorchModuleWrapper will have incorrect behavior using
# `_symbolic_build`. Not sure why.
need_build = need_build or compile_loss_unbuilt
if need_build:
if model_unbuilt or compile_metrics_unbuilt or compile_loss_unbuilt:
if backend.backend() == "torch":
original_training = self.training
self.eval()
# Create symbolic tensors matching an input batch.

def to_symbolic_input(v):
Expand All @@ -1040,7 +1038,7 @@ def to_symbolic_input(v):

# Build all model state with `backend.compute_output_spec`.
try:
y_pred = backend.compute_output_spec(self, x, training=training)
y_pred = backend.compute_output_spec(self, x)
except Exception as e:
raise RuntimeError(
"Unable to automatically build the model. "
Expand Down Expand Up @@ -1070,8 +1068,10 @@ def to_symbolic_input(v):
y,
y_pred,
sample_weight=sample_weight,
training=training,
)
if backend.backend() == "torch":
if original_training:
self.train()
if optimizer_unbuilt:
# Build optimizer
self.optimizer.build(self.trainable_variables)
Expand Down
Loading