How to access and modify the values of a Tensor in PyTorch?

We use Indexing and Slicing to access the values of a tensor. Indexing is used to access the value of a single element of the tensor, whereas Slicing is used to access the values of a sequence of elements.

We use the assignment operator to modify the values of a tensor. Assigning new value/s using the assignment operator will modify the tensor with new value/s.

Steps

  • Import the required libraries. Here, the required library is torch.

  • Define a PyTorch tensor.

  • Access the value of a single element at particular index using indexing or access the values of sequence of elements using slicing.

  • Modify the accessed values with new values using the assignment operator.

  • Finally, print the tensor to check if the tensor is modified with the new values.

Accessing and Modifying Single Elements

You can access individual tensor elements using indexing notation and modify them directly ?

# Python program to access and modify values of a tensor in PyTorch
# Import the libraries
import torch

# Define PyTorch Tensor
a = torch.Tensor([[3, 5],[1, 2],[5, 7]])
print("a:\n",a)

# Access a value at index [1,0]-> 2nd row, 1st Col using indexing
b = a[1,0]
print("a[1,0]:\n", b)

# Other indexing method to access value
c = a[1][0]
print("a[1][0]:\n",c)

# Modifying the value 1 with new value 9
# assignment operator is used to modify with new value
a[1,0] = 9
print("tensor 'a' after modifying value at a[1,0]:")
print("a:\n",a)
a:
tensor([[3., 5.],
        [1., 2.],
        [5., 7.]])
a[1,0]:
 tensor(1.)
a[1][0]:
 tensor(1.)
tensor 'a' after modifying value at a[1,0]:
a:
tensor([[3., 5.],
        [9., 2.],
        [5., 7.]])

Accessing and Modifying Multiple Elements Using Slicing

Slicing allows you to access and modify multiple elements or entire rows/columns at once ?

# Python program to access and modify values of a tensor in PyTorch
# Import necessary libraries
import torch

# Define PyTorch Tensor
a = torch.Tensor([[3, 5],[1, 2],[5, 7]])
print("a:\n", a)

# Access all values of 2nd row using slicing
b = a[1]
print("a[1]:\n", a[1])

# Access all values of 1st and 2nd rows
b = a[0:2]
print("a[0:2]:\n" , a[0:2])

# Access all values of 2nd col
c = a[:,1]
print("a[:,1]:\n", a[:,1])

# Access values from first two rows but 2nd col
print("a[0:2, 1]:\n", a[0:2, 1])

# assignment operator is used to modify with new value
# Modifying the values of 2nd row
a[1] = torch.Tensor([9, 9])
print("After modifying a[1]:\n", a)

# Modify values of first two rows but 2nd col
a[0:2, 1] = torch.Tensor([4, 4])
print("After modifying a[0:2, 1]:\n", a)
a:
tensor([[3., 5.],
        [1., 2.],
        [5., 7.]])
a[1]:
 tensor([1., 2.])
a[0:2]:
 tensor([[3., 5.],
        [1., 2.]])
a[:,1]:
 tensor([5., 2., 7.])
a[0:2, 1]:
 tensor([5., 2.])
After modifying a[1]:
 tensor([[3., 5.],
         [9., 9.],
         [5., 7.]])
After modifying a[0:2, 1]:
tensor([[3., 4.],
        [9., 4.],
        [5., 7.]])

Common Indexing Patterns

Here are some common patterns for accessing tensor elements:

  • tensor[i, j] ? Access element at row i, column j
  • tensor[i] ? Access entire row i
  • tensor[:, j] ? Access entire column j
  • tensor[start:end] ? Access rows from start to end-1
  • tensor[start:end, j] ? Access column j from rows start to end-1

Conclusion

PyTorch tensors support standard Python indexing and slicing operations for accessing elements. Use square brackets with indices for single elements, and slicing notation with colons for multiple elements. The assignment operator directly modifies tensor values in-place.

Updated on: 2026-03-26T18:38:45+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements