WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Expert · question 62 of 100

How do you perform multi-task learning in PyTorch, where a single model is trained to perform multiple tasks simultaneously?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

Multi-task learning (MTL) is a deep learning technique that enables training a single neural network model to perform multiple tasks simultaneously. PyTorch, being one of the most popular deep learning frameworks, provides various functionalities and libraries for implementing MTL.

Here is the general process of implementing MTL in PyTorch:

1. Define the model architecture: Define a shared model architecture that can handle inputs from multiple tasks. The shared model contains both a set of shared layers and layers that are specific to each task.

2. Define the loss functions: The goal of MTL is to optimize multiple loss functions simultaneously. Therefore, each task must have its own loss function. Define the loss functions for each task.

3. Define the optimizer: Use an optimizer to minimize the joint loss function.

4. Train the model: Train the model using the shared architecture, multiple loss functions and an optimizer.

5. Evaluate and adjust the model: Evaluate the performance of the model on each task and make necessary adjustments to improve performance.

Here is an example code snippet for implementing MTL in PyTorch:

import torch
import torch.nn as nn
import torch.optim as optim


class MultiTaskModel(nn.Module):
    def __init__(self, num_classes1, num_classes2):
        super(MultiTaskModel, self).__init__()
        self.shared_layer1 = nn.Linear(in_features=100, out_features=50)
        self.shared_layer2 = nn.Linear(in_features=50, out_features=10)
        self.task1_layer = nn.Linear(in_features=10, out_features=num_classes1)
        self.task2_layer = nn.Linear(in_features=10, out_features=num_classes2)

    def forward(self, x):
        x = self.shared_layer1(x)
        x = self.shared_layer2(x)
        out_task1 = self.task1_layer(x)
        out_task2 = self.task2_layer(x)
        return out_task1, out_task2
        
        
model = MultiTaskModel(num_classes1=2, num_classes2=3)
criterion1 = nn.CrossEntropyLoss()
criterion2 = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

for epoch in range(num_epochs):
    for i, data in enumerate(train_loader, 0):
        inputs, target1, target2 = data
        optimizer.zero_grad()
        output1, output2 = model(inputs)
        loss1 = criterion1(output1, target1)
        loss2 = criterion2(output2, target2)

        loss = loss1 + loss2
        loss.backward()
        optimizer.step()

        # Print out statistics
        running_loss += loss.item()
        if i % 2000 == 1999:
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0
            
# Evaluate model on test set
with torch.no_grad():
    for data in test_loader:
        inputs, target1, target2 = data
        output1, output2 = model(inputs)
        _, predicted1 = torch.max(output1.data, 1)
        _, predicted2 = torch.max(output2.data, 1)
        # Calculate accuracy for both tasks

In this example, we define a MultiTaskModel class that inherits from the nn.Module class. The MultiTaskModel contains two fully connected linear layers for each task, with the number of output classes specified during initialization. Inside the forward() method, the shared layers are passed the input, followed by the task-specific layers, and finally, the output of each task is returned.

We create a criterion instance for each task i.e., CrossEntorpyLoss in this case. We also create an optimizer that optimizes the model parameters during the training phase.

During the training phase, we pass each batch of data, inputs, target1, and target2 to the model. We pass the inputs through the shared layers of the model and then pass them to the layers specific to each task to obtain the output. We calculate the loss for each task separately and add them to get the overall loss. We then backpropagate the loss and update the model weights using the optimizer.

During the evaluation phase, we pass the test data to the model and extract the predictions for each task. We calculate the accuracy for each task separately.

In summary, MTL is a powerful approach that allows multiple tasks to be jointly learned using a single model. When using PyTorch, you define a shared architecture that handles the input from multiple tasks, define the loss functions for each task separately, and use an optimizer to jointly optimize the loss of all tasks.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics