liveBook · Manning

Stateless system

As explained in Deep Learning with Python, Third Edition

A programming design in which functions do not depend on or modify hidden global state and produce outputs only from explicit inputs.

A stateless system makes each function depend only on explicit inputs: it neither reads hidden global state nor quietly preserves values between calls. This makes repeated calls predictable and allows independent execution, which is valuable for parallel and distributed computation. section 3.5 section 3.5.3

Explicit state in a JAX training step

A training function receives model parameters explicitly and returns their replacements: `loss, W, b = training_step(inputs, targets, W, b)`. The function computes gradients from `(W, b)`, updates both values, and returns the loss plus updated parameters. Nothing about the parameters is stored implicitly between calls. section 3.5.4

Use these rules when converting stateful code:

  • Pass mutable or evolving values—such as parameters, optimizer variables, metric variables, or random keys—as function arguments. section 3.5.4 section 7.4.5
  • Return every changed value so the caller owns the next state. section 3.5.4
  • Pass a JAX PRNG key to every random operation; reuse a key for reproducible output and split it deterministically for new values. 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 repeated random call visibly produces the same array when given the same key, making determinism and explicit randomness concrete. section 3.5.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains hidden state, PRNG keys, deterministic splitting, returned updates, and a complete custom training loop.
Examples
Demonstrates explicit PRNG keys, deterministic key splitting, JAX array updates, dense layers, and a linear classifier training loop.
What you can do after
Build custom JAX training loops that pass parameters and random keys explicitly, return updated state, and preserve reproducible computation.
How it connects
Connects stateless computation with automatic parallelization, distributed computation, pseudorandom number generation, automatic differentiation, and custom training loops.

Also explained in

Compare how 14 books explain stateless system →

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.