liveBook · Manning

Key

As explained in Deep Learning with Python, Third Edition

A JAX random-state value passed explicitly to PRNG operations to generate deterministic random numbers without global state.

JAX does not use a global random-number state. Instead, each random operation receives a key: a special array structure created from an integer seed. Pass the key as the first argument to a call such as `jax.random.normal(key, shape=(3,))`. section 3.5.3

Listing 3.30 Using a random seed in Jax
>>> seed_key = jax.random.key(123)
>>> jax.random.normal(seed_key, shape=(3,))
Array([-0.1470326,  0.5524756,  1.648498 ], dtype=float32)
>>> jax.random.normal(seed_key, shape=(3,))
Array([-0.1470326,  0.5524756,  1.648498 ], dtype=float32)

The two calls use the same key and return the same values. A key does not silently advance when used. section 3.5.3

A practical rule of thumb:

  • Use the same key when you need the same random values again. section 3.5.3
  • Use `jax.random.split()` to derive a new key when you need different values. The same sequence of splits produces the same final key. section 3.5.3
>>> seed_key = jax.random.key(123)
>>> jax.random.normal(seed_key, shape=(3,))
Array([-0.1470326,  0.5524756,  1.648498 ], dtype=float32)
>>> new_seed_key = jax.random.split(seed_key, num=1)[0]      #1
>>> jax.random.normal(new_seed_key, shape=(3,))
Array([ 0.5362355, -1.1920372,  2.450225 ], dtype=float32)

Splitting the original key changes the values returned by `random.normal()` without changing global state. section 3.5.3

Managing keys is more work than relying on NumPy’s global PRNG state, but it keeps random operations stateless. That supports repeatable results while allowing the JAX compiler to vectorize code. section 3.5.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains creation, reuse, and deterministic splitting with code, but does not examine PRNG internals.
Examples
Shows repeated `random.normal()` calls returning identical arrays, then a split key producing different values.
What it assumes
Assumes familiarity with random seeds and NumPy-style calls, while introducing JAX keys from creation.
What you can do after
Apply it via `jax.random.key()`, `jax.random.normal()`, and `jax.random.split()` to produce repeatable random values without global state.
How it connects
Connects pseudorandom number generation to statelessness, determinism, and vectorization.

Also explained in

Compare how 3 books explain key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.