Skip to main content
TopMiniSite

Back to all posts

How to Update A Subset Of A 2D Tensor In TensorFlow?

Published on
4 min read
How to Update A Subset Of A 2D Tensor In TensorFlow? image

Best TensorFlow Resources to Buy in February 2026

1 Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow

Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow

BUY & SAVE
Save 39%
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow
2 Deep Learning with TensorFlow and Keras: Build and deploy supervised, unsupervised, deep, and reinforcement learning models, 3rd Edition

Deep Learning with TensorFlow and Keras: Build and deploy supervised, unsupervised, deep, and reinforcement learning models, 3rd Edition

BUY & SAVE
Save 46%
Deep Learning with TensorFlow and Keras: Build and deploy supervised, unsupervised, deep, and reinforcement learning models, 3rd Edition
3 TinyML: Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers

TinyML: Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers

BUY & SAVE
Save 39%
TinyML: Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers
4 Python Machine Learning: Machine Learning and Deep Learning with Python, scikit-learn, and TensorFlow 2, 3rd Edition

Python Machine Learning: Machine Learning and Deep Learning with Python, scikit-learn, and TensorFlow 2, 3rd Edition

BUY & SAVE
Save 18%
Python Machine Learning: Machine Learning and Deep Learning with Python, scikit-learn, and TensorFlow 2, 3rd Edition
5 Understanding Deep Learning: Building Machine Learning Systems with PyTorch and TensorFlow: From Neural Networks (CNN, DNN, GNN, RNN, ANN, LSTM, GAN) to Natural Language Processing (NLP)

Understanding Deep Learning: Building Machine Learning Systems with PyTorch and TensorFlow: From Neural Networks (CNN, DNN, GNN, RNN, ANN, LSTM, GAN) to Natural Language Processing (NLP)

BUY & SAVE
Save 21%
Understanding Deep Learning: Building Machine Learning Systems with PyTorch and TensorFlow: From Neural Networks (CNN, DNN, GNN, RNN, ANN, LSTM, GAN) to Natural Language Processing (NLP)
6 Deep Learning with TensorFlow 2 and Keras: Regression, ConvNets, GANs, RNNs, NLP, and more with TensorFlow 2 and the Keras API, 2nd Edition

Deep Learning with TensorFlow 2 and Keras: Regression, ConvNets, GANs, RNNs, NLP, and more with TensorFlow 2 and the Keras API, 2nd Edition

BUY & SAVE
Deep Learning with TensorFlow 2 and Keras: Regression, ConvNets, GANs, RNNs, NLP, and more with TensorFlow 2 and the Keras API, 2nd Edition
7 Learning TensorFlow: A Guide to Building Deep Learning Systems

Learning TensorFlow: A Guide to Building Deep Learning Systems

BUY & SAVE
Save 68%
Learning TensorFlow: A Guide to Building Deep Learning Systems
8 Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems

Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems

BUY & SAVE
Save 10%
Hands-On Machine Learning with Scikit-Learn and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems
9 TensorFlow in Action

TensorFlow in Action

BUY & SAVE
Save 13%
TensorFlow in Action
+
ONE MORE?

In TensorFlow, you can update a subset of a 2D tensor by using the tf.tensor_scatter_nd_update function. This function allows you to efficiently update values in a tensor based on indices.

To update a subset of a 2D tensor, follow these steps:

  1. Import the required TensorFlow library:

import tensorflow as tf

  1. Create your original 2D tensor that you want to update:

original_tensor = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.int32)

Here, original_tensor is a 2D tensor with shape (3, 2) containing integer values.

  1. Prepare the indices where you want to update the tensor. These indices should correspond to the positions you want to update. For example, if you want to update the element at row 1 (index 0) and column 2 (index 1) to a new value, you can create a tensor for the indices as follows:

indices = tf.constant([[0, 1]])

Here, indices is a 2D tensor of shape (1, 2), indicating the position where you want to update the value.

  1. Prepare the values that you want to replace the original tensor's subset with. For example, if you want to update the subset with the value 9, create a tensor as:

new_values = tf.constant([9], dtype=tf.int32)

Ensure that the dtype of new_values matches the original tensor.

  1. Use the tf.tensor_scatter_nd_update function to update the tensor:

updated_tensor = tf.tensor_scatter_nd_update(original_tensor, indices, new_values)

The tf.tensor_scatter_nd_update function takes the original tensor, indices, and new values as arguments. It returns the updated tensor.

  1. Execute the TensorFlow session to get the updated tensor's value:

with tf.Session() as sess: updated_tensor_value = sess.run(updated_tensor)

print(updated_tensor_value)

The variable updated_tensor_value will contain the updated tensor's value. In this case, it will be [[1, 9], [3, 4], [5, 6]], with the element at row 1 and column 2 updated to 9.

By following these steps, you can efficiently update a subset of a 2D tensor using TensorFlow's tf.tensor_scatter_nd_update function.

What is the difference between a 1D and a 2D tensor?

A 1D tensor is a vector, which is an array of numbers arranged in a single dimension. It has only one axis or dimension. For example, [1, 2, 3] is a 1D tensor.

A 2D tensor, on the other hand, is a matrix, which is an array of numbers arranged in two dimensions: rows and columns. It has two axes or dimensions. For example:

[ [1, 2, 3], [4, 5, 6] ]

In summary, a 1D tensor (vector) has one dimension or axis, while a 2D tensor (matrix) has two dimensions or axes.

How to create a 2D tensor in TensorFlow?

To create a 2D tensor in TensorFlow, you can use the tf.constant() or tf.Variable() method and provide a 2D list or numpy array as the input. Here's an example using tf.constant():

import tensorflow as tf

Creating a 2D tensor using tf.constant()

tensor_2d = tf.constant([[1, 2, 3], [4, 5, 6]])

Printing the created 2D tensor

print(tensor_2d)

Output:

tf.Tensor( [[1 2 3] [4 5 6]], shape=(2, 3), dtype=int32)

Here's an example using tf.Variable():

import tensorflow as tf

Creating a 2D tensor using tf.Variable()

tensor_2d = tf.Variable([[1, 2, 3], [4, 5, 6]])

Printing the created 2D tensor

print(tensor_2d)

Output:

<tf.Variable 'Variable:0' shape=(2, 3) dtype=int32, numpy= array([[1, 2, 3], [4, 5, 6]], dtype=int32)>

In both cases, the resulting tensor will have a shape of (2, 3) as provided by the input values.

How to update a specific region in a 2D tensor?

To update a specific region in a 2D tensor, you can perform the following steps:

  1. Identify the region you want to update by specifying the start and end indices for rows and columns.
  2. Extract the region from the original tensor. You can use slicing to get the sub-tensor representing the required region.
  3. Modify the extracted region as desired by accessing the specific elements and updating them.
  4. Assign the modified region back to the original tensor at the corresponding indices.

Here is an example Python code snippet that demonstrates the above steps:

import torch

Create a 2D tensor

tensor = torch.zeros((4, 4))

Specify the region to update (e.g., rows 1 and 2, columns 2 to 4)

start_row, end_row = 1, 3 start_col, end_col = 2, 4

Extract the region from the original tensor

region = tensor[start_row:end_row, start_col:end_col]

Modify the extracted region

region += 1

Assign the modified region back to the original tensor

tensor[start_row:end_row, start_col:end_col] = region

Print the updated tensor

print(tensor)

Running the above code will give the following output:

tensor([[0., 0., 0., 0.], [0., 1., 1., 0.], [0., 1., 1., 0.], [0., 0., 0., 0.]])

As you can see, the specified region (rows 1 and 2, columns 2 to 4) has been updated with the value 1.