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

Update getting_started.ipynb and getting_started.md #2563

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions docs/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@
" [Module.apply](https://flax.readthedocs.io/en/latest/flax.linen.html#flax.linen.Module.apply)\n",
" method.\n",
"- Computes the `cross_entropy_loss` loss function.\n",
"- Evaluates the loss function and its gradient using\n",
" [jax.value_and_grad](https://jax.readthedocs.io/en/latest/jax.html#jax.value_and_grad).\n",
"- Evaluates the gradient of the loss function using\n",
" [jax.grad](https://jax.readthedocs.io/en/latest/jax.html#jax.grad).\n",
"- Applies a\n",
" [pytree](https://jax.readthedocs.io/en/latest/pytrees.html#pytrees-and-jax-functions)\n",
" of gradients to the optimizer to update the model's parameters.\n",
Expand All @@ -304,8 +304,8 @@
" logits = CNN().apply({'params': params}, batch['image'])\n",
" loss = cross_entropy_loss(logits=logits, labels=batch['label'])\n",
" return loss, logits\n",
" grad_fn = jax.value_and_grad(loss_fn, has_aux=True)\n",
" (_, logits), grads = grad_fn(state.params)\n",
" grad_fn = jax.grad(loss_fn, has_aux=True)\n",
" grads, logits = grad_fn(state.params)\n",
" state = state.apply_gradients(grads=grads)\n",
" metrics = compute_metrics(logits=logits, labels=batch['label'])\n",
" return state, metrics"
Expand Down
8 changes: 4 additions & 4 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ A function that:
[Module.apply](https://flax.readthedocs.io/en/latest/flax.linen.html#flax.linen.Module.apply)
method.
- Computes the `cross_entropy_loss` loss function.
- Evaluates the loss function and its gradient using
[jax.value_and_grad](https://jax.readthedocs.io/en/latest/jax.html#jax.value_and_grad).
- Evaluates the gradient of the loss function using
[jax.grad](https://jax.readthedocs.io/en/latest/jax.html#jax.grad).
- Applies a
[pytree](https://jax.readthedocs.io/en/latest/pytrees.html#pytrees-and-jax-functions)
of gradients to the optimizer to update the model's parameters.
Expand All @@ -220,8 +220,8 @@ def train_step(state, batch):
logits = CNN().apply({'params': params}, batch['image'])
loss = cross_entropy_loss(logits=logits, labels=batch['label'])
return loss, logits
grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
(_, logits), grads = grad_fn(state.params)
grad_fn = jax.grad(loss_fn, has_aux=True)
grads, logits = grad_fn(state.params)
state = state.apply_gradients(grads=grads)
metrics = compute_metrics(logits=logits, labels=batch['label'])
return state, metrics
Expand Down