-
Notifications
You must be signed in to change notification settings - Fork 17
/
train.py
141 lines (118 loc) · 4.77 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
# -*- coding: utf-8 -*-
import os
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import CIFAR10, MNIST
from tensorboardX import SummaryWriter
from ban import config
from ban.updater import BANUpdater
from common.logger import Logger
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--weight", type=str, default=None)
parser.add_argument("--lr", type=float, default=0.01)
parser.add_argument("--n_epoch", type=int, default=50)
parser.add_argument("--batch_size", type=int, default=64)
parser.add_argument("--n_gen", type=int, default=3)
parser.add_argument("--resume_gen", type=int, default=0)
parser.add_argument("--dataset", type=str, default="cifar10")
parser.add_argument("--outdir", type=str, default="snapshots")
parser.add_argument("--print_interval", type=int, default=50)
args = parser.parse_args()
logger = Logger(args)
logger.print_args()
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = "cpu"
transform = transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.4914, 0.4822, 0.4465),
# (0.2023, 0.1994, 0.2010)),
])
if args.dataset == "cifar10":
trainset = CIFAR10(root="./data",
train=True,
download=True,
transform=transform)
testset = CIFAR10(root="./data",
train=False,
download=True,
transform=transform)
else:
trainset = MNIST(root="./data",
train=True,
download=True,
transform=transform)
testset = MNIST(root="./data",
train=False,
download=True,
transform=transform)
train_loader = DataLoader(trainset,
batch_size=args.batch_size,
shuffle=True)
test_loader = DataLoader(testset,
batch_size=args.batch_size,
shuffle=False)
model = config.get_model().to(device)
if args.weight:
model.load_state_dict(torch.load(args.weight))
optimizer = optim.Adam(model.parameters(), lr=args.lr)
kwargs = {
"model": model,
"optimizer": optimizer,
"n_gen": args.n_gen,
}
writer = SummaryWriter()
updater = BANUpdater(**kwargs)
criterion = nn.CrossEntropyLoss()
i = 0
best_loss = 1e+9
best_loss_list = []
print("train...")
for gen in range(args.resume_gen, args.n_gen):
for epoch in range(args.n_epoch):
train_loss = 0
for idx, (inputs, targets) in enumerate(train_loader):
inputs, targets = inputs.to(device), targets.to(device)
t_loss = updater.update(inputs, targets, criterion).item()
train_loss += t_loss
i += 1
if i % args.print_interval == 0:
writer.add_scalar("train_loss", t_loss, i)
val_loss = 0
with torch.no_grad():
for idx, (inputs, targets) in enumerate(test_loader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = updater.model(inputs)
loss = criterion(outputs, targets).item()
val_loss += loss
val_loss /= len(test_loader)
if val_loss < best_loss:
best_loss = val_loss
last_model_weight = os.path.join(args.outdir,
"model"+str(gen)+".pth.tar")
torch.save(updater.model.state_dict(),
last_model_weight)
writer.add_scalar("val_loss", val_loss, i)
logger.print_log(epoch, i, train_loss / args.print_interval, val_loss)
train_loss = 0
print("best loss: ", best_loss)
print("Born Again...")
updater.register_last_model(last_model_weight)
updater.gen += 1
best_loss_list.append(best_loss)
best_loss = 1e+9
model = config.get_model().to(device)
optimizer = optim.Adam(model.parameters(), lr=args.lr)
updater.model = model
updater.optimizer = optimizer
for gen in range(args.n_gen):
print("Gen: ", gen,
", best loss: ", best_loss_list[gen])
if __name__ == "__main__":
main()