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
