What does detach function do in pytorch

This recipe explains what does detach function do in pytorch

Recipe Objective

What does detach function do?

In the way of operations which are recorded as directed graph, in this order we have to enable the automatic differentiation as PyTorch keeps tracking all the operations which involves tensors for which the gradient may need to be computed which is require_grad = True. The Detach() method which constructs a new view on a tensor which is declared not needing the gradients i.e we can say it is to be excluded from the further tracking operations and as the subgraph involving this view is not recorded.

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

Step 1 - Import library

import torch
from torchviz import make_dot
from PIL import Image

Step 2 - Take Sample data

a = torch.zeros(20, requires_grad=True)
b = a**2
c = a**3
d = (b+c).sum()
make_dot(d).render("with_gradients_Pytorch_Exercise_86", format="png") Image.open("/content/with_gradients_Pytorch_Exercise_86.png")

This is sample data on which we have performed operations as with gradients, keeping the gradients as true we can see. After that by using PIL we have load the image for the reference.

Step 3 - Some modification

d.backward()
a.grad

tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

Step 4 - Detach function

b = a**2
c = a.detach()**3
d = (b+c).sum()
make_dot(d).render("with_detached_Pytorch_exercise_86", format="png") Image.open("/content/with_detached_Pytorch_exercise_86.png")

Here we can see the difference between both images, as after applying the detach we see the difference between both images.

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .