<jupyter_start><jupyter_text>Load a toy dataset<jupyter_code>import torch
from tqdm import tqdm

from pytorch_adapt.datasets import get_mnist_mnistm

# mnist is the source domain
# mnistm is the target domain
datasets = get_mnist_mnistm(["mnist"], ["mnistm"], ".", download=True)
dataloader = torch.utils.data.DataLoader(
    datasets["train"], batch_size=32, num_workers=2
)<jupyter_output><empty_output><jupyter_text>Load toy models<jupyter_code>from pytorch_adapt.models import Discriminator, mnistC, mnistG

device = torch.device("cuda")


def get_models():
    G = mnistG(pretrained=True).to(device)
    C = mnistC(pretrained=True).to(device)
    D = Discriminator(in_size=1200, h=256).to(device)
    return {"G": G, "C": C, "D": D}


def get_optimizers(models):
    G_opt = torch.optim.Adam(models["G"].parameters(), lr=0.0001)
    C_opt = torch.optim.Adam(models["C"].parameters(), lr=0.0001)
    D_opt = torch.optim.Adam(models["D"].parameters(), lr=0.0001)
    return [G_opt, C_opt, D_opt]<jupyter_output><empty_output><jupyter_text>Use in vanilla PyTorch<jupyter_code>from pytorch_adapt.hooks import DANNHook
from pytorch_adapt.utils.common_functions import batch_to_device

models = get_models()
optimizers = get_optimizers(models)

# Assuming that models, optimizers, and dataloader are already created.
hook = DANNHook(optimizers)
for data in tqdm(dataloader):
    data = batch_to_device(data, device)
    # Optimization is done inside the hook.
    # The returned loss is for logging.
    loss, _ = hook({}, {**models, **data})<jupyter_output><empty_output><jupyter_text>Build complex algorithms<jupyter_code>from pytorch_adapt.hooks import MCCHook, VATHook

models = get_models()
optimizers = get_optimizers(models)

# G and C are the Generator and Classifier models
G, C = models["G"], models["C"]
misc = {"combined_model": torch.nn.Sequential(G, C)}
hook = DANNHook(optimizers, post_g=[MCCHook(), VATHook()])
for data in tqdm(dataloader):
    data = batch_to_device(data, device)
    loss, _ = hook({}, {**models, **data, **misc})<jupyter_output><empty_output><jupyter_text>Wrap with your favorite PyTorch framework<jupyter_code>from pytorch_adapt.adapters import DANN
from pytorch_adapt.containers import Models
from pytorch_adapt.datasets import DataloaderCreator

models = get_models()
models_cont = Models(models)
adapter = DANN(models=models_cont)
dc = DataloaderCreator(num_workers=2)
dataloaders = dc(**datasets)<jupyter_output><empty_output><jupyter_text>Lightning<jupyter_code>import pytorch_lightning as pl

from pytorch_adapt.frameworks.lightning import Lightning

L_adapter = Lightning(adapter)
trainer = pl.Trainer(gpus=1, max_epochs=1)
trainer.fit(L_adapter, dataloaders["train"])<jupyter_output><empty_output><jupyter_text>Ignite<jupyter_code>from pytorch_adapt.frameworks.ignite import Ignite

models = get_models()
models_cont = Models(models)
adapter = DANN(models=models_cont)

trainer = Ignite(adapter)
trainer.run(datasets, dataloader_creator=dc)<jupyter_output><empty_output><jupyter_text>Check your model's performance<jupyter_code>from pytorch_adapt.validators import SNDValidator

# Random predictions as placeholder
preds = torch.randn(1000, 100)

# Assuming predictions have been collected
target_train = {"preds": preds}
validator = SNDValidator()
score = validator.score(target_train=target_train)<jupyter_output><empty_output><jupyter_text>Lightning<jupyter_code>from pytorch_adapt.frameworks.utils import filter_datasets

models = get_models()
models_cont = Models(models)
adapter = DANN(models=models_cont)
validator = SNDValidator()
dataloaders = dc(**filter_datasets(datasets, validator))
train_loader = dataloaders.pop("train")

L_adapter = Lightning(adapter, validator=validator)
trainer = pl.Trainer(gpus=1, max_epochs=1)
trainer.fit(L_adapter, train_loader, list(dataloaders.values()))<jupyter_output><empty_output><jupyter_text>Ignite<jupyter_code>from pytorch_adapt.validators import ScoreHistory

models = get_models()
models_cont = Models(models)
adapter = DANN(models=models_cont)

validator = ScoreHistory(SNDValidator())
trainer = Ignite(adapter, validator=validator)
trainer.run(datasets, dataloader_creator=dc)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11584205518553758, 'fraction_numerical': 0.008801141769743101, 'mean_word_length': 7.815513626834382, 'pattern_counts': {'":': 5, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '582887', 'license': 'MIT', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1508, 'n_words': 283, 'path': 'examples/other/ReadmeExamples.ipynb', 'repo_name': 'MarkusSagen/pytorch-adapt', 'size': '4204'}