Skip to content

Commit 8a36e40

Browse files
committed
in layer normalization or any other Gaussian, standardeviation can never be zero. Additionally, if the subtraction inside the square root goes to zero, the gradient will become NaN. Therefore, adding a clipping is necessary.
1 parent d0df86e commit 8a36e40

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

ngclearn/utils/model_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def layer_normalize(x, shift=0., scale=1.):
548548
layer-normalized data samples `x`
549549
"""
550550
xmu = jnp.mean(x, axis=1, keepdims=True)
551-
xsigma = jnp.sqrt(jnp.mean(jnp.square(x - xmu)))
551+
xsigma = jnp.sqrt(jnp.mean(jnp.square(x - xmu)).clip(min=1e-6))
552552
_x = (x - xmu)/(xsigma + 1e-6)
553553
return _x * scale + shift
554554

0 commit comments

Comments
 (0)