在Torch中進行遷移學習通常涉及以下步驟:
import torchvision.models as models
model = models.resnet18(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, num_classes)
for param in model.parameters():
param.requires_grad = False
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
for epoch in range(num_epochs):
for inputs, labels in dataloader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
這樣,你就可以在Torch中進行遷移學習了。根據(jù)具體的任務(wù)和數(shù)據(jù)集,可能需要調(diào)整模型結(jié)構(gòu)和訓練策略。