PyTorch Articles

Found 120 articles

How to Copy a Tensor in PyTorch?

AYUSH MISHRA
AYUSH MISHRA
Updated on 27-Mar-2026 10K+ Views

PyTorch is a popular Python library for machine learning and deep learning developed by Facebook AI. When working with tensors, you often need to create copies for different purposes. PyTorch provides several methods to copy tensors, each with distinct behaviors regarding memory sharing and gradient tracking. Using clone() Method The clone() method creates a deep copy of a tensor that preserves the computational graph. This is ideal when you need an independent copy that still supports gradient computation ? import torch # Original tensor original_tensor = torch.tensor([11, 12, 13]) # Create a deep copy ...

Read More

Jacobian matrix in PyTorch

Kalyan Mishra
Kalyan Mishra
Updated on 27-Mar-2026 504 Views

In this article we will learn about the Jacobian matrix and how to calculate this matrix using different methods in PyTorch. We use Jacobian matrix in various machine learning applications. What is a Jacobian Matrix? The Jacobian matrix is a mathematical tool used to calculate the relationship between input and output variables. It contains all the partial derivatives of a vector-valued function. This matrix has various applications in machine learning and computational mathematics: Analyzing gradients and derivatives of functions in multivariable calculus Solving differential equations of systems Calculating inverse of vector-valued functions Analyzing stability of dynamic ...

Read More

Index-based Operation in PyTorch

Priya Sharma
Priya Sharma
Updated on 27-Mar-2026 3K+ Views

Index-based operations play a vital role in manipulating and accessing specific elements or subsets of data within tensors. PyTorch, a popular open-source deep learning framework, provides powerful mechanisms to perform such operations efficiently. By leveraging index-based operations, developers can extract, modify, and rearrange data along various dimensions of a tensor. Tensor Basics PyTorch tensors are multi-dimensional arrays that can hold numerical data of various types, such as floating-point numbers, integers, or Boolean values. Tensors are the fundamental data structure in PyTorch and serve as the building blocks for constructing and manipulating neural networks. To create a tensor ...

Read More

Functional Transforms for Computer Vision using PyTorch

Priya Sharma
Priya Sharma
Updated on 27-Mar-2026 377 Views

Computer vision tasks often require preprocessing and augmentation of image data to improve model performance and generalization. PyTorch provides a powerful library for image transformations called torchvision.transforms. While predefined transforms cover many use cases, functional transforms offer greater flexibility for custom transformations using PyTorch tensors and functions. Understanding Transforms in PyTorch Transforms in PyTorch are operations that modify images or their properties. There are two main types: class transforms and functional transforms. Class transforms are implemented as classes with defined parameters, while functional transforms are implemented as functions that operate directly on input data. Functional transforms offer ...

Read More

Implement Deep Autoencoder in PyTorch for Image Reconstructionp

Jaisshree
Jaisshree
Updated on 27-Mar-2026 818 Views

Deep autoencoders are neural networks that compress input data into a lower-dimensional representation and then reconstruct it back to its original form. In this tutorial, we'll implement a deep autoencoder in PyTorch to reconstruct MNIST handwritten digit images. What is an Autoencoder? An autoencoder consists of two main components: Encoder: Compresses input data into a latent representation Decoder: Reconstructs the original data from the compressed representation The goal is to minimize reconstruction error between input and output, forcing the network to learn meaningful data representations. This makes autoencoders useful for data compression, image denoising, ...

Read More

How to slice a 3D Tensor in Pytorch?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 2K+ Views

A 3D Tensor in PyTorch is a three-dimensional array containing matrices, while 1D and 2D tensors represent vectors and matrices respectively. PyTorch provides various methods to slice 3D tensors using indexing operations and built-in functions like split(). Basic Tensor Slicing Syntax PyTorch uses standard Python indexing with the format tensor[dim1, dim2, dim3] where each dimension can use slice notation ? import torch # Create a 3D tensor with shape (2, 3, 4) tensor_3d = torch.randn(2, 3, 4) print("Original tensor shape:", tensor_3d.shape) print(tensor_3d) Original tensor shape: torch.Size([2, 3, 4]) tensor([[[-0.5234, 1.2341, -0.8765, ...

Read More

How to compute the Logarithm of elements of a tensor in PyTorch?

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

To compute the logarithm of elements of a tensor in PyTorch, we use the torch.log() method. It returns a new tensor with the natural logarithm values of the elements of the original input tensor. Syntax torch.log(input, *, out=None) → Tensor Parameters input − The input tensor containing positive values out (optional) − The output tensor to store the result Steps Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it. ...

Read More

How to get the data type of a tensor in PyTorch?

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

A PyTorch tensor is homogeneous, meaning all elements share the same data type. You can access the data type of any tensor using the .dtype attribute, which returns the tensor's data type. Syntax tensor.dtype Where tensor is the PyTorch tensor whose data type you want to retrieve. Example 1: Random Tensor Data Type The following example shows how to get the data type of a randomly generated tensor − import torch # Create a tensor of random numbers of size 3x4 T = torch.randn(3, 4) print("Original Tensor T:") print(T) ...

Read More

How to compute the sine of elements of a tensor in PyTorch?

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

To compute the sine of elements of a tensor, we use the torch.sin() method. It returns a new tensor with the sine values of the elements of the original input tensor. This function is element-wise and preserves the original tensor's shape. Syntax torch.sin(input, out=None) → Tensor Parameters input − Input tensor containing elements in radians out − Optional output tensor to store the result Example 1: 1D Tensor Computing sine values for a one-dimensional tensor − import torch # Create a 1D tensor T = torch.tensor([1.3, 4.32, ...

Read More

How to squeeze and unsqueeze a tensor in PyTorch?

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

In PyTorch, you can modify tensor dimensions using torch.squeeze() and torch.unsqueeze() methods. The squeeze operation removes dimensions of size 1, while unsqueeze adds new dimensions of size 1 at specified positions. Understanding Squeeze Operation The torch.squeeze() method removes all dimensions of size 1 from a tensor. For example, if a tensor has shape (2 × 1 × 3 × 1), squeezing will result in shape (2 × 3). Example import torch # Create a tensor with dimensions of size 1 tensor = torch.ones(2, 1, 2, 1) print("Original tensor shape:", tensor.shape) print("Original tensor:", tensor) ...

Read More
Showing 1–10 of 120 articles
« Prev 1 2 3 4 5 12 Next »
Advertisements