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 pretrained models guide #5913

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

Conversation

sarahsanders-docker
Copy link

Description

  • Created guide on Loading Pretrained Models

Issue Solved

#5869

Type of Change

  • Adding a new entry

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.

@CLAassistant
Copy link

CLAassistant commented Jan 2, 2025

CLA assistant check
All committers have signed the CLA.

@mamtawardhani mamtawardhani self-assigned this Jan 13, 2025
@mamtawardhani mamtawardhani added new entry New entry or entries status: under review Issue or PR is currently being reviewed pytorch PyTorch labels Jan 13, 2025
Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Thank you for contributing to Codecademy Docs @sarahsanders 😄

The entry looks good for a second round of review 🚀

Comment on lines 2 to 18
Title: Loading Pretrained Models
Description: 'A brief description.' # Required; ideally under 150 characters and starts with a noun (used in search engine results and content previews)
Subjects:
- Machine Learning
Tags:
- Machine Learning
- Models
- Neural Networks
- Deep Learning
- TensorFlow
- PyTorch
- Datasets
- Scikit-learn
- AI
CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first
- 'learn-example-course'
- 'paths/example-path'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Title: Loading Pretrained Models
Description: 'A brief description.' # Required; ideally under 150 characters and starts with a noun (used in search engine results and content previews)
Subjects:
- Machine Learning
Tags:
- Machine Learning
- Models
- Neural Networks
- Deep Learning
- TensorFlow
- PyTorch
- Datasets
- Scikit-learn
- AI
CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first
- 'learn-example-course'
- 'paths/example-path'
Title: 'Loading Pre-trained Models'
Description: 'Initializes a model architecture with pre-trained weights learned from a large dataset, enabling transfer learning or direct inference.'
Subjects:
- 'Machine Learning'
Tags:
- 'Machine Learning'
- 'Models'
- 'Neural Networks'
- 'Deep Learning'
- 'TensorFlow'
- 'PyTorch'
- 'Datasets'
- 'Scikit-learn'
- 'AI'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'

- 'paths/example-path'
---

**Pretrained models** are a foundational concept in machine learning, allowing developers and researchers to leverage models that have already been trained on extensive datasets. This approach accelerates development and improves accuracy by transferring knowledge from existing solutions to new tasks. In PyTorch, loading pretrained models is straightforward and provides access to many state-of-the-art models via the torchvision library and other community-supported sources.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
**Pretrained models** are a foundational concept in machine learning, allowing developers and researchers to leverage models that have already been trained on extensive datasets. This approach accelerates development and improves accuracy by transferring knowledge from existing solutions to new tasks. In PyTorch, loading pretrained models is straightforward and provides access to many state-of-the-art models via the torchvision library and other community-supported sources.
**Loading pre-trained models** is a foundational process in machine learning that enables developers and researchers to utilize models already trained on extensive datasets. This practice speeds development and enhances accuracy by reusing learned features from existing solutions for new tasks. In PyTorch, loading pre-trained models is simple and accessible, offering a range of state-of-the-art models through libraries like `torchvision` and other community-contributed sources.

Comment on lines 36 to 37
- `<model_name>`: Replace this with the desired model name (e.g., `resnet18`, `vgg16`).
- `pretrained=True`: Specifies that the model should load pretrained weights.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- `<model_name>`: Replace this with the desired model name (e.g., `resnet18`, `vgg16`).
- `pretrained=True`: Specifies that the model should load pretrained weights.
- `<model_name>`: The name of the model to load (e.g., `resnet18`, `vgg16`, `mobilenet_v2`, etc.). It must be a valid model name from the `torchvision.models` library.
- `pretrained`: If `True`, the function loads a model initialized with weights pre-trained on the ImageNet dataset. If `False`, it loads a model with random weights.


### Using a Pretrained ResNet-18 for Inference

This example demonstrates loading a pretrained ResNet-18 model and using it for image classification.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
This example demonstrates loading a pretrained ResNet-18 model and using it for image classification.
This example demonstrates how to load a pre-trained ResNet-18 model and use it for image classification:


### Fine-Tuning a Pretrained Model

Here is an example of modifying a pretrained ResNet-18 model for a custom classification task:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Here is an example of modifying a pretrained ResNet-18 model for a custom classification task:
The following example shows how to modify a pre-trained ResNet-18 model for a custom classification task:


Here is an example of modifying a pretrained ResNet-18 model for a custom classification task:

```python
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
```python
```py

Comment on lines 81 to 94
import torch.nn as nn

# Load pretrained model
model = models.resnet18(pretrained=True)

# Modify the final fully connected layer to match the number of output classes
num_classes = 10 # Adjust based on your dataset
model.fc = nn.Linear(model.fc.in_features, num_classes)

# Define loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

# The model is now ready for training on your dataset
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import torch.nn as nn
# Load pretrained model
model = models.resnet18(pretrained=True)
# Modify the final fully connected layer to match the number of output classes
num_classes = 10 # Adjust based on your dataset
model.fc = nn.Linear(model.fc.in_features, num_classes)
# Define loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# The model is now ready for training on your dataset
import torch.nn as nn
from torchvision import models
# Load the pretrained ResNet-18 model
model = models.resnet18(pretrained=True)
# Modify the final fully connected layer to match the number of output classes
num_classes = 10 # Replace with the number of classes in your custom dataset
model.fc = nn.Linear(model.fc.in_features, num_classes)
# Define loss and optimizer
criterion = nn.CrossEntropyLoss() # Loss function for classification
optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # Optimizer for training
# The model is now ready for fine-tuning on your dataset

@Radhika-okhade Radhika-okhade self-assigned this Jan 15, 2025
@Radhika-okhade Radhika-okhade added status: under review Issue or PR is currently being reviewed and removed status: ready for next review labels Jan 15, 2025
@mamtawardhani mamtawardhani marked this pull request as ready for review January 18, 2025 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new entry New entry or entries pytorch PyTorch status: review 1️⃣ completed status: under review Issue or PR is currently being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants