-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
240 lines (215 loc) · 9.14 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from __future__ import print_function
import argparse
from typing import Type
import torch
import torch.utils.data
from torch import optim
from torchvision.utils import save_image
from examples.dataloader.data_loader import data_loaders
from storch import backward
from storch.method import Expect
import storch
from torch.utils.tensorboard import SummaryWriter
from examples.vae.vae import VAE
# Reconstruction + KL divergence losses summed over all elements and batch
def loss_function(recon_x, x):
bce = storch.nn.b_binary_cross_entropy(recon_x, x, reduction="sum")
return bce
def train(epoch, model, train_loader, device, optimizer, args, writer):
model.train()
train_loss = 0
for batch_idx, (data, _) in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
storch.reset()
# Denote the minibatch dimension as being independent
data = storch.denote_independent(data.view(-1, 784), 0, "data")
recon_batch, KLD, z = model(data)
storch.add_cost(loss_function(recon_batch, data), "reconstruction")
cost = backward()
train_loss += cost.item()
optimizer.step()
cond_log = batch_idx % args.log_interval == 0
if cond_log:
step = 100.0 * batch_idx / len(train_loader)
global_step = 100 * (epoch - 1) + step
# Variance of expect method is 0 by definition.
variances = {}
if args.method != "expect" and args.variance_samples > 1:
_consider_param = "probs"
if args.latents < 3:
old_method = model.sampling_method
model.sampling_method = Expect("z")
optimizer.zero_grad()
recon_batch, _, z = model(data)
storch.add_cost(loss_function(recon_batch, data), "reconstruction")
backward()
expect_grad = storch.reduce_plates(
z.grad[_consider_param]
).detach_tensor()
optimizer.zero_grad()
model.sampling_method = old_method
grads = {n: [] for n in z.grad}
for i in range(args.variance_samples):
optimizer.zero_grad()
recon_batch, _, z = model(data)
storch.add_cost(loss_function(recon_batch, data), "reconstruction")
backward()
for param_name, grad in z.grad.items():
# Make sure to reduce the data dimension and detach, for memory reasons.
grads[param_name].append(
storch.reduce_plates(grad).detach_tensor()
)
variances = {}
for param_name, gradz in grads.items():
# Create a new independent dimension for the different gradient samples
grad_samples = storch.gather_samples(gradz, "variance")
# Compute the variance over this independent dimension
variances[param_name] = storch.variance(
grad_samples, "variance"
)._tensor
if param_name == _consider_param and args.latents < 3:
mean = storch.reduce_plates(grad_samples, "variance")
mse = storch.reduce_plates(
(grad_samples - expect_grad) ** 2
).sum()
bias = (storch.reduce_plates((mean - expect_grad) ** 2)).sum()
print("mse", mse._tensor.item())
# Should approach 0 when increasing variance_samples for unbiased estimators.
print("bias", bias._tensor.item())
writer.add_scalar("train/probs_bias", bias._tensor, global_step)
writer.add_scalar("train/probs_mse", mse._tensor, global_step)
print(
"Train Epoch: {} [{}/{} ({:.0f}%)]\tCost: {:.6f}\t Logits var {}".format(
epoch,
batch_idx * len(data),
len(train_loader.dataset),
step,
cost.item(),
variances,
)
)
writer.add_scalar("train/ELBO", cost, global_step)
for param_name, var in variances.items():
writer.add_scalar("train/variance/" + param_name, var, global_step)
avg_train_loss = train_loss / (batch_idx + 1)
print("====> Epoch: {} Average loss: {:.4f}".format(epoch, avg_train_loss))
return avg_train_loss
def test(epoch: int, model: torch.nn.Module, test_loader, device):
model.eval()
test_loss = 0
with torch.no_grad():
for i, (data, _) in enumerate(test_loader):
data = data.to(device)
data = storch.denote_independent(data.view(-1, 784), 0, "data")
recon_batch, KLD, _ = model(data)
test_loss += (
loss_function(recon_batch, data).detach_tensor()
).mean() + KLD.detach_tensor().mean()
if i == 0:
n = min(data.size(0), 8)
# comparison = storch.cat([data[:n],
# recon_batch.detach_tensor()[0].view(args.batch_size, 1, 28, 28)[:n]]) # Take the first sample (0)
# deterministic(save_image)(comparison.cpu(),
# 'results/reconstruction_' + str(epoch) + '.png', nrow=n)
storch.reset()
test_loss /= i + 1
print("====> Test set loss: {:.4f}".format(test_loss))
return test_loss
def main(vae: Type[VAE]):
parser = argparse.ArgumentParser(description="VAE MNIST Example")
parser.add_argument(
"--batch-size",
type=int,
default=128,
metavar="N",
help="input batch size for training (default: 128)",
)
parser.add_argument(
"--epochs",
type=int,
default=10,
metavar="N",
help="number of epochs to train (default: 10)",
)
parser.add_argument(
"--no-cuda", action="store_true", default=False, help="enables CUDA training"
)
parser.add_argument(
"--seed", type=int, default=1, metavar="S", help="random seed (default: 1)"
)
parser.add_argument(
"--log-interval",
type=int,
default=10,
metavar="N",
help="how many batches to wait before logging training status",
)
parser.add_argument(
"--method",
type=str,
default="gumbel",
help="Method in {gumbel, gumbel_straight, score}",
)
parser.add_argument(
"--baseline",
type=str,
default="none",
help="What baseline to use for the score function.",
)
parser.add_argument(
"--latents",
type=int,
default=20,
help="How many latent variables with 10 categories to use",
)
parser.add_argument(
"--samples", type=int, default=1, help="How large of a budget to use"
)
parser.add_argument(
"--variance_samples",
type=int,
default=10,
help="How many samples to use to compute the variance of the estimators.",
)
parser.add_argument("--data_dir", type=str, default="./data/")
parser.add_argument("--dataset", type=str, default="fixedMNIST")
parser.add_argument("--out_dir", type=str, default="./outputs/")
parser.add_argument("--lr", type=float, default=1e-3)
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if args.cuda else "cpu")
train_loader, test_loader = data_loaders(args)
model = vae(args).to(device)
writer = SummaryWriter(args.out_dir + model.name() + "_" + args.method)
print(args)
writer.add_text("hyperparameters", str(args), global_step=0)
optimizer = optim.Adam(model.parameters(), lr=args.lr)
best_train_loss = 1000.0
best_test_loss = 1000.0
for epoch in range(1, args.epochs + 1):
train_loss = train(epoch, model, train_loader, device, optimizer, args, writer)
if train_loss < best_train_loss:
best_train_loss = train_loss
test_loss = test(epoch, model, test_loader, device)
writer.add_scalar("test_loss", test_loss, 100 * epoch)
writer.flush()
if test_loss < best_test_loss:
best_test_loss = test_loss
# TODO: Sampling currently doesn't work because of model.prior requiring the posterior.
# with torch.no_grad():
# im_sample = model.prior([args.latents]).sample((64,))
# im_sample = model.decode(im_sample).cpu()
# save_image(
# im_sample.view(64, 1, 28, 28), args.out_dir + "results/sample_" + str(epoch) + ".png",
# )
measures = {
"hparams/best_train_loss": best_train_loss,
"hparams/best_test_loss": best_test_loss,
# "hparams/train_loss": train_loss, "hparams/test_loss": test_loss,
"train/loss": train_loss,
"test_loss": test_loss,
}
writer.add_hparams(vars(args), measures)
writer.close()