pytorch_generative/models/made.py
def reproduce(
    n_epochs=427, batch_size=128, log_dir="/tmp/run", device="cuda", debug_loader=None
):
    """Training script with defaults to reproduce results.

    The code inside this function is self contained and can be used as a top level
    training script, e.g. by copy/pasting it into a Jupyter notebook.

    Args:
        n_epochs: Number of epochs to train for.
        batch_size: Batch size to use for training and evaluation.
        log_dir: Directory where to log trainer state and TensorBoard summaries.
        device: Device to train on (either 'cuda' or 'cpu').
        debug_loader: Debug DataLoader which replaces the default training and
            evaluation loaders if not 'None'. Do not use unless you're writing unit
            tests.
    """
    from torch import optim
    from torch import distributions
    from torch.nn import functional as F
    from torch.optim import lr_scheduler
    from torch.utils import data
    from torchvision import datasets
    from torchvision import transforms

    from pytorch_generative import trainer
    from pytorch_generative import models

    transform = transforms.Compose(
        [transforms.ToTensor(), lambda x: distributions.Bernoulli(probs=x).sample()]
    )
    train_loader = debug_loader or data.DataLoader(
        datasets.MNIST("/tmp/data", train=True, download=True, transform=transform),
        batch_size=batch_size,
        shuffle=True,
        num_workers=8,
    )
    test_loader = debug_loader or data.DataLoader(
        datasets.MNIST("/tmp/data", train=False, download=True, transform=transform),
        batch_size=batch_size,
        num_workers=8,
    )

    model = models.MADE(input_dim=784, hidden_dims=[8000], n_masks=1)
    optimizer = optim.Adam(model.parameters())

    def loss_fn(x, _, preds):
        batch_size = x.shape[0]
        x, preds = x.view((batch_size, -1)), preds.view((batch_size, -1))
        loss = F.binary_cross_entropy_with_logits(preds, x, reduction="none")
        return loss.sum(dim=1).mean()

    model_trainer = trainer.Trainer(
        model=model,
        loss_fn=loss_fn,
        optimizer=optimizer,
        train_loader=train_loader,
        eval_loader=test_loader,
        log_dir=log_dir,
        device=device,
    )
    model_trainer.interleaved_train_and_eval(n_epochs)
Training script with defaults to reproduce results.

    The code inside this function is self contained and can be used as a top level
    training script, e.g. by copy/pasting it into a Jupyter notebook.

    Args:
        n_epochs: Number of epochs to train for.
        batch_size: Batch size to use for training and evaluation.
        log_dir: Directory where to log trainer state and TensorBoard summaries.
        device: Device to train on (either 'cuda' or 'cpu').
        debug_loader: Debug DataLoader which replaces the default training and
            evaluation loaders if not 'None'. Do not use unless you're writing unit
            tests.
    
Training script with defaults to reproduce results.
The code inside this function is self contained and can be used as a top level
training script, e.g. by copy/pasting it into a Jupyter notebook.
Training script with defaults to reproduce results . The code inside this function is self contained and can be used as a top level training script e . g . by copy / pasting it into a Jupyter notebook .
def reproduce(
    n_epochs=427, batch_size=128, log_dir="/tmp/run", device="cuda", debug_loader=None
):
    from torch import optim
    from torch import distributions
    from torch.nn import functional as F
    from torch.optim import lr_scheduler
    from torch.utils import data
    from torchvision import datasets
    from torchvision import transforms
    from pytorch_generative import trainer
    from pytorch_generative import models
    transform = transforms.Compose(
        [transforms.ToTensor(), lambda x: distributions.Bernoulli(probs=x).sample()]
    )
    train_loader = debug_loader or data.DataLoader(
        datasets.MNIST("/tmp/data", train=True, download=True, transform=transform),
        batch_size=batch_size,
        shuffle=True,
        num_workers=8,
    )
    test_loader = debug_loader or data.DataLoader(
        datasets.MNIST("/tmp/data", train=False, download=True, transform=transform),
        batch_size=batch_size,
        num_workers=8,
    )
    model = models.MADE(input_dim=784, hidden_dims=[8000], n_masks=1)
    optimizer = optim.Adam(model.parameters())
    def loss_fn(x, _, preds):
        batch_size = x.shape[0]
        x, preds = x.view((batch_size, -1)), preds.view((batch_size, -1))
        loss = F.binary_cross_entropy_with_logits(preds, x, reduction="none")
        return loss.sum(dim=1).mean()
    model_trainer = trainer.Trainer(
        model=model,
        loss_fn=loss_fn,
        optimizer=optimizer,
        train_loader=train_loader,
        eval_loader=test_loader,
        log_dir=log_dir,
        device=device,
    )
    model_trainer.interleaved_train_and_eval(n_epochs)
def reproduce ( n_epochs = 427 , batch_size = 128 , log_dir = "/tmp/run" , device = "cuda" , debug_loader = None ) : from torch import optim from torch import distributions from torch . nn import functional as F from torch . optim import lr_scheduler from torch . utils import data from torchvision import datasets from torchvision import transforms from pytorch_generative import trainer from pytorch_generative import models transform = transforms . Compose ( [ transforms . ToTensor ( ) , lambda x : distributions . Bernoulli ( probs = x ) . sample ( ) ] ) train_loader = debug_loader or data . DataLoader ( datasets . MNIST ( "/tmp/data" , train = True , download = True , transform = transform ) , batch_size = batch_size , shuffle = True , num_workers = 8 , ) test_loader = debug_loader or data . DataLoader ( datasets . MNIST ( "/tmp/data" , train = False , download = True , transform = transform ) , batch_size = batch_size , num_workers = 8 , ) model = models . MADE ( input_dim = 784 , hidden_dims = [ 8000 ] , n_masks = 1 ) optimizer = optim . Adam ( model . parameters ( ) ) def loss_fn ( x , _ , preds ) : batch_size = x . shape [ 0 ] x , preds = x . view ( ( batch_size , - 1 ) ) , preds . view ( ( batch_size , - 1 ) ) loss = F . binary_cross_entropy_with_logits ( preds , x , reduction = "none" ) return loss . sum ( dim = 1 ) . mean ( ) model_trainer = trainer . Trainer ( model = model , loss_fn = loss_fn , optimizer = optimizer , train_loader = train_loader , eval_loader = test_loader , log_dir = log_dir , device = device , ) model_trainer . interleaved_train_and_eval ( n_epochs )
Training script with defaults to reproduce results.
Training script with defaults to reproduce results .
"""Training script with defaults to reproduce results.

    The code inside this function is self contained and can be used as a top level
    training script, e.g. by copy/pasting it into a Jupyter notebook.

    Args:
        n_epochs: Number of epochs to train for.
        batch_size: Batch size to use for training and evaluation.
        log_dir: Directory where to log trainer state and TensorBoard summaries.
        device: Device to train on (either 'cuda' or 'cpu').
        debug_loader: Debug DataLoader which replaces the default training and
            evaluation loaders if not 'None'. Do not use unless you're writing unit
            tests.
    """
n_epochs
Number of epochs to train for.
Number of epochs to train for .
batch_size
Batch size to use for training and evaluation.
Batch size to use for training and evaluation .
log_dir
Directory where to log trainer state and TensorBoard summaries.
Directory where to log trainer state and TensorBoard summaries .
device
Device to train on (either 'cuda' or 'cpu').
Device to train on ( either ' cuda ' or ' cpu ' ) .
debug_loader
Debug DataLoader which replaces the default training and
evaluation loaders if not 'None'. Do not use unless you're writing unit
tests.
Debug DataLoader which replaces the default training and evaluation loaders if not ' None ' . Do not use unless you ' re writing unit tests .