在PyTorch中,two-head网络是一种常用的网络结构,用于处理多任务学习问题。本文将提供一个完整的攻略,介绍如何使用PyTorch训练two-head网络。我们将提供两个示例,分别是使用nn.ModuleList和使用nn.Sequential。
示例1:使用nn.ModuleList
以下是一个示例,展示如何使用nn.ModuleList训练two-head网络。
1. 导入库
import torch
import torch.nn as nn
import torch.optim as optim
2. 定义模型
class TwoHeadNet(nn.Module):
def __init__(self):
super(TwoHeadNet, self).__init__()
self.shared_layers = nn.Sequential(
nn.Conv2d(3, 6, 5),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(6, 16, 5),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Flatten()
)
self.head1_layers = nn.ModuleList([
nn.Linear(16 * 5 * 5, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 10)
])
self.head2_layers = nn.ModuleList([
nn.Linear(16 * 5 * 5, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 2)
])
def forward(self, x):
x = self.shared_layers(x)
out1 = x
out2 = x
for layer in self.head1_layers:
out1 = layer(out1)
for layer in self.head2_layers:
out2 = layer(out2)
return out1, out2
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = TwoHeadNet().to(device)
3. 定义损失函数和优化器
criterion1 = nn.CrossEntropyLoss()
criterion2 = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
4. 训练模型
num_epochs = 10
for epoch in range(num_epochs):
running_loss1 = 0.0
running_loss2 = 0.0
for i, (inputs, labels1, labels2) in enumerate(trainloader, 0):
inputs = inputs.to(device)
labels1 = labels1.to(device)
labels2 = labels2.to(device)
optimizer.zero_grad()
outputs1, outputs2 = model(inputs)
loss1 = criterion1(outputs1, labels1)
loss2 = criterion2(outputs2, labels2)
loss = loss1 + loss2
loss.backward()
optimizer.step()
running_loss1 += loss1.item()
running_loss2 += loss2.item()
if i % 2000 == 1999: # 每2000个小批量数据打印一次损失值
print('[%d, %5d] loss1: %.3f, loss2: %.3f' %
(epoch + 1, i + 1, running_loss1 / 2000, running_loss2 / 2000))
running_loss1 = 0.0
running_loss2 = 0.0
print('Finished Training')
示例2:使用nn.Sequential
以下是一个示例,展示如何使用nn.Sequential训练two-head网络。
1. 导入库
import torch
import torch.nn as nn
import torch.optim as optim
2. 定义模型
class TwoHeadNet(nn.Module):
def __init__(self):
super(TwoHeadNet, self).__init__()
self.shared_layers = nn.Sequential(
nn.Conv2d(3, 6, 5),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(6, 16, 5),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Flatten()
)
self.head1_layers = nn.Sequential(
nn.Linear(16 * 5 * 5, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 10)
)
self.head2_layers = nn.Sequential(
nn.Linear(16 * 5 * 5, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 2)
)
def forward(self, x):
x = self.shared_layers(x)
out1 = self.head1_layers(x)
out2 = self.head2_layers(x)
return out1, out2
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = TwoHeadNet().to(device)
3. 定义损失函数和优化器
criterion1 = nn.CrossEntropyLoss()
criterion2 = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
4. 训练模型
num_epochs = 10
for epoch in range(num_epochs):
running_loss1 = 0.0
running_loss2 = 0.0
for i, (inputs, labels1, labels2) in enumerate(trainloader, 0):
inputs = inputs.to(device)
labels1 = labels1.to(device)
labels2 = labels2.to(device)
optimizer.zero_grad()
outputs1, outputs2 = model(inputs)
loss1 = criterion1(outputs1, labels1)
loss2 = criterion2(outputs2, labels2)
loss = loss1 + loss2
loss.backward()
optimizer.step()
running_loss1 += loss1.item()
running_loss2 += loss2.item()
if i % 2000 == 1999: # 每2000个小批量数据打印一次损失值
print('[%d, %5d] loss1: %.3f, loss2: %.3f' %
(epoch + 1, i + 1, running_loss1 / 2000, running_loss2 / 2000))
running_loss1 = 0.0
running_loss2 = 0.0
print('Finished Training')
总结
本文提供了一个完整的攻略,介绍了如何使用PyTorch训练two-head网络。我们提供了两个示例,分别是使用nn.ModuleList和使用nn.Sequential。在实现过程中,我们使用了PyTorch的nn.ModuleList和nn.Sequential,并使用了CrossEntropyLoss损失函数和SGD优化器。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用Pytorch训练two-head网络的操作 - Python技术站