To sort the elements of a tensor in PyTorch, we can use the torch.sort() method. This method returns two tensors: the first tensor contains sorted values of the elements and the second tensor contains indices of elements in the original tensor. We can sort 2D tensors row-wise and column-wise by specifying the dimension. Syntax torch.sort(input, dim=None, descending=False) Parameters input − The input tensor to be sorted dim − Dimension along which to sort (0 for column-wise, 1 for row-wise) descending − If True, sorts in descending order (default: False) Example 1: ... Read More
A PyTorch tensor is similar to a NumPy array but optimized for GPU acceleration. Computing mean and standard deviation are fundamental statistical operations in deep learning for data normalization and analysis. Basic Syntax PyTorch provides built-in functions for these statistical computations − torch.mean(input, dim=None) − Computes the mean value torch.std(input, dim=None) − Computes the standard deviation Computing Mean and Standard Deviation of 1D Tensor Let's start with a simple one-dimensional tensor − import torch # Create a 1D tensor tensor_1d = torch.tensor([2.453, 4.432, 0.754, -6.554]) print("Tensor:", tensor_1d) # Compute ... Read More
To perform element-wise division on two tensors in PyTorch, we can use the torch.div() method. It divides each element of the first input tensor by the corresponding element of the second tensor. We can also divide a tensor by a scalar. A tensor can be divided by a tensor with same or different dimension. The dimension of the final tensor will be same as the dimension of the higher-dimensional tensor. If we divide a 1D tensor by a 2D tensor, then the final tensor will a 2D tensor. Syntax torch.div(input, other, *, rounding_mode=None, out=None) Parameters: ... Read More
To perform element-wise subtraction on tensors, we can use the torch.sub() method of PyTorch. The corresponding elements of the tensors are subtracted. We can subtract a scalar or tensor from another tensor with same or different dimensions. The dimension of the final tensor will be the same as the dimension of the higher-dimensional tensor due to PyTorch's broadcasting rules. Syntax torch.sub(input, other, *, alpha=1, out=None) Parameters: input − The tensor to be subtracted from other − The tensor or scalar to subtract alpha − The multiplier for other (default: 1) out − The ... Read More
We can use torch.add() to perform element-wise addition on tensors in PyTorch. It adds the corresponding elements of the tensors. We can add a scalar or tensor to another tensor. We can add tensors with same or different dimensions. The dimension of the final tensor will be same as the dimension of the higher dimension tensor. Steps Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it. Define two or more PyTorch tensors and print them. If you want to add a scalar quantity, ... Read More
To resize a PyTorch tensor, we use the .view() method. We can increase or decrease the dimension of the tensor, but we have to make sure that the total number of elements in a tensor must match before and after the resize. Steps Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it. Create a PyTorch tensor and print it. Resize the above-created tensor using .view() and assign the value to a variable. .view() does not resize the ... Read More
PyTorch provides two main methods to join tensors: torch.cat() and torch.stack(). The key difference is that torch.cat() concatenates tensors along an existing dimension, while torch.stack() creates a new dimension for joining. Key Differences torch.cat() concatenates tensors along an existing dimension without changing the number of dimensions. torch.stack() stacks tensors along a new dimension, increasing the tensor dimensionality by one. Using torch.cat() with 1D Tensors Let's start by concatenating 1D tensors ? import torch # Create 1D tensors t1 = torch.tensor([1, 2, 3, 4]) t2 = torch.tensor([0, 3, 4, 1]) t3 = ... Read More
In PyTorch, tensor metadata includes essential information like size, shape, data type, and device location. The most commonly accessed metadata are the tensor's dimensions and total number of elements. Key Metadata Properties PyTorch tensors provide several ways to access metadata: .size() − Returns the dimensions as a torch.Size object .shape − Returns the same dimensions as .size() torch.numel() − Returns the total number of elements .dtype − Returns the data type .device − Returns the device (CPU/GPU) Example 1: 2D Tensor Metadata import torch # Create a 4x3 tensor T = ... Read More
A PyTorch tensor is like numpy.ndarray. The difference between these two is that a tensor utilizes the GPUs to accelerate numeric computation. We convert a numpy.ndarray to a PyTorch tensor using the function torch.from_numpy(). And a tensor is converted to numpy.ndarray using the .numpy() method. Steps Import the required libraries. Here, the required libraries are torch and numpy. Create a numpy.ndarray or a PyTorch tensor. Convert the numpy.ndarray to a PyTorch tensor using torch.from_numpy() function or convert the PyTorch tensor to numpy.ndarray using the .numpy() method. ... Read More
We use Indexing and Slicing to access the values of a tensor. Indexing is used to access the value of a single element of the tensor, whereas Slicing is used to access the values of a sequence of elements. We use the assignment operator to modify the values of a tensor. Assigning new value/s using the assignment operator will modify the tensor with new value/s. Steps Import the required libraries. Here, the required library is torch. Define a PyTorch tensor. Access the value of a single element ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance