在PyTorch中處理多任務學習通常有兩種方法:
class MultiTaskModel(nn.Module):
def __init__(self):
super(MultiTaskModel, self).__init__()
self.shared_layers = nn.Sequential(
nn.Linear(100, 50),
nn.ReLU()
)
self.task1_output = nn.Linear(50, 10)
self.task2_output = nn.Linear(50, 5)
def forward(self, x):
x = self.shared_layers(x)
output1 = self.task1_output(x)
output2 = self.task2_output(x)
return output1, output2
model = MultiTaskModel()
criterion = nn.CrossEntropyLoss()
output1, output2 = model(input)
loss = 0.5 * criterion(output1, target1) + 0.5 * criterion(output2, target2)
class SharedFeatureExtractor(nn.Module):
def __init__(self):
super(SharedFeatureExtractor, self).__init__()
self.layers = nn.Sequential(
nn.Linear(100, 50),
nn.ReLU()
)
def forward(self, x):
return self.layers(x)
class MultiTaskModel(nn.Module):
def __init__(self):
super(MultiTaskModel, self).__init__()
self.shared_feature_extractor = SharedFeatureExtractor()
self.task1_output = nn.Linear(50, 10)
self.task2_output = nn.Linear(50, 5)
def forward(self, x):
x = self.shared_feature_extractor(x)
output1 = self.task1_output(x)
output2 = self.task2_output(x)
return output1, output2
model = MultiTaskModel()
criterion = nn.CrossEntropyLoss()
output1, output2 = model(input)
loss = 0.5 * criterion(output1, target1) + 0.5 * criterion(output2, target2)
無論采用哪種方法,都需要根據任務的不同設置不同的損失函數,并且根據實際情況調整不同任務之間的權重。