Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add knowledge_distillation_tutorial #2514

Merged
merged 33 commits into from
Aug 22, 2023

Conversation

AlexandrosChrtn
Copy link
Contributor

@AlexandrosChrtn AlexandrosChrtn commented Jul 31, 2023

Includes a Knowledge distillation tutorial.
From this tutorial one can learn:

  • How to modify model classes to extract hidden representations and use them for further calculations
  • How to modify regular train loops in pytorch to include additional losses on top of, for example, cross-entropy for classification
  • How to improve the performance of lightweight models, using more complex models as teachers

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 31, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/2514

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit cafd91e with merge base e7c86fd (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

Hi @AlexandrosChrtn!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@netlify
Copy link

netlify bot commented Jul 31, 2023

Deploy Preview for pytorch-tutorials-preview failed.

Name Link
🔨 Latest commit 79b7467
🔍 Latest deploy log https://app.netlify.com/sites/pytorch-tutorials-preview/deploys/64c7ddfb7a63f7000843ae3c

@AlexandrosChrtn
Copy link
Contributor Author

@svekars Hello! I get this when deploying, looks related to recent changes in requirements.txt

ERROR: No matching distribution found for scipy==1.11.1 (from -r requirements.txt (line 50))
Error installing pip dependencies
Build was terminated: dependency_installation script returned non-zero exit code: 1
Failing build: Failed to install dependencies

@svekars
Copy link
Contributor

svekars commented Aug 7, 2023

@AlexandrosChrtn if you are referring to the Netlify error, it can be safely ignored for now. GitHub Actions checks are the ones we are looking at.

@svekars
Copy link
Contributor

svekars commented Aug 7, 2023

Can you please add a better description for this tutorial describing how the PyTorch community benefits from this tutorial?

@AlexandrosChrtn
Copy link
Contributor Author

@svekars Added a short description with bullets. Let me know if you need anything else!

@svekars svekars requested review from malfet and albanD August 14, 2023 15:08
Copy link
Contributor

@albanD albanD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

])

# Data preprocessing for CIFAR-10
transform_test = transforms.Compose([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the same as train one?

soft_targets = nn.functional.softmax(teacher_logits / T, dim=-1)
soft_prob = nn.functional.log_softmax(student_logits / T, dim=-1)

# Calculate the soft targets loss. Scaled by T**2 as suggested by the authors of the paper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which paper?

Copy link
Contributor

@albanD albanD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update.
That sounds good to me cc @svekars

Copy link
Contributor

@svekars svekars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much, @AlexandrosChrtn. I added some editorial suggestions. A couple of things, in addition to that:

  • Can you some headings that would better break your tutorial to sections (I made some suggestions in my review)
  • Please break lines at 79 symbols where possible so that it's easier to edit in the future.

Let me know if you have any questions.

beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
import torchvision.transforms as transforms
import torchvision.datasets as datasets

# Ignore warnings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an explanation to the comment why we ignore them

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

######################################################################
# CIFAR-10 is a popular image dataset with 10 classes. Our objective is to predict one of the following classes for each input image.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a heading 2 here?

beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
@svekars
Copy link
Contributor

svekars commented Aug 15, 2023

Also, make sure to check the html output here and fix the spellcheck by enclosing CosineLoss in double backticks (``).

@AlexandrosChrtn
Copy link
Contributor Author

Thanks everyone for the insightful suggestions! @svekars I think I fixed everything based on your proposals, except for the conclusion part, where it feels repetitive to state everything the user learned, becuase we do so in the beginning, however I can still add a few lines if you think it's best. I added sections and removed the lines about filtering warnings. The only reason they were there at the first place is because I saw them in other tutorials (I speculate people add them because of deprecation warnings due to version mismatches and what not).

Copy link
Contributor

@svekars svekars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final cleanup

beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
beginner_source/knowledge_distillation_tutorial.py Outdated Show resolved Hide resolved
@svekars svekars merged commit 827843f into pytorch:main Aug 22, 2023
18 checks passed
mikaylagawarecki pushed a commit to mikaylagawarecki/tutorials that referenced this pull request Sep 18, 2023
* Add knowledge_distillation_tutorial
---------

Co-authored-by: Svetlana Karslioglu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants