-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
base: main
Are you sure you want to change the base?
Add pretrained models guide #5913
Conversation
There was a problem hiding this 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 🚀
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' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**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. |
- `<model_name>`: Replace this with the desired model name (e.g., `resnet18`, `vgg16`). | ||
- `pretrained=True`: Specifies that the model should load pretrained weights. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `<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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```python | |
```py |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
Description
Issue Solved
#5869
Type of Change
Checklist
main
branch.Issues Solved
section.