-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix nnx guide error #2183
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
Fix nnx guide error #2183
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -208,7 +208,8 @@ def loss_fn(model_): | |||||||
| y_pred = model_(x) | ||||||||
| return jnp.mean((y - y_pred) ** 2) | ||||||||
|
|
||||||||
| grads = nnx.grad(loss_fn, wrt=trainable_var)(model) | ||||||||
| diff_state = nnx.DiffState(0, trainable_var) | ||||||||
| grads = nnx.grad(loss_fn, argnums=diff_state)(model) | ||||||||
|
Comment on lines
+211
to
+212
Contributor
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. For conciseness, you can inline the creation of
Suggested change
|
||||||||
| optimizer.update(model, grads) | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -223,7 +223,8 @@ def train_step(model, optimizer, batch): | |||||||
| y_pred = model_(x) | ||||||||
| return jnp.mean((y - y_pred) ** 2) | ||||||||
|
|
||||||||
| grads = nnx.grad(loss_fn, wrt=trainable_var)(model) | ||||||||
| diff_state = nnx.DiffState(0, trainable_var) | ||||||||
| grads = nnx.grad(loss_fn, argnums=diff_state)(model) | ||||||||
|
Comment on lines
+226
to
+227
Contributor
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. For conciseness, you can inline the creation of
Suggested change
|
||||||||
| optimizer.update(model, grads) | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
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.
For conciseness, you can inline the creation of
nnx.DiffStatedirectly into thennx.gradcall. This removes the need for a temporary variable and makes the code a single, expressive line.