Menu

Clipping gradients by value can turn them 24 degrees, or 51
📰

Clipping gradients by value can turn them 24 degrees, or 51

DEV Community: deeplearning·Academic Quant·2 days ago
#owfEnJUp
#devtodeeplearning#education#devto#dev#clip
Long article (577 words) — no AI summary yet.
Reading 0:00
15s threshold

There are two ways to clip a gradient and they're often treated as the same thing. Take the gradient (0.3, 0.4, 12). Its length is 12.01 and nearly all of that is in the last entry. If you clip each value to 1 you get (0.3, 0.4, 1), which points 24.2 degrees away from where you started. If you clip the norm to 1 you get (0.025, 0.033, 0.999), and the angle is zero.

import numpy as np

def clip_value ( g , c ) : return np . clip ( g , - c , c ) def clip_norm ( g , c ) : return g * min ( 1.0 , c / np . linalg . norm ( g ) )

g = np . array ( [ 0.3 , 0.4 , 12.0 ] ) for f in ( clip_value , clip_norm ) : h = f ( g , 1.0 ) cos = h @ g / np . linalg . norm ( h ) / np . linalg . norm ( g ) print ( f . __name__ , h . round ( 4 ) , np . degrees ( np . arccos ( min ( 1 , cos ) ) ) . round ( 1 ) )

Enter fullscreen mode

Exit fullscreen mode

When you clip the norm you're taking a shorter step the same way.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.