Пример #1
0
def main():
    torch.manual_seed(6717449005)

    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    train_loader, test_loader = mnist_data()

    model = Lenet_300_100().to(device)
    optimizer = optim.Adam(model.parameters())
    criterion = my_loss(model, len(train_loader.dataset))

    train(model, device, train_loader, criterion, optimizer, n_epochs=400)

    model.set_flag('train', False)
    test(model, device, train_loader, criterion)

    model.set_flag('train', True)
    evaluate(model, device, train_loader, criterion, 10)

    criterion = my_loss(model, len(test_loader.dataset))

    model.set_flag('train', False)
    print('Mean')
    test(model, device, test_loader, criterion)

    model.set_flag('train', True)
    print('Stochastic')
    evaluate(model, device, test_loader, criterion, 10)
    save(model)
Пример #2
0
def test_pl_model():
    mnist = mnist_data()
    cnn = CNN(num_channels=mnist.dims[0],
              num_classes=mnist.num_classes)  # Architecture
    BaseLitModel(datamodule=mnist, backbone=cnn, lr=1e-3,
                 flood_height=0)  # Lightning model
    cnn = CNN(num_channels=mnist.dims[0],
              num_classes=mnist.num_classes,
              maxpool=False)
    BaseLitModel(datamodule=mnist, backbone=cnn, lr=1e-3, flood_height=0)
Пример #3
0
def main():
    torch.manual_seed(6717449005)

    criterion = nn.CrossEntropyLoss()
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    train_loader, test_loader = mnist_data()

    model = Lenet_300_100().to(device)
    optimizer = optim.Adam(model.parameters())

    train(model, device, train_loader, criterion, optimizer)
    test(model, device, test_loader, criterion)
    save(model)
Пример #4
0
from torch import nn, optim
from torch.autograd.variable import Variable
from torchvision import transforms, datasets
from utils import Logger, mnist_data, images_to_vectors, vectors_to_images, noise
from discriminator import DiscriminatorNet
from generator import GeneratorNet
from trainers import train_discriminator, train_generator

# Hack for MNIST fetch error
from six.moves import urllib
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)

# Load data
data = mnist_data()
# Create loader with data, so that we can iterate over it
data_loader = torch.utils.data.DataLoader(data, batch_size=100, shuffle=True)
# Num batches
num_batches = len(data_loader)


use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
discriminator = DiscriminatorNet().to(device)
generator = GeneratorNet().to(device)


# Optimizers
d_optimizer = optim.Adam(discriminator.parameters(), lr=0.0002)
g_optimizer = optim.Adam(generator.parameters(), lr=0.0002)
Пример #5
0
def test_mnist_data():
    mnist_data()
Пример #6
0
def test_ImagePredictionLogger():
    mnist = mnist_data()  # datamodule
    return ImagePredictionLogger(mnist.val_dataloader())
Пример #7
0
def test_knnMonitorLogger():
    mnist = mnist_data()  # datamodule
    test_transforms = ComposeMany([A.Normalize(mean=(0.0,), std=(1,)), ToTensorV2()])
    memory_dataloader = mnist.train_dataloader(transforms=test_transforms)
    test_dataloader = mnist.test_dataloader(transforms=test_transforms)
    return knnMonitorLogger(memory_dataloader, test_dataloader, knn_k=1, knn_t=1)