forked from DakshIdnani/pytorch-nice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
51 lines (38 loc) · 1.35 KB
/
train.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import torch
import torch.optim as optim
from torchvision import transforms, datasets
from config import cfg
from nice import NICE
# Data
transform = transforms.ToTensor()
dataset = datasets.MNIST(root='./data/mnist', train=True, transform=transform, download=True)
dataloader = torch.utils.data.DataLoader(dataset=dataset, batch_size=cfg['TRAIN_BATCH_SIZE'],
shuffle=True, pin_memory=True)
model = NICE(data_dim=784, num_coupling_layers=cfg['NUM_COUPLING_LAYERS'])
if cfg['USE_CUDA']:
device = torch.device('cuda')
model = model.to(device)
# Train the model
model.train()
opt = optim.Adam(model.parameters())
for i in range(cfg['TRAIN_EPOCHS']):
mean_likelihood = 0.0
num_minibatches = 0
for batch_id, (x, _) in enumerate(dataloader):
x = x.view(-1, 784) + torch.rand(784) / 256.
if cfg['USE_CUDA']:
x = x.cuda()
x = torch.clamp(x, 0, 1)
z, likelihood = model(x)
loss = -torch.mean(likelihood) # NLL
loss.backward()
opt.step()
model.zero_grad()
mean_likelihood -= loss
num_minibatches += 1
mean_likelihood /= num_minibatches
print('Epoch {} completed. Log Likelihood: {}'.format(i, mean_likelihood))
if i % 5 == 0:
save_path = os.path.join(cfg['MODEL_SAVE_PATH'], '{}.pt'.format(i))
torch.save(model.state_dict(), save_path)