Skip to content
Open
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
6 changes: 5 additions & 1 deletion jax/_src/nn/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,11 @@ def standardize(x: ArrayLike,
# when used in neural network normalization layers
variance = jnp.mean(
jnp.square(x), axis, keepdims=True, where=where) - jnp.square(mean)
return jnp.subtract(x, jnp.asarray(mean)) * lax.rsqrt(jnp.asarray(variance) + epsilon)
# Because we're using a less accurate variance definition, it may return
# negative values. This is problematic for the rsqrt, so we take the
# absolute value.
variance = lax.abs(variance)
return jnp.subtract(x, mean) * lax.rsqrt(variance + epsilon)

# TODO(slebedev): Change the type of `x` to `ArrayLike`.
@api.jit(static_argnames=("num_classes", "dtype", "axis"))
Expand Down
6 changes: 6 additions & 0 deletions tests/nn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@ def testStandardizeWhereMask(self):

self.assertAllClose(out_masked, out_filtered)

def testStandardizeNegativeVariance(self):
# Regression test for https://github.com/google/jax/issues/30426
x = jnp.array([-11., -11., -11.]) + 3e-6
result = jax.nn.standardize(x)
self.assertFalse(jnp.any(jnp.isnan(result)))

def testOneHot(self):
actual = nn.one_hot(jnp.array([0, 1, 2]), 3)
expected = jnp.array([[1., 0., 0.],
Expand Down
Loading