-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhp-tuning.py
196 lines (161 loc) · 6 KB
/
hp-tuning.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
"""
This script uses ray tune for hyper parameter optimization.
Most of the code is copied from the below link.
https://pytorch.org/tutorials/beginner/hyperparameter_tuning_tutorial.html
"""
import argparse
import os
from functools import partial
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from ray import tune
from ray.tune import CLIReporter
from ray.tune.schedulers import ASHAScheduler
from torch.cuda.amp import autocast
from model.model import ResMLP
from utils import read_json
# fix random seeds for reproducibility
SEED = 42
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
def train(config: dict):
net = ResMLP(
dropout=config["dropout"],
num_residuals_per_block=config["num_residuals_per_block"],
num_blocks=config["num_blocks"],
num_classes=config["num_classes"],
num_initial_features=512,
add_residual=config["add_residual"],
add_IC=config["add_IC"],
)
device = "cpu"
if torch.cuda.is_available():
device = "cuda:0"
if torch.cuda.device_count() > 1:
net = nn.DataParallel(net)
net.to(device)
if config["criterion"] == "mse":
criterion = nn.MSELoss()
elif config["criterion"] == "cse":
criterion = nn.CrossEntropyLoss()
else:
raise ValueError
optimizer = optim.AdamW(
net.parameters(), lr=config["lr"], weight_decay=config["weight_decay"]
)
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(
optimizer, gamma=config["gamma"]
)
if config["gender_or_age"].lower() == "age":
from data_loader.data_loaders import AgeDataLoader as DataLoader
elif config["gender_or_age"].lower() == "gender":
from data_loader.data_loaders import GenderDataLoader as DataLoader
else:
raise ValueError
trainloader = DataLoader(
data_dir=config["data_dir"],
batch_size=config["batch_size"],
shuffle=True,
validation_split=config["validation_split"],
num_workers=config["cpus"],
dataset=config["dataset"],
num_classes=config["num_classes"],
test_cross_val=None,
training=None,
limit_data=config["limit_data"],
)
valloader = trainloader.split_validation()
for epoch in range(config["max_num_epochs"]):
net.train()
running_loss = 0.0
epoch_steps = 0
for batch_idx, (inputs, labels) in enumerate(trainloader):
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
if config["amp"]:
with autocast():
outputs = net(inputs)
loss = criterion(outputs, labels)
else:
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
epoch_steps += 1
if batch_idx % 100 == 99: # print every 100 mini-batches
print(
"[%d, %5d] loss: %.3f"
% (epoch + 1, batch_idx + 1, running_loss / epoch_steps)
)
running_loss = 0.0
net.eval()
val_loss = 0.0
val_steps = 0
total = 0
correct = 0
for val_batch_idx, (inputs, labels) in enumerate(valloader):
with torch.no_grad():
inputs, labels = inputs.to(device), labels.to(device)
if config["amp"]:
with autocast():
outputs = net(inputs)
loss = criterion(outputs, labels)
else:
outputs = net(inputs)
loss = criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
val_loss += loss.cpu().numpy()
val_steps += 1
with tune.checkpoint_dir(epoch) as checkpoint_dir:
path = os.path.join(checkpoint_dir, "checkpoint")
torch.save((net.state_dict(), optimizer.state_dict()), path)
tune.report(loss=(val_loss / val_steps), accuracy=correct / total)
lr_scheduler.step()
print("Finished Training")
def main(config_path: str):
config = read_json(config_path)
config["dropout"] = tune.choice(config["dropout"])
config["num_residuals_per_block"] = tune.choice(config["num_residuals_per_block"])
config["num_blocks"] = tune.choice(config["num_blocks"])
config["batch_size"] = tune.choice(config["batch_size"])
config["lr"] = tune.loguniform(*config["lr"])
config["weight_decay"] = tune.loguniform(*config["weight_decay"])
config["gamma"] = tune.loguniform(*config["gamma"])
scheduler = ASHAScheduler(
metric="loss",
mode="min",
max_t=config["max_num_epochs"],
grace_period=1,
reduction_factor=2,
)
reporter = CLIReporter(metric_columns=["loss", "accuracy", "training_iteration"])
result = tune.run(
partial(train),
resources_per_trial={"cpu": config["cpus"], "gpu": config["gpus_per_trial"]},
config=config,
num_samples=config["num_samples"],
scheduler=scheduler,
progress_reporter=reporter,
)
best_trial = result.get_best_trial("loss", "min", "last")
print("Best trial config: {}".format(best_trial.config))
print("Best trial final validation loss: {}".format(best_trial.last_result["loss"]))
print(
"Best trial final validation accuracy: {}".format(
best_trial.last_result["accuracy"]
)
)
if __name__ == "__main__":
args = argparse.ArgumentParser(description="hp-tuning")
args.add_argument("-c", "--config", default="hp-tuning.json", type=str)
config_path = args.parse_args().config
main(config_path)