How to Slice a 3D Tensor in Pytorch?
Last Updated :
18 Jul, 2021
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 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]]])
Slicing a 3D Tensor
Slicing: 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]
Parameters:
- 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
Example 1: Python code to access all the tensors of 1 dimension and get only 7 values in that dimension
Python3
# 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]]])
Example 2: Python code to access all the tensors of all dimensions and get only 3 values in each dimension
Python3
# 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],
[10, 11, 12]]])
Example 3: Access 8 elements in 1 dimension on all tensors
Python3
# access 8 elements in 1 dimension
# on all tensors
print(a[0:2, 1, 0:8])
Output:
tensor([[10, 11, 12, 13, 14, 15, 16, 17],
[81, 82, 83, 84, 85, 86, 87, 88]])
Similar Reads
How to resize a tensor in PyTorch?
In this article, we will discuss how to resize a Tensor in Pytorch. Resize allows us to change the size of the tensor. we have multiple methods to resize a tensor in PyTorch. let's discuss the available methods. Method 1: Using view() method We can resize the tensors in PyTorch by using the view() m
5 min read
Way to Copy a Tensor in PyTorch
In deep learning, PyTorch has become a popular framework for building and training neural networks. At the heart of PyTorch is the tensorâa multi-dimensional array that serves as the fundamental building block for all operations in the framework. There are many scenarios where you might need to copy
5 min read
Reshaping a Tensor in Pytorch
In this article, we will discuss how to reshape a Tensor in Pytorch. Reshaping allows us to change the shape with the same data and number of elements as self but with the specified shape, which means it returns the same data as the specified array, but with different specified dimension sizes. Crea
7 min read
How to Upsample a PyTorch Tensor?
As the amount of data generated by modern sensors and simulations continues to grow, it's becoming increasingly common for datasets to include multiple channels representing different properties or dimensions. However, in some cases, these channels may be at a lower resolution or spatial/temporal sc
8 min read
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
Two-Dimensional Tensors in Pytorch
PyTorch is a python library developed by Facebook to run and train machine learning and deep learning models. In PyTorch everything is based on tensor operations. Two-dimensional tensors are nothing but matrices or vectors of two-dimension with specific datatype, of n rows and n columns. Representat
3 min read
How to Get the Value of a Tensor in PyTorch
When working with PyTorch, a powerful and flexible deep learning framework, you often need to access and manipulate the values stored within tensors. Tensors are the core data structures in PyTorch, representing multi-dimensional arrays that can store various types of data, including scalars, vector
5 min read
How to Correctly Access Elements in a 3D Pytorch Tensor?
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 t
4 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
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