TensorFlow
tf.scatter_update
tf.Variable
machine learning
deep learning

Use tf.scatter_update in a two dimensional tf.Variable

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The old tf.scatter_update question is really about indexed assignment into a TensorFlow variable. In modern TensorFlow, the exact API depends on whether you are updating whole rows of a tf.Variable or arbitrary positions inside a tensor.

For TensorFlow 2 code, the most useful tools are variable.scatter_nd_update(...) for mutable variables and tf.tensor_scatter_nd_update(...) when you want a new tensor value instead of in-place mutation. The older tf.scatter_update name belongs to the TensorFlow 1 style and is now mostly a compatibility topic.

Updating Rows in a 2D Variable

Suppose you have a 2 x 3 variable and want to replace row 1.

python
1import tensorflow as tf
2
3v = tf.Variable([
4    [1, 2, 3],
5    [4, 5, 6],
6], dtype=tf.int32)
7
8v.scatter_nd_update(
9    indices=[[1]],
10    updates=[[40, 50, 60]],
11)
12
13print(v.numpy())

Output:

python
[[ 1  2  3]
 [40 50 60]]

Each index identifies the leading dimensions to update. Since v is 2D and the index is [1], the update replaces one whole row.

Updating Individual Cells

If you want to update specific elements rather than whole rows, provide full coordinates.

python
1import tensorflow as tf
2
3v = tf.Variable([
4    [1, 2, 3],
5    [4, 5, 6],
6], dtype=tf.int32)
7
8v.scatter_nd_update(
9    indices=[[0, 1], [1, 2]],
10    updates=[20, 99],
11)
12
13print(v.numpy())

Output:

python
[[ 1 20  3]
 [ 4  5 99]]

Now each index identifies one element, so the updates are scalar values.

When to Use tf.tensor_scatter_nd_update

If you do not want to mutate the original variable, use tf.tensor_scatter_nd_update instead.

python
1import tensorflow as tf
2
3x = tf.constant([
4    [1, 2, 3],
5    [4, 5, 6],
6], dtype=tf.int32)
7
8y = tf.tensor_scatter_nd_update(
9    x,
10    indices=[[0, 0], [1, 1]],
11    updates=[10, 50],
12)
13
14print(x.numpy())
15print(y.numpy())

Here x stays unchanged and y is the updated tensor.

This is often a better fit inside functional model code where mutation is not desired.

Understanding the Shape Rule

The key rule is that the shape of updates must match what each index selects.

  • index shape [row] in a 2D tensor selects an entire row
  • index shape [row, col] selects a single cell

That is why row replacement needs updates shaped like rows, while element replacement needs scalar updates.

Once you understand that rule, most scatter-update errors become much easier to debug.

Why the Old API Name Appears in Answers

Older TensorFlow 1 examples often use tf.scatter_update or tf.compat.v1.scatter_update. Those APIs reflected the graph-mode style where mutable variable ops were built into the computation graph explicitly.

In TensorFlow 2, eager execution makes the variable methods easier to use directly, so variable.scatter_nd_update(...) is usually the clearer modern answer.

Common Pitfalls

A common mistake is giving row indices but scalar updates, or element indices but row-shaped updates. The index depth determines what slice is being replaced.

Another mistake is trying to use tensor scatter update when actual variable mutation is required later in the same stateful workflow.

Developers also copy old TensorFlow 1 answers that use compatibility APIs even though the project is already using TensorFlow 2 eager mode.

Finally, remember that scatter updates do not behave like arbitrary Python list assignment. TensorFlow still enforces tensor shape consistency.

Summary

  • In TensorFlow 2, prefer variable.scatter_nd_update(...) for mutating a tf.Variable.
  • Use tf.tensor_scatter_nd_update(...) when you want a new tensor instead of mutation.
  • Row indices update whole rows; full coordinate indices update individual cells.
  • The shape of updates must match the slice selected by each index.
  • Many old examples use TensorFlow 1 naming, but the modern idea is the same: indexed tensor updates with strict shape rules.

Course illustration
Course illustration

All Rights Reserved.