-
Notifications
You must be signed in to change notification settings - Fork 12
/
eval_ais.py
139 lines (118 loc) · 5.02 KB
/
eval_ais.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import argparse
import torch
import numpy as np
import os
import torchvision
device = torch.device('cuda:' + str(0) if torch.cuda.is_available() else 'cpu')
import vamp_utils
import mlp
from pcd_ebm_ema import get_sampler, EBM
import ais
def makedirs(dirname):
"""
Make directory only if it's not already there.
"""
if not os.path.exists(dirname):
os.makedirs(dirname)
def main(args):
makedirs(args.save_dir)
logger = open("{}/log.txt".format(args.save_dir), 'w')
def my_print(s):
print(s)
logger.write(str(s) + '\n')
torch.manual_seed(args.seed)
np.random.seed(args.seed)
my_print("Loading data")
train_loader, val_loader, test_loader, args = vamp_utils.load_dataset(args)
plot = lambda p, x: torchvision.utils.save_image(x.view(x.size(0),
args.input_size[0], args.input_size[1], args.input_size[2]),
p, normalize=True, nrow=int(x.size(0) ** .5))
def preprocess(data):
if args.dynamic_binarization:
return torch.bernoulli(data)
else:
return data
# make model
my_print("Making Model")
if args.model.startswith("mlp-"):
nint = int(args.model.split('-')[1])
net = mlp.mlp_ebm(np.prod(args.input_size), nint)
elif args.model.startswith("resnet-"):
nint = int(args.model.split('-')[1])
net = mlp.ResNetEBM(nint)
elif args.model.startswith("cnn-"):
nint = int(args.model.split('-')[1])
net = mlp.MNISTConvNet(nint)
else:
raise ValueError("invalid model definition")
# get data mean and initialize buffer
my_print("Getting init batch")
init_batch = []
for x, _ in train_loader:
init_batch.append(preprocess(x))
init_batch = torch.cat(init_batch, 0)
eps = 1e-2
init_mean = init_batch.mean(0) * (1. - 2 * eps) + eps
if args.base_dist:
model = EBM(net, init_mean)
else:
model = EBM(net)
d = torch.load(args.ckpt_path)
if args.ema:
model.load_state_dict(d['ema_model'])
else:
model.load_state_dict(d['model'])
buffer = d['buffer']
# wrap model for annealing
init_dist = torch.distributions.Bernoulli(probs=init_mean.to(device))
# get sampler
sampler = get_sampler(args)
my_print(device)
my_print(model)
my_print(sampler)
logZ, train_ll, val_ll, test_ll, ais_samples = ais.evaluate(model, init_dist, sampler,
train_loader, val_loader, test_loader,
preprocess, device,
args.eval_sampling_steps,
args.n_samples, viz_every=args.viz_every)
my_print("EMA Train log-likelihood: {}".format(train_ll.item()))
my_print("EMA Valid log-likelihood: {}".format(val_ll.item()))
my_print("EMA Test log-likelihood: {}".format(test_ll.item()))
for _i, _x in enumerate(ais_samples):
plot("{}/EMA_sample_{}.png".format(args.save_dir, _i), _x)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# data
parser.add_argument('--save_dir', type=str, default="/tmp/test_discrete")
parser.add_argument('--dataset_name', type=str, default='static_mnist')
parser.add_argument('--ckpt_path', type=str, default=None)
# data generation
parser.add_argument('--n_out', type=int, default=3) # potts
# models
parser.add_argument('--model', type=str, default='mlp-256')
parser.add_argument('--base_dist', action='store_true')
parser.add_argument('--ema', action='store_true')
parser.add_argument('--p_control', type=float, default=0.0)
# mcmc
parser.add_argument('--sampler', type=str, default='gibbs')
parser.add_argument('--seed', type=int, default=1234567)
parser.add_argument('--sampling_steps', type=int, default=100)
parser.add_argument('--steps_per_iter', type=int, default=1)
parser.add_argument('--eval_sampling_steps', type=int, default=100)
parser.add_argument('--buffer_size', type=int, default=1000)
parser.add_argument('--buffer_init', type=str, default='mean')
# training
parser.add_argument('--n_iters', type=int, default=100000)
parser.add_argument('--warmup_iters', type=int, default=-1)
parser.add_argument('--n_epochs', type=int, default=100)
parser.add_argument('--n_samples', type=int, default=100)
parser.add_argument('--batch_size', type=int, default=100)
parser.add_argument('--test_batch_size', type=int, default=100)
parser.add_argument('--print_every', type=int, default=100)
parser.add_argument('--viz_every', type=int, default=1000)
parser.add_argument('--eval_every', type=int, default=1000)
parser.add_argument('--lr', type=float, default=.001)
parser.add_argument('--weight_decay', type=float, default=.0)
args = parser.parse_args()
args.device = device
main(args)