PyTorch Articles

Page 2 of 12

How to compute the histogram of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

The histogram of a tensor is computed using torch.histc(). It returns a histogram represented as a tensor. It takes four parameters: input, bins, min and max. It sorts the elements into equal width bins between min and max. It ignores the elements smaller than the min and greater than the max. Syntax torch.histc(input, bins=100, min=0, max=0) Parameters input − Input tensor bins − Number of histogram bins (default: 100) min − Lower range of bins (default: 0) max − Upper range of bins (default: 0) Basic Example Let's create a ...

Read More

How to find mean across the image channels in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 3K+ Views

RGB images have three channels: Red, Green, and Blue. Computing the mean of pixel values across these channels is a common preprocessing step in computer vision. In PyTorch, we use torch.mean() on image tensors with dim=[1, 2] to calculate channel-wise means. Understanding Image Tensors PyTorch image tensors have shape [C, H, W] where C is channels, H is height, and W is width. Setting dim=[1, 2] computes the mean across height and width dimensions, leaving us with three values (one per channel). Method 1: Using PIL and torch.mean() This approach reads images using PIL and applies ...

Read More

How to compare two tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 11K+ Views

To compare two tensors element-wise in PyTorch, we use the torch.eq() method. It compares corresponding elements and returns True if the elements are equal, else it returns False. We can compare tensors with same or different dimensions, but their sizes must match at non-singleton dimensions. Syntax torch.eq(input, other) Parameters: input − First tensor to compare other − Second tensor to compare Return Value: A tensor of boolean values (True or False) Example 1: Comparing 1-D Tensors The following example shows how to ...

Read More

How to find the k-th and the top "k" elements of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 997 Views

PyTorch provides powerful methods to find specific elements in tensors. torch.kthvalue() finds the k-th smallest element, while torch.topk() finds the k largest elements. Finding the k-th Element with torch.kthvalue() The torch.kthvalue() method returns the k-th smallest element after sorting the tensor in ascending order. It returns both the value and its index in the original tensor. Syntax torch.kthvalue(input, k, dim=None, keepdim=False) Parameters input − The input tensor k − The k-th element to find (1-indexed) dim − The dimension along which to find the k-th value keepdim − Whether to keep ...

Read More

How to sort the elements of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

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

How to compute the mean and standard deviation of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 6K+ Views

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

How to perform element-wise division on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 5K+ Views

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

How to perform element-wise subtraction on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 5K+ Views

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

How to perform element-wise addition on tensors in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 9K+ Views

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

How to resize a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 7K+ Views

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
Showing 11–20 of 120 articles
« Prev 1 2 3 4 5 12 Next »
Advertisements