Multitask Learning with Hard Parameter Sharing - Complete Guide

In traditional machine learning workflows, models are trained independently for distinct prediction tasks. However, training separate models for related problems ignores shared underlying representations, increases computational overhead, and requires larger labeled datasets per task.

Multitask learning (MTL) addresses this by training a single model to solve multiple tasks simultaneously, leveraging shared representations to improve generalization. Hard parameter sharing is the most common MTL approach, where hidden layers are shared between all tasks while keeping several task-specific output layers intact.

Shared Backbone & Task-Specific Heads Architecture

The architecture of a hard parameter sharing network is partitioned into two primary functional components:

  • Shared Backbone Encoder: Early and intermediate neural network layers (such as shared convolutional blocks or transformer encoders) are shared across all tasks, forcing the network to learn a universal feature representation.
  • Task-Specific Output Heads: Separate, task-specific linear layers or small MLPs branch off from the shared backbone to predict individual target labels (e.g., classification head vs. regression head).
  • Gradient Balancing: Because different tasks contribute gradients of varying magnitudes to the shared backbone, gradient scaling and loss weighting are essential to prevent dominant tasks from degrading performance on secondary tasks.

PyTorch Implementation of Hard Parameter Sharing

Here is a complete PyTorch training script demonstrating how to build and train a multitask model with a shared encoder backbone and multiple task-specific output heads:

Python
PyTorch implementation of a multitask model with hard parameter sharing
import torch
import torch.nn as nn
import torch.nn.functional as F

class MultitaskHardSharingModel(nn.Module):
    def __init__(self, input_dim, shared_hidden_dim, num_classes_task1, num_outputs_task2):
        super(MultitaskHardSharingModel, self).__init__()
        # 1. Shared backbone encoder layers
        self.shared_backbone = nn.Sequential(
            nn.Linear(input_dim, shared_hidden_dim),
            nn.ReLU(),
            nn.Linear(shared_hidden_dim, shared_hidden_dim),
            nn.ReLU()
        )
        
        # 2. Task 1 specific output head (e.g., Classification)
        self.task1_head = nn.Linear(shared_hidden_dim, num_classes_task1)
        
        # 3. Task 2 specific output head (e.g., Regression)
        self.task2_head = nn.Linear(shared_hidden_dim, num_outputs_task2)

    def forward(self, x):
        # Pass input through shared feature extractor
        shared_features = self.shared_backbone(x)
        
        # Compute task-specific outputs
        out_task1 = self.task1_head(shared_features)
        out_task2 = self.task2_head(shared_features)
        
        return out_task1, out_task2

if __name__ == "__main__":
    # Mock test execution
    model = MultitaskHardSharingModel(input_dim=64, shared_hidden_dim=32, num_classes_task1=3, num_outputs_task2=1)
    sample_input = torch.randn(16, 64)
    
    preds_t1, preds_t2 = model(sample_input)
    print(f"Task 1 predictions shape: {preds_t1.shape}")
    print(f"Task 2 predictions shape: {preds_t2.shape}")

Multitask Learning Best Practices

When deploying hard parameter sharing models in production engineering environments, follow these guidelines:

  • Task Relatedness: Ensure your selected tasks share underlying statistical properties; forcing completely unrelated tasks into a shared backbone often leads to negative transfer and reduced accuracy.
  • Dynamic Loss Weighting: Use uncertainty weighting or GradNorm algorithms instead of static manual weights to balance loss gradients across competing tasks automatically.
  • Monitor Task Convergence: Track individual task losses separately during training to identify if one task is stalling or destabilizing the shared encoder backbone.