← Back to home
Jul 30, 2026MR. ERROR 3133 min read

Domain Adaptation

Domain adaptation techniques enable models to perform well on unseen data. This tutorial covers the core concept and provides a worked example.

machine-learningdomain-adaptationpytorch
Domain Adaptation

Introduction to Domain Adaptation

Domain adaptation is a subfield of transfer learning that focuses on adapting a model trained on a source domain to perform well on a target domain. The core concept of domain adaptation is to bridge the gap between the source and target domains by minimizing the difference in their distributions.

Context and Importance

In real-world applications, it is common to encounter situations where the training data (source domain) and the data the model will be applied to (target domain) have different distributions. For example, a model trained on images taken in daylight may not perform well on images taken at night. Domain adaptation techniques can help mitigate this issue by adapting the model to the new environment.

Core Concept

The core concept of domain adaptation is based on the idea of minimizing the difference between the source and target domains. This can be achieved by using techniques such as:

  • Domain-invariant feature learning: learning features that are invariant to the domain shift
  • Domain adversarial training: training the model to be domain-agnostic by using an adversarial loss function
  • Maximum mean discrepancy (MMD): measuring the difference between the source and target domains using MMD

Worked Example

Let's consider an example using PyTorch, where we want to adapt a model trained on the MNIST dataset (source domain) to perform well on the USPS dataset (target domain).

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms

# Define the model
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(28*28, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = x.view(-1, 28*28)
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# Load the MNIST and USPS datasets
transform = transforms.Compose([transforms.Resize((28, 28)), transforms.ToTensor()])
mnist_train = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
usps_train = datasets.USPS('~/.pytorch/USPS_data/', download=True, train=True, transform=transform)

# Train the model on the MNIST dataset
model = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
for epoch in range(10):
    for x, y in mnist_train:
        x, y = x.to(device), y.to(device)
        optimizer.zero_grad()
        output = model(x)
        loss = criterion(output, y)
        loss.backward()
        optimizer.step()

# Adapt the model to the USPS dataset using domain adversarial training
class DomainAdversarialLoss(nn.Module):
    def __init__(self):
        super(DomainAdversarialLoss, self).__init__()
        self.fc1 = nn.Linear(128, 128)
        self.fc2 = nn.Linear(128, 1)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = torch.sigmoid(self.fc2(x))
        return x

domain_loss = DomainAdversarialLoss()
for epoch in range(10):
    for x, y in usps_train:
        x, y = x.to(device), y.to(device)
        optimizer.zero_grad()
        output = model(x)
        domain_output = domain_loss(output)
        loss = criterion(output, y) + 0.1 * domain_loss(output)
        loss.backward()
        optimizer.step()

Pitfalls and Challenges

Some common pitfalls and challenges in domain adaptation include:

  • Overfitting to the source domain: the model may overfit to the source domain and fail to adapt to the target domain
  • Underfitting to the target domain: the model may underfit to the target domain and fail to capture the underlying patterns
  • Choosing the wrong adaptation method: choosing the wrong adaptation method can lead to poor performance on the target domain

What to Read Next

For further reading, we recommend checking out the following resources:

  • Deep Domain Confusion (DDC): a technique that uses a domain confusion loss to adapt the model to the target domain
  • Correlation Alignment (CORAL): a technique that uses a correlation alignment loss to adapt the model to the target domain
  • Domain Adversarial Neural Networks (DANN): a technique that uses a domain adversarial loss to adapt the model to the target domain