-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_gaussian.py
61 lines (54 loc) · 1.64 KB
/
train_gaussian.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
52
53
54
55
56
57
58
59
60
61
import torch
import numpy as np
import os
from models import MADE
from data import MNIST
from train import (
train_epoch_gaussian,
validate_epoch_gaussian,
)
from test import test_model_gaussian
from utils.plot import sample_digits_gaussian
# --------- parameters ----------
load_model = False
n_in = 784
hidden_dims = [1024]
lr = 1e-3 # try 1e-4 !!!
epochs = 200
seed = 876
random_order = False
save_model = True
# -------------------------------
# Get datasets and train loaders.
mnist = MNIST()
train, val, _ = mnist.get_data_splits()
train_loader = torch.utils.data.DataLoader(train, batch_size=128, shuffle=True)
val_loader = torch.utils.data.DataLoader(val, batch_size=128, shuffle=True)
model = MADE(n_in, hidden_dims, random_order=False, seed=seed, gaussian=True)
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=1e-6)
# Early stopping
i = 0
max_loss = np.inf
for epoch in range(1, epochs + 1):
train_epoch_gaussian(model, train_loader, epoch, optimizer)
# sample_digits_gaussian(model, epoch, random_order=random_order, seed=seed)
val_loss = validate_epoch_gaussian(model, val_loader, epoch)
if val_loss < max_loss:
max_loss = val_loss
i = 0
else:
i += 1
if i > 20:
break
print("Early stopping counter:", i)
if save_model:
string = "_".join([str(h) for h in hidden_dims])
torch.save(
{
"epoch": epochs,
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"scheduler_state_dict": scheduler.state_dict(),
},
"./model_saves/model_gaussian_" + string + ".pt",
)