liveBook · Manning

Mutation

As explained in Deep Learning with Python, Third Edition

Updating an existing tensor or array object directly rather than constructing a new one.

Mutation updates an existing tensor or array object directly instead of constructing a replacement. JAX rejects this in-place style because it conflicts with stateless computation; updates therefore return a new tensor. section 3.5.3

Replacing an element in JAX

Given `x = jnp.array([1, 2, 3], dtype="float32")`, write `new_x = x.at[0].set(10)`. The expression changes the desired value in the returned array while following JAX’s non-mutating update model. section 3.5.3

Use these rules when translating array code to JAX:

  • Do not assign directly into a JAX array. section 3.5.3
  • Use `at()` with `set()` to express an indexed update and bind the resulting tensor to a new name. section 3.5.3
Listing 3.31 Modifying values in a JAX array
>>> x = jnp.array([1, 2, 3], dtype="float32")
>>> new_x = x.at[0].set(10)

Inspect the compact JAX example showing an indexed update that produces `new_x`. section 3.5.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines in-place modification and demonstrates one indexed replacement, stopping short of broader mutation patterns.
Examples
Uses a three-element JAX array and replaces its first value with `at[0].set(10)`.
What you can do after
Apply it via JAX’s `at()` and `set()` APIs to express updates without direct assignment.
How it connects
Connects mutation with tensor assignment, statelessness, and functional array updates.

Also explained in

Compare how 12 books explain mutation →

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.