liveBook · Manning

Tensor assignment

As explained in Deep Learning with R, Third Edition

Tensor assignment is the operation of storing values into a tensor, including modification of existing tensor elements.

Tensor assignment stores a value at a tensor location or changes an existing element. Its behavior depends on the framework: JAX creates an updated tensor, while PyTorch permits in-place modification. section 3.5.3 section 3.4.1

Updating an element in JAX

Create a tensor, update its first element with at()$set(), and keep the result in a new object: x <- jnp$array(c(1, 2, 3), dtype = "float32") new_x <- x$at[0]$set(10) new_x contains 10, 2, and 3, while x remains unchanged. The update follows JAX’s stateless design and supports pure-function style code. section 3.5.3

Choose the assignment style according to the tensor system:

  • Use JAX’s at()$set() API to produce a new tensor with a selected element replaced. section 3.5.3
  • Use PyTorch indexing for in-place assignment when changing an existing tensor is appropriate. section 3.4.1
  • Use R’s [<- syntax as a convenient JAX shorthand; R presents similar copy-on-modify behavior even though the syntax looks in-place. section 3.5.3
  • Represent trainable state with PyTorch’s Parameter class when assignment should identify model parameters for automatic tracking. section 3.4.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Defines assignment, demonstrates indexed updates, and contrasts functional replacement with in-place mutation without detailing compiler internals.
Examples
Demonstrates JAX at()$set(), R-style [<- assignment, PyTorch indexing, and PyTorch Parameter construction.
What it assumes
Assumes basic tensor creation and indexing, while introducing framework-specific mutation and replacement semantics.
What you can do after
Apply it via JAX or PyTorch APIs to replace tensor elements and represent trainable state appropriately.
How it connects
Connects tensor assignment with JAX, NumPy arrays, PyTorch tensors, pure functions, and trainable parameters.

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.