-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridsearch.py
187 lines (165 loc) · 5.69 KB
/
gridsearch.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
import torch
import trainers.utils as utils
import data.data_loaders as data_loaders
from trainers.trainer_mnist import MNISTTrainer
from trainers.trainer_svhn import SVHNTrainer
from trainers.trainer_multinumber import MultiNumberTrainer
from config import get_config
import wandb
import pprint
# WandB config
PROJECT = None
ENTITY = None
def main():
# Get config from gridsearch
config, unparsed = get_config()
# Init WanDB
wandb.init(project=config.wandb_project, entity=config.wandb_entity)
wandb_config = wandb.config
for k, v in wandb_config.items():
config.__dict__[k] = v
# Ensure dirs exist
utils.prepare_dirs(config)
# ensure reproducibility
torch.manual_seed(config.random_seed)
kwargs = {}
if config.use_gpu:
torch.cuda.manual_seed(config.random_seed)
kwargs = {"num_workers": config.num_workers, "pin_memory": True}
# instantiate data loaders
if config.task == "mnist":
# Load MNIST
train_dloader = None
if config.is_train:
train_dloader = data_loaders.get_train_valid_loader_mnist(
config.data_dir,
config.batch_size,
config.random_seed,
config.valid_size,
config.shuffle,
config.show_sample,
**kwargs,
)
test_dloader = data_loaders.get_test_loader_mnist(
config.data_dir, config.batch_size, **kwargs,
)
# Initialize Trainer for MNIST Dataset
trainer = MNISTTrainer(config, train_loader=train_dloader, test_loader=test_dloader, is_gridsearch=True)
elif config.task == "svhn":
# Load SVHN
train_dloader = None
if config.is_train:
train_dloader = data_loaders.get_train_valid_loader_svhn(
config.data_dir,
config.batch_size,
config.random_seed,
config.show_sample,
do_preprocessing=config.preprocess,
**kwargs,
)
test_dloader = data_loaders.get_test_loader_svhn(
config.data_dir, config.batch_size, do_preprocessing=config.preprocess,**kwargs,
)
# Initialize Trainer for SVHN Dataset
trainer = SVHNTrainer(config, train_loader=train_dloader, test_loader=test_dloader, is_gridsearch=True)
elif config.task == "multinumber":
# Load synthetic MultiNumber dataset on SVHN
train_loader, val_loader = None, None
if config.is_train:
train_loader = data_loaders.get_loader_multinumber(
config.data_dir,
"train",
config.batch_size,
config.end_class,
config.separator_class,
debug_run=config.debug_run,
use_encoder=config.use_encoder,
snapshot=config.snapshot,
**kwargs,
)
val_loader = data_loaders.get_loader_multinumber(
config.data_dir,
"val",
config.batch_size,
config.end_class,
config.separator_class,
debug_run=config.debug_run,
use_encoder=config.use_encoder,
snapshot=config.snapshot,
**kwargs,
)
test_dloader = data_loaders.get_loader_multinumber(
config.data_dir,
"test",
config.batch_size,
config.end_class,
config.separator_class,
debug_run=config.debug_run,
use_encoder=config.use_encoder,
snapshot=config.snapshot,
**kwargs,
)
# Initialize Trainer for SVHN Dataset
trainer = MultiNumberTrainer(config, train_loader=train_loader, val_loader=val_loader, test_loader=test_dloader)
# Start training
utils.save_config(config)
trainer.train()
trainer.test()
if __name__ == "__main__":
torch.autograd.set_detect_anomaly(True)
# WanDB login
wandb.login()
# Hyperparameter search config
sweep_config = {
'name': 'Transformer GTrXL SVHN',
'method': 'random',
'metric': {
'name': 'Test Reward',
'goal': 'maximize'
},
'parameters': {
'transformer_model': {
'value': "gtrxl"},
'core_type': {
'value': "transformer"},
'epochs': {
'value': 5},
'batch_size': {
'value': 128},
'cell_size': {
'value': 512},
'hidden_size': {
'value': 1024},
'preprocess': {
'value': True},
'num_glimpses': {
'value': 3},
'optimizer': {
'value': "adamw"},
'weight_decay': {
'values': [0.01, 0.001, 0]},
'std': {
'values': [0.01, 0.03, 0.05]},
'momentum': {
'value': 0.0}, # Not used
'patch_size': {
'value': 28},
'init_lr': {
'values': [
0.00002, 0.00001, 0.000005, 0.000001,
]},
'inner_size': {
'values': [
1024,
]},
'n_heads': {
'values': [
1,
]}
}
}
# Print gridsearch info
pprint.pprint(sweep_config)
# Start gridsearch
sweep_id = wandb.sweep(sweep_config, project=PROJECT, entity=ENTITY)
wandb.agent(sweep_id, function=main, count=35)