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

[Concept Entry] Created the export entry #6127

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dakshdeepHERE
Copy link
Collaborator

Description

Issue Solved

Type of Change

  • Adding a new entry
  • Editing an existing entry (fixing a typo, bug, issues, etc)
  • Updating the documentation

Checklist

  • All writings are my own.
  • My entry follows the Codecademy Docs style guide.
  • My changes generate no new warnings.
  • I have performed a self-review of my own writing and code.
  • I have checked my entry and corrected any misspellings.
  • I have made corresponding changes to the documentation if needed.
  • I have confirmed my changes are not being pushed from my forked main branch.
  • I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • I have linked any issues that are relevant to this PR in the Issues Solved section.

@dakshdeepHERE dakshdeepHERE added new entry New entry or entries pytorch PyTorch labels Feb 10, 2025
Copy link
Collaborator

@PragatiVerma18 PragatiVerma18 left a comment

Choose a reason for hiding this comment

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

Hey @dakshdeepHERE, I have a few suggestions, please check the comments and make the necessary changes.

Comment on lines +2 to +15
title: Model Export with TorchScript
description: Convert PyTorch models to TorchScript for production deployment in non-Python environments.
subject:
- 'Computer Science'
- 'Data Science'
- 'Machine Learning'
tags:
- 'Deployment'
- 'TorchScript'
- 'Model Export'
- 'PyTorch'
catalog_content:
- 'learn-pytorch'
- 'paths/data-science'
Copy link
Collaborator

Choose a reason for hiding this comment

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

The headers Title, Description, Subject, and Tags should be in title case, and the sentences should be inside ''.

Suggested change
title: Model Export with TorchScript
description: Convert PyTorch models to TorchScript for production deployment in non-Python environments.
subject:
- 'Computer Science'
- 'Data Science'
- 'Machine Learning'
tags:
- 'Deployment'
- 'TorchScript'
- 'Model Export'
- 'PyTorch'
catalog_content:
- 'learn-pytorch'
- 'paths/data-science'
Title: 'Model Export with TorchScript'
Description: 'Convert PyTorch models to TorchScript for optimized deployment in non-Python environments like mobile and embedded systems.'
Subject:
- 'Computer Science'
- 'Data Science'
- 'Machine Learning'
Tags:
- 'Deployment'
- 'TorchScript'
- 'Model Export'
- 'PyTorch'
CatalogContent:
- 'learn-pytorch'
- 'paths/data-science'

Comment on lines +51 to +62
import torch

class DynamicModel(torch.nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
if x.sum() > 0:
return x * 2
else:
return x - 1

model = DynamicModel()
scripted_model = torch.jit.script(model) # Handles dynamic control flow
scripted_model.save("dynamic_model.pt")
Copy link
Collaborator

Choose a reason for hiding this comment

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

The example should give some tangible output that helps the reader understand what's happening -

Suggested change
import torch
class DynamicModel(torch.nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
if x.sum() > 0:
return x * 2
else:
return x - 1
model = DynamicModel()
scripted_model = torch.jit.script(model) # Handles dynamic control flow
scripted_model.save("dynamic_model.pt")
import torch
class DynamicModel(torch.nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
if x.sum() > 0:
return x * 2
else:
return x - 1
model = DynamicModel()
scripted_model = torch.jit.script(model) # Handles dynamic control flow
scripted_model.save("dynamic_model.pt")
# Testing the scripted model
x1 = torch.tensor([1.0, -0.5, 3.0])
x2 = torch.tensor([-2.0, -1.5, -0.5])
print(scripted_model(x1))
print(scripted_model(x2))

Add output below this -

tensor([ 2., -1.,  6.])
tensor([-3.0000, -2.5000, -1.5000])

Comment on lines +70 to +78
import torch
import torchvision

model = torchvision.models.resnet18(weights="IMAGENET1K_V1").eval()

# Trace with example input
dummy_input = torch.rand(1, 3, 224, 224)
traced_model = torch.jit.trace(model, dummy_input)
traced_model.save("resnet18_traced.pt")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. The example should have an output -

Suggested change
import torch
import torchvision
model = torchvision.models.resnet18(weights="IMAGENET1K_V1").eval()
# Trace with example input
dummy_input = torch.rand(1, 3, 224, 224)
traced_model = torch.jit.trace(model, dummy_input)
traced_model.save("resnet18_traced.pt")
import torch
import torchvision
model = torchvision.models.resnet18(weights="IMAGENET1K_V1").eval()
# Trace with example input
dummy_input = torch.rand(1, 3, 224, 224)
traced_model = torch.jit.trace(model, dummy_input)
traced_model.save("resnet18_traced.pt")
# Running inference with traced model
output = traced_model(dummy_input)
print(output.shape)

Add output below that:

torch.Size([1, 1000])

This confirms that the traced ResNet model processes an image and produces 1000 output logits (corresponding to ImageNet classes).

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.

[Concept Entry] PyTorch: Model Export with TorchScript
2 participants