liveBook · Manning

Pseudo-random number generator

As explained in Deep Learning with JAX

A deterministic algorithm that produces numbers that appear random from an internal state or seed.

A pseudo-random number generator (PRNG) uses a deterministic algorithm to produce sequences that approximate random numbers. A seed initializes its internal state, and repeating that state reproduces the same sequence. PRNGs support weight initialization, data sampling, augmentation, dropout, generative models, hyperparameter tuning, Monte Carlo simulation, and statistical sampling. section 9.1 section 9.0

JAX makes the generator functional: randomized functions receive an explicit key instead of reading hidden mutable state. Use each key once, then split it into new keys for later operations. This design keeps randomness explicit and supports reproducible functional programs, unlike the usual stateful pattern associated with NumPy and commonly with TensorFlow or PyTorch. section 2.4.1 section 9.0

Initializing neural-network parameters

from jax import random key = random.PRNGKey(0) w_key, b_key = random.split(key) weights = random.normal(w_key, (512, 784)) biases = random.normal(b_key, (512,)) The separate keys make the two random draws explicit. A larger initializer can split one key into keys for every layer, as in the network-parameter example. section 2.4.1

Listing 2.4 Initializing the neural network
from jax import random
  
LAYER_SIZES = [28*28, 512, 10]                       #1
PARAM_SCALE = 0.01                                   #2
  
def init_network_params(sizes, key=random.PRNGKey(0), scale=1e-2):
  """Initialize all layers for a fully-connected 
neural network with given sizes"""
  
  def random_layer_params(m, n, key, scale=1e-2):
    """A helper function to randomly initialize
 weights and biases of a dense layer""" 
    w_key, b_key = random.split(key)                 #3
    return scale * random.normal(w_key, (n, m)), 
scale * random.normal(b_key, (n,))                 #4
  
  keys = random.split(key, len(sizes))
  return [random_layer_params(m, n, k, scale) 
for m, n, k in zip(sizes[:-1], sizes[1:], keys)]   #5
  
params = init_network_params(
LAYER_SIZES, random.PRNGKey(0), scale=PARAM_SCALE)
#1 The list with layer sizes
#2 Parameter for scaling random values
#3 Generates random keys (more on this in chapter 7)
#4 Generates random values for the layer parameters w and b
#5 Running generation for all the layers

Browse the complete listing for layer sizes, parameter scaling, per-layer key splitting, and dense-layer initialization. section 2.4.1

How Deep Learning with JAX covers it

How deep it goes
Defines seeds, internal state, deterministic sequences, periods, and JAX key discipline.
Examples
Uses neural-network weights, image noise, rotations, random data splits, sampling, and parameter searches as applications.
What you can do after
Apply it via JAX keys to initialize parameters and generate controlled random values without hidden generator state.
How it connects
Connects functional programming with NumPy, neural-network initialization, data augmentation, dropout, and reproducibility.

Also explained in

Compare how 10 books explain pseudo-random number generator →

Related concepts

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