-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
34 lines (26 loc) · 1.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from model import Generator, Discrimnator
from train import Trainer
batch_size = 128
transforms = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, ))])
dataset = datasets.MNIST(root='./dataset/', train=True, download=False, transform=transforms)
dataloader = DataLoader(dataset, shuffle=True, batch_size=batch_size)
generator = Generator()
discriminator = Discrimnator()
print(generator)
print(discriminator)
initial_lr = 5e-4
betas = (0.9, 0.99)
g_optimizer = optim.Adam(generator.parameters(), lr=initial_lr, betas=betas)
d_optimizer = optim.Adam(discriminator.parameters(), lr=initial_lr, betas=betas)
epochs = 200
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
trainer = Trainer(generator, g_optimizer, discriminator, d_optimizer, device=device)
trainer.train(dataloader, epochs)
name = 'mnist_model'
torch.save(trainer.G.state_dict(), './gen_' + name + '.pt')
torch.save(trainer.D.state_dict(), './dis_' + name + '.pt')