Open In App

How to Slice a 3D Tensor in Pytorch?

Last Updated : 18 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

TensorFlow - How to create a tensor of all ones that has the same shape as the input tensor
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. Method Used: ones_like: This method accepts a Tensor as input and returns a Tensor with same shape having all values set to one. Example 1: C/C++ Code # importing the library import tensorflow as tf # Initializing the In
1 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=False) Parameters: input: It is an input PyTorch tensor
3 min read
Difference between Tensor and Variable in Pytorch
In this article, we are going to see the difference between a Tensor and a variable in Pytorch. Pytorch is an open-source Machine learning library used for computer vision, Natural language processing, and deep neural network processing. It is a torch-based library. It contains a fundamental set of features that allow numerical computation, deploym
3 min read
Pytorch Functions - tensor(), fill_diagnol(), append(), index_copy()
This article aims to share some PyTorch functions that will help you a lot in your deep learning and data science journey. Each function will be explained using two write examples and one example where you can't use those functions. So let's get started. PyTorch is an open-source machine learning library, it contains a tensor library that enables t
9 min read
One-Dimensional Tensor in Pytorch
In this article, we are going to discuss a one-dimensional tensor in Python. We will look into the following concepts: Creation of One-Dimensional TensorsAccessing Elements of TensorSize of TensorData Types of Elements of TensorsView of TensorFloating Point TensorIntroduction The Pytorch is used to process the tensors. Tensors are multidimensional
5 min read
How to Get the Shape of a Tensor as a List of int in Pytorch?
To get the shape of a tensor as a list in PyTorch, we can use two approaches. One using the size() method and another by using the shape attribute of a tensor in PyTorch. In this short article, we are going to see how to use both of the approaches. Using size() method: The size() method returns the size of the self tensor. The returned value is a s
1 min read
Creating a Tensor in Pytorch
All the deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions. Tensors can be created from Python lists with the torch.tensor() function. The tensor() Method: To create tensors with Pytorch we can simply use the tensor() method: Syntax: torch.tensor(Data) Example: C/C++ Code im
6 min read
How to Get the Data Type of a Pytorch Tensor?
In this article, we are going to create a tensor and get the data type. The Pytorch is used to process the tensors. Tensors are multidimensional arrays. PyTorch accelerates the scientific computation of tensors as it has various inbuilt functions. Vector: A vector is a one-dimensional tensor that holds elements of multiple data types. We can create
3 min read
Change view of Tensor in PyTorch
In this article, we will learn how to change the shape of tensors using the PyTorch view function. We will also look at the multiple ways in which we can change the shape of the tensors. Also, we can use the view function to convert lower-dimensional matrices to higher dimensions. What is the necessary condition to use the PyTorch view() function o
3 min read
How to Convert Pytorch tensor to Numpy array?
In this article, we are going to convert Pytorch tensor to NumPy array. Method 1: Using numpy(). Syntax: tensor_name.numpy() Example 1: Converting one-dimensional a tensor to NumPy array C/C++ Code # importing torch module import torch # import numpy module import numpy # create one dimensional tensor with # float type elements b = torch.tensor([10
2 min read
Article Tags :
Practice Tags :