How to Correctly Access Elements in a 3D Pytorch Tensor?
Last Updated :
23 Aug, 2021
In this article, we will discuss how to access elements in a 3D Tensor in Pytorch. PyTorch is an optimized tensor library majorly used for Deep Learning applications using GPUs and CPUs. It is one of the widely used Machine learning libraries, others being TensorFlow and Keras. The python supports the torch module, so to work with this first we import the module to the workspace.
Syntax:
import torch
We can create a vector by using the torch.tensor() function
Syntax:
torch.tensor([value1,value2,.value n])
Example 1: Python code to create an 3 D Tensor and display
Python3
# import torch module
import torch
# create an 3 D tensor with 8 elements each
a = torch.tensor([[[1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
# display actual tensor
print(a)
Output:
tensor([[[ 1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
To access elements from a 3-D tensor Slicing can be used. Slicing means selecting the elements present in the tensor by using ":" slice operator. We can slice the elements by using the index of that particular element.
Note: Indexing starts with 0
Syntax:
tensor[tensor_position_start:tensor_position_end, tensor_dimension_start:tensor_dimension_end , tensor_value_start:tensor_value_end]
where,
- tensor_position_start - Specifies the Tensor to start iterating
- tensor_position_end - Specifies the Tensor to stop iterating
- tensor_dimension_start - Specifies the Tensor to start the iteration of tensor in given positions
- tensor_dimension_stop- Specifies the Tensor to stop the iteration of tensor in given positions
- tensor_value_start - Specifies the start position of the  tensor to iterate the elements given in dimensions
- tensor_value_stop - Specifies the end position of the tensor to iterate the elements given in dimensions
Given below are the various examples for the same.
Example 2: Python code to access  all the tensors of 1  dimension and get only 7 values in that dimension
Python3
# import torch module
import torch
# create an 3 D tensor with 8 elements each
a = torch.tensor([[[1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
# display actual tensor
print(a)
# access all the tensors of 1 dimension
# and get only 7 values in that dimension
print(a[0:1, 0:1, :7])
Output:
tensor([[[ 1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
tensor([[[1, 2, 3, 4, 5, 6, 7]]])
Example 3: Python code to access  all the tensors of all dimensions and get only 3 values in each dimension
Python3
# import torch module
import torch
# create an 3 D tensor with 8 elements each
a = torch.tensor([[[1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
# display actual tensor
print(a)
# access all the tensors of all dimensions
# and get only 3 values in each dimension
print(a[0:1, 0:2, :3])
Output:
tensor([[[ 1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
tensor([[[ 1, 2, 3],
[10, 11, 12]]])
Example 4: access 8 elements in 1 dimension on all tensors
Python3
# import torch module
import torch
# create an 3 D tensor with 8 elements each
a = torch.tensor([[[1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
# display actual tensor
print(a)
# access 8 elements in 1 dimension on all tensors
print(a[0:2, 1, 0:8])
Output:
tensor([[[ 1, 2, 3, 4, 5, 6, 7, 8],
[10, 11, 12, 13, 14, 15, 16, 17]],
[[71, 72, 73, 74, 75, 76, 77, 78],
[81, 82, 83, 84, 85, 86, 87, 88]]])
tensor([[10, 11, 12, 13, 14, 15, 16, 17],
[81, 82, 83, 84, 85, 86, 87, 88]])
Similar Reads
How To Sort The Elements of a Tensor in PyTorch?
In this article, we are going to see how to sort the elements of a PyTorch Tensor in Python. To sort the elements of a PyTorch tensor, we use torch.sort() method. Â We can sort the elements along with columns or rows when the tensor is 2-dimensional. Syntax: torch.sort(input, dim=- 1, descending=Fals
3 min read
How to access the metadata of a tensor in PyTorch?
In this article, we are going to see how to access the metadata of a tensor in PyTorch using Python. PyTorch in Python is a machine learning library. Also, it is free and open-source. It was firstly introduced by the Facebook AI research team. A tensor in PyTorch is similar to a NumPy array. But it
3 min read
How to Slice a 3D Tensor in Pytorch?
In this article, we will discuss how to Slice a 3D Tensor in Pytorch. Let's create a 3D Tensor for demonstration. We can create a vector by using torch.tensor() function Syntax: torch.tensor([value1,value2,.value n]) Code: Python3 # import torch module import torch # create an 3 D tensor with 8 elem
2 min read
How to perform element-wise division on tensors in PyTorch?
In this article, we will understand how to perform element-wise division of two tensors in PyTorch. To perform the element-wise division of tensors, we can apply the torch.div() method. It takes two tensors (dividend and divisor) as the inputs and returns a new tensor with the element-wise division
3 min read
How to compute element-wise entropy of an input tensor in PyTorch
In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a
2 min read
Convert PyTorch Tensor to Python List
PyTorch, a widely-used open-source machine learning library, is known for its flexibility and ease of use in building deep learning models. A fundamental component of PyTorch is the tensor, a multi-dimensional array that serves as the primary data structure for model training and inference. However,
3 min read
How to check if a tensor is contiguous or not in PyTorch
In this article, we are going to see how to check if a tensor is contiguous or not in PyTorch. A contiguous tensor could be a tensor whose components are stored in a contiguous order without having any empty space between them. We can check if a tensor is contiguous or not by using the Tensor.is_con
2 min read
TensorFlow - How to create a tensor with all elements set to one
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. Methods Used: tf.ones: This methods accepts the shape and type and returns a tensor of given shape and type having all values set to 1.tf.fill: This method accepts shape
1 min read
How to access and modify the values of a Tensor in PyTorch?
In this article, we are going to see how to access and modify the value of a tensor in PyTorch using Python. We can access the value of a tensor by using indexing and slicing. Indexing is used to access a single value in the tensor. slicing is used to access the sequence of values in a tensor. we ca
2 min read
How to perform element-wise addition on tensors in PyTorch?
In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in di
3 min read