PyTorch Transfer Learning Guide
Model transfer learning in PyTorch typically involves the following steps:
- Load a pre-trained model: First, load a model that has been trained on a large dataset, such as ResNet trained on ImageNet.
import torch
import torchvision.models as models
pretrained_model = models.resnet18(pretrained=True)
- Change the final layer of the model: Depending on your task requirements, it is usually necessary to modify the final layer of the pre-trained model, such as replacing the fully connected layer of the pre-trained model with a fully connected layer suitable for your new task.
pretrained_model.fc = nn.Linear(pretrained_model.fc.in_features, num_classes)
- Freeze the parameters of the pre-trained model: Typically, we freeze the parameters of the pre-trained model and only train the newly added fully connected layer.
for param in pretrained_model.parameters():
param.requires_grad = False
- Define loss function and optimizer: Define appropriate loss function and optimizer based on your task requirements.
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(pretrained_model.fc.parameters(), lr=0.001)
- Train the model: train the model using a new dataset.
for epoch in range(num_epochs):
for images, labels in dataloader:
optimizer.zero_grad()
outputs = pretrained_model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
By following the above steps, you can perform model transfer learning in PyTorch. You can adjust and expand the above steps according to specific task requirements.