Tensorflow
tensor modification
machine learning
Python
deep learning

Tensorflow How to modify the value in tensor

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow tensors (tf.Tensor) are immutable — you cannot modify their values in place. To "modify" a tensor, you create a new tensor with the desired changes. For mutable state, TensorFlow provides tf.Variable, which supports in-place assignment operations. The key tools for tensor modification are tf.tensor_scatter_nd_update for updating specific indices, tf.where for conditional replacement, and tf.Variable.assign for mutable variables.

Tensors Are Immutable

python
1import tensorflow as tf
2
3x = tf.constant([1, 2, 3, 4, 5])
4
5# This does NOT work — tensors are immutable
6# x[2] = 10  # TypeError: 'tensorflow.python.framework.ops.EagerTensor' does not support item assignment

Every operation on a tensor returns a new tensor. This is by design for computational graph tracing and automatic differentiation.

Method 1: tf.tensor_scatter_nd_update

Update specific indices in a tensor:

python
1x = tf.constant([1, 2, 3, 4, 5])
2
3# Update index 2 to value 10
4indices = tf.constant([[2]])
5updates = tf.constant([10])
6result = tf.tensor_scatter_nd_update(x, indices, updates)
7print(result)  # [1, 2, 10, 4, 5]
8
9# Update multiple indices
10indices = tf.constant([[0], [3], [4]])
11updates = tf.constant([100, 400, 500])
12result = tf.tensor_scatter_nd_update(x, indices, updates)
13print(result)  # [100, 2, 3, 400, 500]

For 2D tensors:

python
1matrix = tf.constant([[1, 2, 3],
2                       [4, 5, 6],
3                       [7, 8, 9]])
4
5# Update position [1, 2] to 99
6indices = tf.constant([[1, 2]])
7updates = tf.constant([99])
8result = tf.tensor_scatter_nd_update(matrix, indices, updates)
9print(result)
10# [[ 1,  2,  3],
11#  [ 4,  5, 99],
12#  [ 7,  8,  9]]
13
14# Update multiple positions
15indices = tf.constant([[0, 0], [2, 2]])
16updates = tf.constant([11, 99])
17result = tf.tensor_scatter_nd_update(matrix, indices, updates)
18# [[11,  2,  3],
19#  [ 4,  5,  6],
20#  [ 7,  8, 99]]

Method 2: tf.where for Conditional Replacement

Replace values based on a condition:

python
1x = tf.constant([1, -2, 3, -4, 5])
2
3# Replace negative values with 0
4result = tf.where(x > 0, x, 0)
5print(result)  # [1, 0, 3, 0, 5]
6
7# Replace values above a threshold
8result = tf.where(x > 3, tf.constant(999), x)
9print(result)  # [1, -2, 3, -4, 999]
10
11# Conditional replacement with another tensor
12a = tf.constant([10, 20, 30, 40, 50])
13b = tf.constant([1, 2, 3, 4, 5])
14mask = tf.constant([True, False, True, False, True])
15result = tf.where(mask, a, b)
16print(result)  # [10, 2, 30, 4, 50]

Method 3: tf.Variable for Mutable State

tf.Variable supports in-place modifications:

python
1var = tf.Variable([1, 2, 3, 4, 5])
2
3# Replace all values
4var.assign([10, 20, 30, 40, 50])
5print(var)  # [10, 20, 30, 40, 50]
6
7# Modify a single index
8var[2].assign(999)
9print(var)  # [10, 20, 999, 40, 50]
10
11# Modify a slice
12var[1:3].assign([200, 300])
13print(var)  # [10, 200, 300, 40, 50]
14
15# In-place arithmetic
16var.assign_add([1, 1, 1, 1, 1])
17print(var)  # [11, 201, 301, 41, 51]
18
19var.assign_sub([1, 1, 1, 1, 1])
20print(var)  # [10, 200, 300, 40, 50]

Method 4: Masking and Arithmetic

Modify values without explicit indexing:

python
1x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0])
2
3# Set all values greater than 3 to 0
4mask = tf.cast(x > 3, tf.float32)
5result = x * (1 - mask)  # Zero out values where mask is 1
6print(result)  # [1., 2., 3., 0., 0.]
7
8# Add 10 to values greater than 3
9bonus = mask * 10
10result = x + bonus
11print(result)  # [1., 2., 3., 14., 15.]
12
13# Clip values to a range
14result = tf.clip_by_value(x, clip_value_min=2.0, clip_value_max=4.0)
15print(result)  # [2., 2., 3., 4., 4.]

Method 5: tf.concat and tf.slice for Rebuilding

Replace a section by slicing and concatenating:

python
1x = tf.constant([1, 2, 3, 4, 5, 6, 7])
2
3# Replace index 3 with 99
4before = x[:3]                    # [1, 2, 3]
5new_val = tf.constant([99])       # [99]
6after = x[4:]                     # [5, 6, 7]
7result = tf.concat([before, new_val, after], axis=0)
8print(result)  # [1, 2, 3, 99, 5, 6, 7]

Converting Between Tensor and NumPy

For complex modifications, convert to NumPy, modify, and convert back:

python
1x = tf.constant([[1, 2], [3, 4]])
2
3# To NumPy
4arr = x.numpy()
5arr[0, 1] = 99
6
7# Back to tensor
8result = tf.constant(arr)
9print(result)
10# [[ 1, 99],
11#  [ 3,  4]]

This approach is straightforward but cannot be used inside @tf.function because NumPy operations are not part of the TensorFlow graph.

Common Pitfalls

  • Expecting in-place modification on tf.Tensor: Tensors are immutable. Operations like x[0] = 5 raise TypeError. Use tf.tensor_scatter_nd_update to create a new tensor with updated values, or use tf.Variable for mutable state.
  • Using tf.Variable in a computation graph unintentionally: Variables maintain state between calls. If used inside a model layer without proper initialization, they can cause unexpected behavior during training.
  • NumPy conversion in @tf.function: .numpy() is not available inside @tf.function because TensorFlow traces the function as a graph. Use tf.tensor_scatter_nd_update or tf.where for graph-compatible modifications.
  • Wrong indices shape in tensor_scatter_nd_update: Indices must have shape (num_updates, rank_of_tensor). For a 1D tensor, use [[0], [2]] not [0, 2]. Incorrect shapes produce cryptic dimension errors.
  • Modifying tensors during gradient computation: Modifying a tensor mid-computation can break the gradient tape. If you need conditional modifications during training, use tf.where or tf.cond, which are differentiable.

Summary

  • tf.Tensor is immutable — you cannot change values in place
  • Use tf.tensor_scatter_nd_update to update specific indices in a new tensor
  • Use tf.where for conditional value replacement based on a mask
  • Use tf.Variable with .assign() for mutable, in-place modifications
  • Avoid .numpy() inside @tf.function — use TensorFlow operations for graph-compatible modifications

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.