Divergence domain adaptation methods.

This example illustrates the DeepCoral method from [1] on a simple image classification task.

# Author: Théo Gnassounou
#
# License: BSD 3-Clause
# sphinx_gallery_thumbnail_number = 4
from skorch import NeuralNetClassifier
from torch import nn

from skada.datasets import load_mnist_usps
from skada.deep import DeepCoral
from skada.deep.modules import MNISTtoUSPSNet

Load the image datasets

dataset = load_mnist_usps(n_classes=2, n_samples=0.5, return_dataset=True)
X, y, sample_domain = dataset.pack_train(as_sources=["mnist"], as_targets=["usps"])
X_test, y_test, sample_domain_test = dataset.pack_test(as_targets=["usps"])
  0%|          | 0.00/9.91M [00:00<?, ?B/s]
100%|██████████| 9.91M/9.91M [00:00<00:00, 145MB/s]

  0%|          | 0.00/28.9k [00:00<?, ?B/s]
100%|██████████| 28.9k/28.9k [00:00<00:00, 59.4MB/s]

  0%|          | 0.00/1.65M [00:00<?, ?B/s]
100%|██████████| 1.65M/1.65M [00:00<00:00, 178MB/s]

  0%|          | 0.00/4.54k [00:00<?, ?B/s]
100%|██████████| 4.54k/4.54k [00:00<00:00, 12.1MB/s]
/home/circleci/project/skada/datasets/_mnist_usps.py:72: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  mnist_target = torch.tensor(mnist_dataset.targets)

  0%|          | 0.00/1.83M [00:00<?, ?B/s]
  2%|▏         | 32.8k/1.83M [00:00<00:13, 138kB/s]
  4%|▎         | 65.5k/1.83M [00:00<00:12, 138kB/s]
  7%|▋         | 131k/1.83M [00:00<00:08, 201kB/s]
 13%|█▎        | 229k/1.83M [00:00<00:05, 285kB/s]
 25%|██▌       | 459k/1.83M [00:01<00:02, 530kB/s]
 50%|█████     | 918k/1.83M [00:01<00:00, 1.01MB/s]
 98%|█████████▊| 1.80M/1.83M [00:01<00:00, 1.90MB/s]
100%|██████████| 1.83M/1.83M [00:01<00:00, 1.10MB/s]

Train a classic model

model = NeuralNetClassifier(
    MNISTtoUSPSNet(),
    criterion=nn.CrossEntropyLoss(),
    batch_size=128,
    max_epochs=5,
    train_split=False,
    lr=1e-2,
)
model.fit(X[sample_domain > 0], y[sample_domain > 0])
model.score(X_test, y=y_test)
  epoch    train_loss     dur
-------  ------------  ------
      1        1.6348  3.6962
      2        0.3176  3.4033
      3        0.1082  3.2970
      4        0.0643  3.3030
      5        0.0456  3.4945

0.8842443729903537

Train a DeepCoral model

model = DeepCoral(
    MNISTtoUSPSNet(),
    layer_name="fc1",
    batch_size=128,
    max_epochs=5,
    train_split=False,
    reg=1,
    lr=1e-2,
)
model.fit(X, y, sample_domain=sample_domain)
model.score(X_test, y_test, sample_domain=sample_domain_test)
  epoch    train_loss     dur
-------  ------------  ------
      1        1.4456  7.0768
      2        0.2629  6.5027
      3        0.1114  6.7957
      4        0.0737  6.6992
      5        0.0648  7.1999

0.9517684887459807

Total running time of the script: (1 minutes 0.276 seconds)

Gallery generated by Sphinx-Gallery