liveBook · Manning

Stateless function component

As explained in Deep Learning with R, Third Edition

A stateless function is a function whose changing computational state is supplied through its arguments and returned explicitly rather than hidden in mutable external state.

A stateless function receives changing computational state through its arguments and returns updated state explicitly. Nothing essential is hidden in mutable external variables. section 3.5.4

A stateless linear-model update

A JAX model accepts inputs, weights, and bias as arguments. A training step computes loss and gradients, updates the weights and bias, and returns the loss together with their new values. Repeated calls therefore carry the complete evolving state explicitly. section 3.5.4

A stateless training function typically follows this sequence:

  • Accept model variables and any other changing values as arguments. section 3.5.4
  • Compute predictions, loss, and gradients from the supplied state. section 3.5.4
  • Return updated variables rather than modifying hidden state; include optimizer, trainable, and non-trainable variables when they participate in training. section 7.4.2
learning_rate <- 0.1

training_step <- jax$jit(\(inputs, targets, W, b) {  #1
  .[loss, grads] <- grad_fn(list(W, b), inputs, targets)  #2
  .[grad_wrt_W, grad_wrt_b] <- grads
  W <- W - grad_wrt_W * learning_rate  #3
  b <- b - grad_wrt_b * learning_rate  #3
  tuple(loss, W, b)  #4
})

This listing shows the complete shape of a stateless JAX update: receive variables, calculate gradients, update them, and return the new values. section 3.5.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains arguments, returned updates, gradients, optimizer state, and non-trainable variables through runnable training procedures.
Examples
Demonstrates a JAX linear classifier and a Keras MNIST training step with TensorFlow, PyTorch, and JAX backends.
What it assumes
Assumes familiarity with model variables, loss functions, gradients, and optimizers; demonstrates their coordination in training steps.
What you can do after
Build custom JAX training loops that pass model and optimizer state explicitly between update calls.
How it connects
Connects stateless functions with JAX, automatic differentiation, custom training loops, Keras, and optimizer state.

Related concepts

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