Skip to content

Instantly share code, notes, and snippets.

@mirceast
Created May 22, 2019 14:18
Show Gist options
  • Save mirceast/8327bbbc89c5611fc35ea8404eaf36f1 to your computer and use it in GitHub Desktop.
Save mirceast/8327bbbc89c5611fc35ea8404eaf36f1 to your computer and use it in GitHub Desktop.
Transfer Learning 4
# Download a pre-trained ResNet18 model and freeze its weights
model = torchvision.models.resnet18(pretrained=True)
for param in model.parameters():
param.requires_grad = False
# Replace the final fully connected layer
# Parameters of newly constructed modules have requires_grad=True by default
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 2)
# Send the model to the GPU
model = model.to(device)
# Set the loss function
criterion = nn.CrossEntropyLoss()
# Observe that only the parameters of the final layer are being optimized
optimizer_conv = optim.SGD(model.fc.parameters(), lr=0.001, momentum=0.9)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)
model, epoch_time = train_model(model, criterion, optimizer_conv, exp_lr_scheduler, num_epochs=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment