How to Perform in-place Operations in PyTorch?
Last Updated :
05 Jun, 2022
In this article, we will see different in-place operations performed on tensors in PyTorch.
Inplace operations are used to directly alter the values of a tensor. The data collected from the user will not be copied. The fundamental benefit of adopting these procedures is that they reduce memory storage while increasing computation, making it easier to deal with high-dimensional data.
In this article, we will see how to do some of the arithmetic operations (addition, subtraction, and multiplication) with and without in-place operations.
Syntax for Addition
Normal Addition: t1.add(t2)
In-place Addition: t1.add_(t2)
Syntax for Subtraction
Normal Subtraction: t1.sub(t2)
In-place Subtraction: t1.sub_(t2)
Syntax for Subtraction
Normal Multiplication: t1.mul(t2)
In-place Multiplication: t1.mul_(t2)
Example 1:
In this example, we are creating two tensors a and b that hold a single value and perform all Addition operations, and then we will display the content of a tensor using the item() method.
Python3
# import required library
import torch
# create two tensors a and b with
# single value
a = torch.tensor(2)
b = torch.tensor(6)
print("Addition")
# Normal addition
a.add(b)
# display content in a
print("Normal addition : ", a.item())
# In-place addition
a.add_(b)
# display content in a
print("In-place addition : ", a.item())
Output:
Addition
Normal addition : 2
In-place addition : 8
Example 2:
In this example, we are creating two tensors a and b that hold a single value and perform all subtraction operations, and then we will display the content of a tensor using the item() method.
Python3
# import required library
import torch
# create two tensors a and b with
# single value
a = torch.tensor(2)
b = torch.tensor(6)
print("Subtraction")
# Normal Subtraction
a.sub(b)
# display content in a
print("Normal Subtraction : ", a.item())
# In-place Subtraction
a.sub_(b)
# display content in a
print("In-place Subtraction : ", a.item())
Output:
Subtraction
Normal Subtraction : 2
In-place Subtraction : -4
Example 3
In this example, we are creating two tensors a and b that hold a single value and perform all multiplication operations, and then we will display the content of a tensor using the item() method.
Python3
# import required library
import torch
# create two tensors a and b with
# single value
a = torch.tensor(2)
b = torch.tensor(6)
print("Multiplication")
# Normal Multiplication
a.mul(b)
# display content in a
print("Normal Subtraction : ", a.item())
# In-place Multiplication
a.mul_(b)
# display content in a
print("In-place Multiplication : ", a.item())
Output:
Multiplication
Normal Subtraction : 2
In-place Multiplication : 12
Example 4
In this example, we are creating two tensors a and b that hold a multiple value and perform all Addition and multiplication operations, and then we will display the content of a tensor using the item() method.
Python3
# import required library
import torch
# create two tensors a and b with
# 4 values each
a = torch.tensor([2, 3, 4, 5])
b = torch.tensor([2, 3, 4, 5])
print("Addition")
# Normal addition
a.add(b)
# display content in a
print("Normal addition : ", a)
# In-place addition
a.add_(b)
# display content in a
print("In-place addition : ", a)
print("\nMultiplication")
# Normal Multiplication
a.mul(b)
# display content in a
print("Normal Subtraction : ", a)
# In-place Multiplication
a.mul_(b)
# display content in a
print("In-place Multiplication : ", a)
print()
Output:
Addition
Normal addition : tensor([2, 3, 4, 5])
In-place addition : tensor([ 4, 6, 8, 10])
Multiplication
Normal Subtraction : tensor([ 4, 6, 8, 10])
In-place Multiplication : tensor([ 8, 18, 32, 50])
Similar Reads
How to Iterate Over Layers in PyTorch
PyTorch is a powerful and widely-used deep learning framework that offers flexibility and ease of use for building and training neural networks. One common task when working with neural networks is iterating over the layers of a model, whether to inspect their properties, modify them, or apply custo
5 min read
How to improve the performance of PyTorch models?
PyTorch's flexibility and ease of use make it a popular choice for deep learning. To attain the best possible performance from a model, it's essential to meticulously explore and apply diverse optimization strategies. The article explores effective methods to enhance the training efficiency and accu
10 min read
Vector Operations in Pytorch
In this article, we are going to discuss vector operations in PyTorch. Vectors are a one-dimensional tensor, which is used to manipulate the data. Vector operations are of different types such as mathematical operation, dot product, and linspace. PyTorch is an optimized tensor library majorly used f
4 min read
Tensor Operations in PyTorch
In this article, we will discuss tensor operations in PyTorch. PyTorch is a scientific package used to perform operations on the given data like tensor in python. A Tensor is a collection of data like a numpy array. We can create a tensor using the tensor function: Syntax: torch.tensor([[[element1,e
5 min read
How to implement neural networks in PyTorch?
This tutorial shows how to use PyTorch to create a basic neural network for classifying handwritten digits from the MNIST dataset. Neural networks, which are central to modern AI, enable machines to learn tasks like regression, classification, and generation. With PyTorch, you'll learn how to design
5 min read
How to Process Multiple Losses in PyTorch
When working with complex machine learning models in PyTorch, especially those involving multi-task learning or models with multiple objectives, it is often necessary to handle multiple loss functions. This article will guide you through the process of managing and combining multiple loss functions
5 min read
How to Read a JPEG or PNG Image in PyTorch
In this article, we are going to discuss how to Read a JPEG or PNG Image using PyTorch in Python. image_read() method In PyTorch, the image_read() method is used to read an image as input and return a tensor of size [C, H, W], where C represents a number of channels and H, W represents height and wi
2 min read
How to perform element-wise multiplication on tensors in PyTorch?
In this article, we are going to see how to perform element-wise multiplication on tensors in PyTorch in Python. We can perform element-wise addition using torch.mul() method. This function also allows us to perform multiplication on the same or different dimensions of tensors. If tensors are diffe
3 min read
How to Install Pytorch on MacOS?
PyTorch is an open-source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook's AI Research lab. It is free and open-source software released under the Modified BSD license. Prerequisites:
2 min read
How to set up and Run CUDA Operations in Pytorch ?
CUDA(or Compute Unified Device Architecture) is a proprietary parallel computing platform and programming model from NVIDIA. Using the CUDA SDK, developers can utilize their NVIDIA GPUs(Graphics Processing Units), thus enabling them to bring in the power of GPU-based parallel processing instead of t
4 min read