Pytorch Tensor Indexing

In this chapter of Pytorch Tutorial, you will learn about indexing a Pytorch tensor.

Indexing a Pytorch tensor is similar to that of a Python list. The pytorch tensor indexing is 0 based, i.e, the first element of the array has index 0.

Accessing Tensor Elements

You can access an element from the Pytorch tensor by specifying the index of the element.

Syntax:

tensor_name[index]

Example

In this example, we will be getting the first element(0th index), and the fourth element from the Pytorch tensor.

tensor = torch.tensor([2, 4, 1, 7, 0, 9])

print(tensor[0])
# Outputs- tensor(2)

print(tensor[3])
# Outputs- tensor(7)

Note– The output is returned as a tensor since the original data structure is also a tensor.

Similar to numpy, you can also access a range of elements from the tensor. We need to specify a start index and an end index separated by a colon. The tensor returned will contain all the elements from the starting index(including) to the ending index(excluding).

Syntax:

tensor_name[start_index : end_index]

Example

In this example, we will be getting the elements between start index 1(inclusive) and end index 5(exclusive).

tensor = torch.tensor([2, 4, 1, 7, 0, 9])

print(tensor[1 : 5])
# Outputs- tensor([4, 1, 7, 0])

Example

If you do not mention the end index, then all the elements from the start index till the end are selected. In this example, we will be getting the elements from the start index 2(inclusive) to the very last element of the tensor.

tensor = torch.tensor([2, 4, 1, 7, 0, 9])

print(tensor[2 : ])
# Outputs- tensor([1, 7, 0, 9])

Example

If you do not mention the start index, then all the elements from the start until the end index(excluding the element at the end index) are selected. In this example, we will be getting the elements from the very start to the end index 4(exclusive) of the tensor.

tensor = torch.tensor([2, 4, 1, 7, 0, 9])

print(tensor[ : 4])
# Outputs- tensor([2, 4, 1, 7])

Example

If you do not mention the start index and the end index, then all the elements from the very start until the very end are selected. Essentially, the original tensor is returned. In this example, we will be getting all the elements from the original tensor.

tensor = torch.tensor([2, 4, 1, 7, 0, 9])

print(tensor[ : ])
# Outputs- tensor([2, 4, 1, 7, 0, 9])

You can access elements from a multi-dimensional tensor similar to how you do it in numpy.

Example

Since the tensor in this example has a rank of 2(in other words, it is an array of arrays), its elements are vectors(arrays) and not scalars(a single value).

tensor = torch.tensor([[1, 2, 1], [3, 8, 4]])

print(tensor[1])
# Outputs- tensor([3, 8, 4])

Example

In the example below, you will get the 3rd element from the 1st array of the tensor.

tensor = torch.tensor([[1, 2, 1], [3, 8, 4]])

print(tensor[0][2])
# Outputs- tensor(1)

Changing Tensor Elements

You can also change a particular element of a pytorch tensor. You can do this by simply assigning a new value to the tensor element.

Syntax:

tensor_name[index] = new_element

Example

In this example, you will change the first element of the tensor from 2 to 8.

tensor = torch.tensor([2, 4, 1, 7, 0, 9])

# assign value 8 to the 0th index in the tensor
tensor[0] = 8

print(tensor)
# Outputs- tensor([8, 4, 1, 7, 0, 9])

You can use indexing to change elements in a tensor of any rank. This can be done in a way similar to how you do it in numpy. This is illustrated in the example below

Example

tensor = torch.tensor([[1, 2, 1], [3, 8, 4]])

# assign value 7 at the given index
tensor[0][0] = 7

print(tensor)
# Outputs- tensor([[7, 2, 1], [3, 8, 4]])