-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Updates to "Customizing fit() with JAX" guide. #2188
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
Open
hertschuh
wants to merge
1
commit into
keras-team:master
Choose a base branch
from
hertschuh:jax_fit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -60,17 +60,10 @@ | |
- We implement a fully-stateless `compute_loss_and_updates()` method | ||
to compute the loss as well as the updated values for the non-trainable | ||
variables of the model. Internally, it calls `stateless_call()` and | ||
the built-in `compute_loss()`. | ||
the built-in `stateless_compute_loss()`. | ||
- We implement a fully-stateless `train_step()` method to compute current | ||
metric values (including the loss) as well as updated values for the | ||
trainable variables, the optimizer variables, and the metric variables. | ||
|
||
Note that you can also take into account the `sample_weight` argument by: | ||
|
||
- Unpacking the data as `x, y, sample_weight = data` | ||
- Passing `sample_weight` to `compute_loss()` | ||
- Passing `sample_weight` alongside `y` and `y_pred` | ||
to metrics in `stateless_update_state()` | ||
""" | ||
|
||
|
||
|
@@ -79,8 +72,10 @@ def compute_loss_and_updates( | |
self, | ||
trainable_variables, | ||
non_trainable_variables, | ||
metrics_variables, | ||
x, | ||
y, | ||
sample_weight, | ||
training=False, | ||
): | ||
y_pred, non_trainable_variables = self.stateless_call( | ||
|
@@ -89,8 +84,21 @@ def compute_loss_and_updates( | |
x, | ||
training=training, | ||
) | ||
loss = self.compute_loss(x, y, y_pred) | ||
return loss, (y_pred, non_trainable_variables) | ||
loss, ( | ||
trainable_variables, | ||
non_trainable_variables, | ||
metrics_variables, | ||
) = self.stateless_compute_loss( | ||
trainable_variables, | ||
non_trainable_variables, | ||
metrics_variables, | ||
x=x, | ||
y=y, | ||
y_pred=y_pred, | ||
sample_weight=sample_weight, | ||
training=training, | ||
) | ||
return loss, (y_pred, non_trainable_variables, metrics_variables) | ||
|
||
def train_step(self, state, data): | ||
( | ||
|
@@ -99,25 +107,24 @@ def train_step(self, state, data): | |
optimizer_variables, | ||
metrics_variables, | ||
) = state | ||
x, y = data | ||
x, y, sample_weight = keras.utils.unpack_x_y_sample_weight(data) | ||
|
||
# Get the gradient function. | ||
grad_fn = jax.value_and_grad(self.compute_loss_and_updates, has_aux=True) | ||
|
||
# Compute the gradients. | ||
(loss, (y_pred, non_trainable_variables)), grads = grad_fn( | ||
(loss, (y_pred, non_trainable_variables, metrics_variables)), grads = grad_fn( | ||
trainable_variables, | ||
non_trainable_variables, | ||
metrics_variables, | ||
x, | ||
y, | ||
sample_weight, | ||
training=True, | ||
) | ||
|
||
# Update trainable variables and optimizer variables. | ||
( | ||
trainable_variables, | ||
optimizer_variables, | ||
) = self.optimizer.stateless_apply( | ||
trainable_variables, optimizer_variables = self.optimizer.stateless_apply( | ||
optimizer_variables, grads, trainable_variables | ||
) | ||
|
||
|
@@ -129,10 +136,12 @@ def train_step(self, state, data): | |
len(new_metrics_vars) : len(new_metrics_vars) + len(metric.variables) | ||
] | ||
if metric.name == "loss": | ||
this_metric_vars = metric.stateless_update_state(this_metric_vars, loss) | ||
this_metric_vars = metric.stateless_update_state( | ||
this_metric_vars, loss, sample_weight=sample_weight | ||
) | ||
else: | ||
this_metric_vars = metric.stateless_update_state( | ||
this_metric_vars, y, y_pred | ||
this_metric_vars, y, y_pred, sample_weight=sample_weight | ||
) | ||
logs[metric.name] = metric.stateless_result(this_metric_vars) | ||
new_metrics_vars += this_metric_vars | ||
|
@@ -186,6 +195,7 @@ def compute_loss_and_updates( | |
non_trainable_variables, | ||
x, | ||
y, | ||
sample_weight, | ||
training=False, | ||
): | ||
y_pred, non_trainable_variables = self.stateless_call( | ||
|
@@ -194,7 +204,7 @@ def compute_loss_and_updates( | |
x, | ||
training=training, | ||
) | ||
loss = self.loss_fn(y, y_pred) | ||
loss = self.loss_fn(y, y_pred, sample_weight=sample_weight) | ||
return loss, (y_pred, non_trainable_variables) | ||
|
||
def train_step(self, state, data): | ||
|
@@ -204,7 +214,7 @@ def train_step(self, state, data): | |
optimizer_variables, | ||
metrics_variables, | ||
) = state | ||
x, y = data | ||
x, y, sample_weight = keras.utils.unpack_x_y_sample_weight(data) | ||
|
||
# Get the gradient function. | ||
grad_fn = jax.value_and_grad(self.compute_loss_and_updates, has_aux=True) | ||
|
@@ -215,14 +225,12 @@ def train_step(self, state, data): | |
non_trainable_variables, | ||
x, | ||
y, | ||
sample_weight, | ||
training=True, | ||
) | ||
|
||
# Update trainable variables and optimizer variables. | ||
( | ||
trainable_variables, | ||
optimizer_variables, | ||
) = self.optimizer.stateless_apply( | ||
trainable_variables, optimizer_variables = self.optimizer.stateless_apply( | ||
optimizer_variables, grads, trainable_variables | ||
) | ||
|
||
|
@@ -231,10 +239,10 @@ def train_step(self, state, data): | |
mae_metric_vars = metrics_variables[len(self.loss_tracker.variables) :] | ||
|
||
loss_tracker_vars = self.loss_tracker.stateless_update_state( | ||
loss_tracker_vars, loss | ||
loss_tracker_vars, loss, sample_weight=sample_weight | ||
) | ||
mae_metric_vars = self.mae_metric.stateless_update_state( | ||
mae_metric_vars, y, y_pred | ||
mae_metric_vars, y, y_pred, sample_weight=sample_weight | ||
) | ||
|
||
logs = {} | ||
|
@@ -287,7 +295,7 @@ def metrics(self): | |
class CustomModel(keras.Model): | ||
def test_step(self, state, data): | ||
# Unpack the data. | ||
x, y = data | ||
x, y, sample_weight = keras.utils.unpack_x_y_sample_weight(data) | ||
( | ||
trainable_variables, | ||
non_trainable_variables, | ||
|
@@ -301,21 +309,37 @@ def test_step(self, state, data): | |
x, | ||
training=False, | ||
) | ||
loss = self.compute_loss(x, y, y_pred) | ||
loss, ( | ||
trainable_variables, | ||
non_trainable_variables, | ||
metrics_variables, | ||
) = self.stateless_compute_loss( | ||
trainable_variables, | ||
non_trainable_variables, | ||
metrics_variables, | ||
x=x, | ||
y=y, | ||
y_pred=y_pred, | ||
sample_weight=sample_weight, | ||
training=False, | ||
) | ||
|
||
# Update metrics. | ||
new_metrics_vars = [] | ||
logs = {} | ||
for metric in self.metrics: | ||
this_metric_vars = metrics_variables[ | ||
len(new_metrics_vars) : len(new_metrics_vars) + len(metric.variables) | ||
] | ||
if metric.name == "loss": | ||
this_metric_vars = metric.stateless_update_state(this_metric_vars, loss) | ||
this_metric_vars = metric.stateless_update_state( | ||
this_metric_vars, loss, sample_weight=sample_weight | ||
) | ||
else: | ||
this_metric_vars = metric.stateless_update_state( | ||
this_metric_vars, y, y_pred | ||
this_metric_vars, y, y_pred, sample_weight=sample_weight | ||
) | ||
Comment on lines
334
to
341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
logs = metric.stateless_result(this_metric_vars) | ||
logs[metric.name] = metric.stateless_result(this_metric_vars) | ||
new_metrics_vars += this_metric_vars | ||
|
||
# Return metric logs and updated state variables. | ||
|
@@ -336,7 +360,7 @@ def test_step(self, state, data): | |
# Evaluate with our custom test_step | ||
x = np.random.random((1000, 32)) | ||
y = np.random.random((1000, 1)) | ||
model.evaluate(x, y) | ||
model.evaluate(x, y, return_dict=True) | ||
|
||
|
||
""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The metrics are being updated twice.
stateless_compute_loss
, called withingrad_fn
, already updates the metrics. These subsequent calls tometric.stateless_update_state
are redundant and will result in incorrect metric values. You should remove this block. Themetrics_variables
returned fromgrad_fn
already contain the updated state, and thelogs
should be computed from that state.