-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_loops.py
208 lines (183 loc) · 6.23 KB
/
train_loops.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
import numpy as np
import torch
import tqdm
import models
import utils
import wandb
from utils import eval_utils
def eval_batch(x, model, agg_metrics, train=1):
"""
Evaluate a single dataloader batch.
Agg_metrics is a dict keeping a running sum of eval statistics over the epoch.
Args:
x (tensor): batch of input data to be passed to model.
model: the model that can forward pass `x` and has function attribute
`loss` that can be called.
Returns:
loss (tensor) shape 0. Loss that can be backpropped on.
agg_metrics (tensor): same object that was passed as Arg that is updated
with the new running total after this batch.
"""
# forward pass & loss
n_samples = len(x)
x, y, mu, logvar = model(x)
loss_dict = model.loss(x, y, mu, logvar)
loss = loss_dict["loss"] * n_samples
# aggregate statistics
agg_metrics["n_samples"] += n_samples
agg_metrics["loss"] += loss.item()
agg_metrics["loss_recon"] += loss_dict["loss_recon"] * n_samples
agg_metrics["loss_kl"] += loss_dict["loss_kl"] * n_samples
agg_metrics["beta"] = loss_dict["beta"]
return loss, agg_metrics
def log_metrics(
epoch, agg_metrics, batch_idx=0, train=1, do_wandb=0, do_progress_bar=0, tq=None
):
"""
Log metrics. If training, then log metrics at batch intervals defined (defined
by the config parameter
Optionally log to wandb, or update a tqdm progress bar.
"""
# if using wandb in training, only update on certain increments, otherwise
# it may slow things down https://docs.wandb.ai/guides/technical-faq#will-wandb-slow-down-my-training
if (
train
and do_wandb
and batch_idx % wandb.config["logging"]["train_batch_freq"] != 0
):
return
# agg the metrics
n_samples = agg_metrics["n_samples"]
loss = agg_metrics["loss"] / n_samples
loss_recon = agg_metrics["loss_recon"] / n_samples
loss_kl = agg_metrics["loss_kl"] / n_samples
beta = agg_metrics["beta"]
# tqdm progress bar
if do_progress_bar:
train_test = "Train" if train else "**** Test"
msg = (
f"{train_test} epoch {epoch} | loss: {loss:.5g}; recon: {loss_recon:.5g}; "
f"kl : {loss_kl:.5g}; beta {beta}"
)
tq.set_description(msg)
# wandb logging
if do_wandb:
loss_recon_per_pixel = loss_recon / wandb.config["data"]["n_pixels"]
if train:
step = epoch * wandb.config["data"]["n_loader_train"] + batch_idx
wandb.log(
step=step,
data=dict(
epoch=epoch,
train_loss=loss,
train_loss_recon=loss_recon,
train_loss_kl=loss_kl,
loss_recon_per_pixel=loss_recon_per_pixel,
beta=beta,
),
)
else:
# Validation: called once per validation epoch.
# called on the same step as the last train run
step = wandb.run.step
wandb.log(
step=step,
data=dict(
epoch=epoch,
valid_loss=loss,
valid_loss_recon=loss_recon,
valid_loss_kl=loss_kl,
valid_recon_per_pixel=loss_recon_per_pixel,
beta=beta,
),
)
return
def eval_slow(epoch, model, loader_train, loader_test, do_wandb=1, device="cuda"):
"""
Validation methods that may take a while to run, so in a separate function to be
called less frequently.
Do eval methods that are time consuming like plot generating, model fitting
and so on.
Currently nothing is implemented, but you can use it to save reconstruction grids for example
"""
if not do_wandb:
return
model.eval()
with torch.no_grad():
step = wandb.run.step
# f_recons_train=utils.
# f_recons_test=utils.
# wandb.log(step=step, data=dict(epoch=epoch, f_recons_train=f_recons_train, f_recons_test=f_recons_test))
def train(
epoch,
model,
loader_train,
optimizer,
do_wandb=0,
do_progress_bar=1,
device="cuda",
batch_lim=None,
):
""" """
model.train()
model.to(device)
agg_metrics = dict(n_samples=0, loss=0, loss_recon=0, loss_kl=0, beta=0)
if do_wandb:
wandb.config["data"]["n_loader_train"] = len(loader_train)
wandb.config["data"]["n_pixels"] = np.product(loader_train.dataset[0][0].shape)
if do_progress_bar:
t = tqdm.tqdm(loader_train)
else:
t = loader_train
for batch_idx, (x, _) in enumerate(t):
x = x.to(device)
if batch_lim is not None and batch_idx >= batch_lim:
break
# eval batch and backprop
optimizer.zero_grad()
loss, agg_metrics = eval_batch(x, model, agg_metrics)
loss.backward()
optimizer.step()
# update per-batch metrics as needed. If do_wandb, then the frequency
# of logging is only on some batches according to wandb.config.
log_metrics(
epoch,
agg_metrics,
batch_idx=batch_idx,
train=1,
do_wandb=do_wandb,
do_progress_bar=do_progress_bar,
tq=t,
)
return
def valid(epoch, model, loader_test, do_wandb=0, do_progress_bar=1, device="cuda"):
agg_metrics = dict(
n_samples=0,
loss=0,
loss_recon=0,
loss_kl=0,
beta=0,
invar_metrics=dict(invar_maxdiff=[], invar_meandiff=[]),
)
if do_progress_bar:
t = tqdm.tqdm(loader_test)
# t.set_description(f"Valid epoch {epoch}")
else:
t = loader_test
model.eval()
model.to(device)
with torch.no_grad():
for batch_idx, (x, _) in enumerate(t):
x = x.to(device)
# metrics needing iteration through valid dataset
loss, agg_metrics = eval_batch(x, model, agg_metrics, train=0)
# log to wandb
log_metrics(
epoch,
agg_metrics,
train=0,
do_wandb=do_wandb,
do_progress_bar=do_progress_bar,
tq=t,
)
return