Skip to content

How to get per-example gradients in Flax? #2084

Answered by marcvanzee
marcvanzee asked this question in Q&A
Discussion options

You must be logged in to vote

All layers accept batch dimension free inputs. Example:

def loss_fn(param, batch):
  return jnp.square(param * batch)

model = 3.
per_example_grad_fn = jax.vmap(jax.grad(loss_fn), in_axes=(None, 0))
per_example_grad_fn(param, jnp.ones((3,)))

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mu-arkhipov
Comment options

@VMS-6511
Comment options

Answer selected by marcvanzee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #858 on April 27, 2022 20:27.