-
Notifications
You must be signed in to change notification settings - Fork 19.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back loss information for multiple outputs (#20023)
* Bring back loss info for multiple outputs * Fix CI * Update torch eval mode * Add SymbolicScope * Minor updates and add tests * Address comment * Fix TorchWrapper of training args
- Loading branch information
1 parent
de5d18b
commit f6a81cc
Showing
18 changed files
with
289 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from keras.src.api_export import keras_export | ||
from keras.src.backend.common import global_state | ||
|
||
|
||
@keras_export("keras.SymbolicScope") | ||
class SymbolicScope: | ||
"""Scope to indicate the symbolic stage.""" | ||
|
||
def __enter__(self): | ||
self.original_scope = get_symbolic_scope() | ||
global_state.set_global_attribute("symbolic_scope", self) | ||
return self | ||
|
||
def __exit__(self, *args, **kwargs): | ||
global_state.set_global_attribute("symbolic_scope", self.original_scope) | ||
|
||
|
||
def in_symbolic_scope(): | ||
return global_state.get_global_attribute("symbolic_scope") is not None | ||
|
||
|
||
def get_symbolic_scope(): | ||
return global_state.get_global_attribute("symbolic_scope") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import numpy as np | ||
|
||
from keras.src import ops | ||
from keras.src import testing | ||
from keras.src.backend.common.symbolic_scope import SymbolicScope | ||
from keras.src.backend.common.symbolic_scope import in_symbolic_scope | ||
|
||
|
||
class TestSymbolicScope(testing.TestCase): | ||
def test_basic_flow(self): | ||
|
||
# Define a function that behaves differently according to | ||
# `in_symbolic_scope`. | ||
def compute_loss(y, y_pred): | ||
if in_symbolic_scope(): | ||
return ops.zeros_like(y) | ||
return ops.add(y, y_pred) | ||
|
||
y = ops.ones(shape=(2,)) | ||
y_pred = ops.ones(shape=(2,)) | ||
with SymbolicScope(): | ||
loss = compute_loss(y, y_pred) | ||
self.assertAllClose(loss, np.zeros((2,))) | ||
|
||
loss = compute_loss(y, y_pred) | ||
self.assertAllClose(loss, 2 * np.ones((2,))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.