How to slice a 3D Tensor in Pytorch?


The 3D Tensor in Python is known for its array of matrices whereas the 1D and 2D Tensor represents vector and matrix respectively. The Pytorch is defined by an open-source module of machine learning framework based on Python programming and it provides the module named torch. In Python, we have some built-in functions like randn(), rand(), and split() that can be used to represent the slice of a 3D tensor in Pytorch.

Syntax

The following syntax is used in the examples −

import torch

This is a Python module that holds the base class of all neural networks. It helps to build and train the neural network such as input, output, and hidden.

randn() 

This is a numpy library built-in function that can be used to set the random sample from an array for standard normal distribution.

rand()

This is an in-built function of Python that generates pseudo-random numbers.

split()

This is the built-in method in Python that represents the string conversion into the list.

Installation Requirement

pip install torch

This module helps to run the program based on 1D, 2D, and, 3D Tensor.

Example 1

In the following example, we will start the program by importing the torch module. Then use the built-in method named randn() that follows the module torch, accepts some integer as a parameter to distribute the average standard of the array in the form of a list and stores it in the variable a. Then continuously initialize three variables- b, c, and, d that will slice the dimension by the keeping its element. Next, use the print function to set all the variables one after another and get the result.

import torch
a = torch.randn(1,2,3)

# Slicing of the first dimension by keeping the first element
b = x[0:1,:,:]

# Slicing of the second dimension by keeping the second element
c = x[:,0:2,:]

# Slicing of the third dimension by keeping the third element
d = x[:,:,-2:]
print(a)
print(b)
print(c)
print(d)

Output

tensor([[[ 0.1352, -0.8739, -1.5743],
         [-0.4853,  0.7519,  0.9749]]])
tensor([[[-0.7441,  0.3385, -1.0671,  1.0431],
         [ 0.2104, -0.3083,  1.1756,  0.3387],
         [ 0.8722,  0.4534,  0.5528, -0.3567]]])
tensor([[[-0.7441,  0.3385, -1.0671,  1.0431],
         [ 0.2104, -0.3083,  1.1756,  0.3387]],

        [[ 0.6623,  1.3858, -1.0033, -0.5977],
         [ 1.1141, -0.8058,  1.5914,  0.6476]]])
tensor([[[-1.0671,  1.0431],
         [ 1.1756,  0.3387],
         [ 0.5528, -0.3567]],

        [[-1.0033, -0.5977],
         [ 1.5914,  0.6476],
         [ 1.6480, -0.9212]]])

Example 2

In the following example, begin the program by importing the torch module. Then store the total of three array lists in the variable tensor_3d. Next, store the 0th index of the list in the variable tensor. In the same way, stored the list1 and list2 in the variable tensor1 and tensor2 respectively. Then print the result with the help of all tensor variables.

import torch
tensor_3d = ([[[10, 20, 30, 40],[50, 60, 70, 80],[1, 2, 3, 4]],
   [[13, 14, 15, 16],[21, 22, 23, 24], [3, 4, 5, 6]],
   [[61, 62, 63, 64],[71, 72, 73, 74],[7, 8, 9, 10]]])
tensor = tensor_3d[0]
tensor1 = tensor_3d[1]
tensor2 = tensor_3d[2]
print("The first dimension:",tensor)
print("The second dimension:",tensor1)
print("The third dimension:",tensor2)

Output

The first dimension: [[10, 20, 30, 40], [50, 60, 70, 80], [1, 2, 3, 4]]
The second dimension: [[13, 14, 15, 16], [21, 22, 23, 24], [3, 4, 5, 6]]
The third dimension: [[61, 62, 63, 64], [71, 72, 73, 74], [7, 8, 9, 10]]

Example 3

In the following example, begin the program by importing the necessary module named torch and random which will generate the array of matrices with random values. Then the torch.rand((3, 4, 8)) creates the random values between 0 and 1 and stores it in the variable x. Next, torch.split() accepts three parameters- x, 3, and, 2 which define the tensor x into chunks along the third dimension (2) with a size of 3 for each chunk and store it in the variable slices. Finally, we are printing the result with the help of variable slices.

Note that chunk refers to a smaller tensor created by dividing the original tensor along a specific dimension.

import torch
import random

# generate the random values between 0 and 1
x = torch.rand((3,4,8))
slices = torch.split(x, 3, 2)
print(slices)

Output

(tensor([[[0.3747, 0.4710, 0.4233, 0.2445],
         [0.9414, 0.6634, 0.6091, 0.8761],
         [0.7911, 0.8687, 0.8468, 0.8766],
         [0.8192, 0.1498, 0.7685, 0.4718]],

        [[0.4160, 0.7172, 0.4647, 0.4860],
         [0.7074, 0.9610, 0.4967, 0.7411],
         [0.2269, 0.8565, 0.8671, 0.3461],
         [0.0397, 0.0809, 0.2017, 0.1106]],

        [[0.6528, 0.7044, 0.7829, 0.1844],
         [0.4297, 0.2802, 0.4159, 0.4485],
         [0.5843, 0.6958, 0.8991, 0.3918],
         [0.2549, 0.9363, 0.3098, 0.7053]]]), tensor([[[0.0095, 0.4314, 0.6110, 0.4594],
         [0.8541, 0.7622, 0.3550, 0.7715],
         [0.5951, 0.9786, 0.3598, 0.2587],
         [0.5516, 0.6523, 0.5176, 0.1267]],

        [[0.8616, 0.3667, 0.0145, 0.9252],
         [0.5995, 0.3094, 0.0483, 0.1996],
         [0.1004, 0.1373, 0.3303, 0.0982],
         [0.2414, 0.7782, 0.8850, 0.1027]],

        [[0.0559, 0.8675, 0.7974, 0.6309],
         [0.3900, 0.4914, 0.4957, 0.5973],
         [0.6249, 0.8075, 0.6700, 0.7789],
         [0.6211, 0.9158, 0.6633, 0.2306]]]))

Conclusion

We discussed all the types of tensors in Pytorch. The Pytorch is simply known for N-dimension or we can say the high-dimensional tensor. The above outputs used various built-in function like split(), randn(), and, rand() that helps to get the result in the form of a 3D tensor. The various application of tensor is used in the field of physics, engineering, and, computer science.

Updated on: 17-Jul-2023

774 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements