-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.py
66 lines (52 loc) · 2.05 KB
/
main.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
import torch
import torch.nn as nn
from utils.parser import args
from utils import logger, Trainer, Tester
from utils import init_device, init_model, FakeLR, WarmUpCosineAnnealingLR
from dataset import Cost2100DataLoader
def main():
logger.info('=> PyTorch Version: {}'.format(torch.__version__))
# Environment initialization
device, pin_memory = init_device(args.seed, args.cpu, args.gpu, args.cpu_affinity)
# Create the data loader
train_loader, val_loader, test_loader = Cost2100DataLoader(
root=args.data_dir,
batch_size=args.batch_size,
num_workers=args.workers,
pin_memory=pin_memory,
scenario=args.scenario)()
# Define model
model = init_model(args)
model.to(device)
# Define loss function
criterion = nn.MSELoss().to(device)
# Inference mode
if args.evaluate:
Tester(model, device, criterion)(test_loader)
return
# Define optimizer and scheduler
lr_init = 1e-3 if args.scheduler == 'const' else 2e-3
optimizer = torch.optim.Adam(model.parameters(), lr_init)
if args.scheduler == 'const':
scheduler = FakeLR(optimizer=optimizer)
else:
scheduler = WarmUpCosineAnnealingLR(optimizer=optimizer,
T_max=args.epochs * len(train_loader),
T_warmup=30 * len(train_loader),
eta_min=5e-5)
# Define the training pipeline
trainer = Trainer(model=model,
device=device,
optimizer=optimizer,
criterion=criterion,
scheduler=scheduler,
resume=args.resume)
# Start training
trainer.loop(args.epochs, train_loader, val_loader, test_loader)
# Final testing
loss, rho, nmse = Tester(model, device, criterion)(test_loader)
print(f"\n=! Final test loss: {loss:.3e}"
f"\n test rho: {rho:.3e}"
f"\n test NMSE: {nmse:.3e}\n")
if __name__ == "__main__":
main()