question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add HOWTO: Gradient clipping

See original GitHub issue

We currently don’t have a canonical implementation in Flax. Some optimizers have it built in.

Possible implementation:

def clip_grad_norm(grad, max_norm):
  norm = jnp.linalg.norm(jax.tree_util.tree_leaves(jax.tree_map(jnp.linalg.norm, grad)))
  factor = jnp.minimum(max_norm, max_norm / (norm + 1e-6))
  return jax.tree_map((lambda x: x * factor), grad)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
EyalRozenberg1commented, Mar 10, 2022

Yes. I actually just saw that there is an implementation there for what we discussed. optax.clip_by_global_norm()

1reaction
jheekcommented, Mar 10, 2022

Shouldn’t this be part of optax?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to apply gradient clipping in TensorFlow? - Stack Overflow
I would like to know How to apply gradient clipping on this network on the RNN where there is a possibility of exploding...
Read more >
Understanding Gradient Clipping (and How It Can Fix ...
The idea behind clipping-by-value is simple. We define a minimum clip value and a maximum clip value. If a gradient exceeds some threshold...
Read more >
How to Avoid Exploding Gradients With Gradient Clipping
Another solution to the exploding gradient problem is to clip the gradient if it becomes too large or too small. We can update...
Read more >
How to apply Gradient Clipping in PyTorch
There are many ways to compute gradient clipping, but a common one is to rescale gradients so that their norm is at most...
Read more >
How to Make a Gradient Clipping Mask in Illustrator - YouTube
Learn how to make a gradient clipping mask in this Illustrator tutorial. We learn how to use transparency masks to create and edit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found