Tensor.detach() Method in Python PyTorch Last Updated : 10 Jun, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will see Tensor.detach() method in PyTorch using Python. Pytorch is a Python and C++ interface for an open-source deep learning platform. It is found within the torch module. In PyTorch, the input data has to be processed in the form of a tensor. It also includes a module that calculates gradients automatically for backpropagation. Tensor.detach() method in PyTorch is used to separate a tensor from the computational graph by returning a new tensor that doesn't require a gradient. If we want to move a tensor from the Graphical Processing Unit (GPU) to the Central Processing Unit (CPU), then we can use detach() method. It will not take any parameter and return the detached tensor. Syntax: tensor.detach() Return: the detached tensor Example 1: In this example, we will create a one-dimensional tensor with a gradient parameter and detach it using a tensor.detach() method. requires_grad takes boolean value - True Python3 # import the torch module import torch # create one dimensional tensor with 5 elements with requires_grad # parameter that sets to True tensor1 = torch.tensor([7.8, 3.2, 4.4, 4.3, 3.3], requires_grad=True) print(tensor1) # detach the tensor print(tensor1.detach()) Output: tensor([7.8000, 3.2000, 4.4000, 4.3000, 3.3000], requires_grad=True) tensor([7.8000, 3.2000, 4.4000, 4.3000, 3.3000])Example 2: In this example, we will create a two-dimensional tensor with a gradient parameter= False , you will notice that in the output, the tensor doesn't effect if we set requires_grad = False, and detach it using a tensor.detach() method. Python3 # import the torch module import torch # create two dimensional tensor with 5 elements with # requires_grad parameter that sets to True tensor1 = torch.tensor([[7.8, 3.2, 4.4, 4.3, 3.3], [3., 6., 7., 3., 2.]], requires_grad=False) print(tensor1) # detach the tensor print(tensor1.detach()) Output: tensor([[7.8000, 3.2000, 4.4000, 4.3000, 3.3000], [3.0000, 6.0000, 7.0000, 3.0000, 2.0000]]) tensor([[7.8000, 3.2000, 4.4000, 4.3000, 3.3000], [3.0000, 6.0000, 7.0000, 3.0000, 2.0000]]) Comment More infoAdvertise with us Next Article Tensor.detach() Method in Python PyTorch sravankumar_171fa07058 Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads Python - PyTorch is_tensor() method PyTorch torch.is_tensor() method returns True if the passed object is a PyTorch tensor. Syntax: torch.is_tensor(object) Arguments object: This is input tensor to be tested. Return: It returns either True or False. Let's see this concept with the help of few examples: Example 1: Python3 # Importing t 1 min read How Does the "View" Method Work in Python PyTorch? PyTorch, a popular open-source machine learning library, is known for its dynamic computational graphs and intuitive interface, particularly when it comes to tensor operations. One of the most commonly used tensor operations in PyTorch is the .view() function. If you're working with PyTorch, underst 5 min read Python - PyTorch is_storage() method PyTorch torch.is_storage() method returns True if obj is a PyTorch storage object. Syntax: torch.is_storage(object) Arguments object: This is input tensor to be tested. Return: It returns either True or False. Let's see this concept with the help of few examples: Example 1: Python3 # Importing the P 1 min read Python - PyTorch is_storage() method PyTorch torch.is_storage() method returns True if obj is a PyTorch storage object. Syntax: torch.is_storage(object) Arguments object: This is input tensor to be tested. Return: It returns either True or False. Let's see this concept with the help of few examples: Example 1: Python3 # Importing the P 1 min read Python - PyTorch div() method PyTorch torch.div() method divides every element of the input with a constant and returns a new modified tensor. Syntax: torch.div(inp, other, out=None) Arguments inp: This is input tensor. other: This is a number to be divided to each element of input inp. out: The output tensor. Return: It returns 1 min read Python - PyTorch frac() method PyTorch torch.frac() method computes the fractional portion of each element in input. Syntax: torch.frac(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1: Python3 # Importing t 1 min read Python - PyTorch ceil() method PyTorch torch.ceil() method returns a new tensor having the ceil value of the elements of input, Which is the smallest integer larger than or equal to each element. Syntax: torch.ceil(inp, out=None) Arguments inp: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see t 1 min read Python Pytorch empty() method PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.empty() returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument size. Syntax: 1 min read Python PyTorch zeros() method PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.zeros() returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size. Syntax: torch.zeros 1 min read Python - PyTorch add() method PyTorch torch.add() method adds a constant value to each element of the input tensor and returns a new modified tensor. Syntax: torch.add(inp, c, out=None) Arguments inp: This is input tensor. c: The value that is to be added to every element of tensor. out: This is optional parameter and it is the 1 min read Like