-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.py
187 lines (161 loc) · 7.29 KB
/
search.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 os
import argparse
import warnings
import numpy as np
from time import time
import tensorflow as tf
from shutil import rmtree
from tensorboard.plugins.hparams import api as hp
np.random.seed(1234)
tf.random.set_seed(1234)
from main import main as train
class HParams(object):
def __init__(self, args, session, algorithm, model, activation, noise_dim,
num_units, kernel_size, strides, phase_shuffle, gradient_penalty,
n_critic):
self.session = session
self.input_dir = args.input_dir
self.output_dir = os.path.join(
args.output_dir, '{:03d}_{}_units{}_kl{}_strides{}_ps{}_{}_nd{}'.format(
session, model, num_units, kernel_size, strides, phase_shuffle,
activation, noise_dim))
self.batch_size = args.batch_size
self.num_units = num_units
self.kernel_size = kernel_size
self.strides = strides
self.phase_shuffle = phase_shuffle
self.epochs = args.epochs
self.dropout = 0.2
self.learning_rate = 0.0001
self.noise_dim = noise_dim
self.gradient_penalty = gradient_penalty
self.model = model
self.activation = activation
self.batch_norm = False
self.layer_norm = True
self.algorithm = algorithm
self.n_critic = n_critic
self.clear_output_dir = False
self.save_generated = 'last'
self.plot_weights = False
self.skip_checkpoints = False
self.mixed_precision = args.mixed_precision
self.profile = False
self.dpi = 120
self.verbose = args.verbose
self.global_step = 0
self.surrogate_ds = True if 'surrogate' in args.input_dir else False
def print_experiment_settings(session, hparams):
print('\nExperiment {:03d}'
'\n-----------------------------------------\n'
'\talgorithm: {}\n'
'\tmodel: {}\n'
'\tnum_units: {}\n'
'\tkernel_size: {}\n'
'\tstrides: {}\n'
'\tphase shuffle: {}\n'
'\tactivation: {}\n'
'\tnoise dim: {}'.format(session, hparams.algorithm, hparams.model,
hparams.num_units, hparams.kernel_size,
hparams.strides, hparams.phase_shuffle,
hparams.activation, hparams.noise_dim))
def run_experiment(hparams, hp_hparams):
print_experiment_settings(hparams.session, hparams)
logdir = os.path.join(hparams.output_dir, 'test')
with tf.summary.create_file_writer(logdir).as_default():
hp.hparams(hp_hparams)
metrics = train(hparams, return_metrics=True)
for key, item in metrics.items():
tf.summary.scalar('test/{}'.format(key), item, step=hparams.epochs + 1)
def search(args):
if args.clear_output_dir and os.path.exists(args.output_dir):
rmtree(args.output_dir)
hp_algorithm = hp.HParam('algorithm', hp.Discrete(['wgan-gp']))
hp_model = hp.HParam('models', hp.Discrete(['wavegan']))
hp_activation = hp.HParam('activation', hp.Discrete(['leakyrelu']))
hp_noise_dim = hp.HParam('noise_dim', hp.Discrete([4, 8, 16]))
hp_num_units = hp.HParam('num_units', hp.Discrete([8, 16, 32]))
hp_kernel_size = hp.HParam('kernel_size', hp.Discrete([2, 3, 4]))
hp_strides = hp.HParam('strides', hp.Discrete([1]))
hp_phase_shuffle = hp.HParam('phase_shuffle', hp.Discrete([0, 1]))
hp_gradient_penalty = hp.HParam('gradient_penalty', hp.Discrete([10.0]))
hp_n_critic = hp.HParam('n_critic', hp.Discrete([5]))
with tf.summary.create_file_writer(args.output_dir).as_default():
hp.hparams_config(
hparams=[
hp_algorithm, hp_model, hp_activation, hp_noise_dim, hp_num_units,
hp_kernel_size, hp_strides, hp_phase_shuffle, hp_gradient_penalty,
hp_n_critic
],
metrics=[
hp.Metric('test/signals_metrics/min', display_name='min'),
hp.Metric('test/signals_metrics/max', display_name='max'),
hp.Metric('test/signals_metrics/mean', display_name='mean'),
hp.Metric('test/signals_metrics/std', display_name='std')
])
session = 0
for algorithm in hp_algorithm.domain.values:
for model in hp_model.domain.values:
for activation in hp_activation.domain.values:
for noise_dim in hp_noise_dim.domain.values:
for num_units in hp_num_units.domain.values:
for kernel_size in hp_kernel_size.domain.values:
for strides in hp_strides.domain.values:
for phase_shuffle in hp_phase_shuffle.domain.values:
for gradient_penalty in hp_gradient_penalty.domain.values:
for n_critic in hp_n_critic.domain.values:
session += 1
hparams = HParams(
args,
session,
algorithm=algorithm,
model=model,
activation=activation,
noise_dim=noise_dim,
num_units=num_units,
kernel_size=kernel_size,
strides=strides,
phase_shuffle=phase_shuffle,
gradient_penalty=gradient_penalty,
n_critic=n_critic)
if os.path.exists(hparams.output_dir):
print('Experiment {} already exists'.format(
hparams.output_dir))
continue
hp_hparams = {
hp_algorithm: algorithm,
hp_model: model,
hp_activation: activation,
hp_noise_dim: noise_dim,
hp_num_units: num_units,
hp_kernel_size: kernel_size,
hp_strides: strides,
hp_phase_shuffle: phase_shuffle,
hp_gradient_penalty: gradient_penalty,
hp_n_critic: n_critic
}
try:
start = time()
run_experiment(hparams, hp_hparams)
end = time()
print('\nExperiment {:03d} completed in {:.2f}hrs\n'.
format(session, (end - start) / (60 * 60)))
except Exception as e:
print('\nExperiment {:03d} ERROR: {}'.format(
session, e))
print('\nExperiment completed, TensorBoard log at {}'.format(args.output_dir))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input_dir', default='dataset/')
parser.add_argument('--output_dir', default='runs/hparams_turning')
parser.add_argument('--batch_size', default=64, type=int)
parser.add_argument('--epochs', default=400, type=int)
parser.add_argument('--clear_output_dir', action='store_true')
parser.add_argument('--mixed_precision', action='store_true')
parser.add_argument('--verbose', default=0, type=int)
args = parser.parse_args()
if args.verbose == 0:
warnings.simplefilter(action='ignore', category=UserWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)
warnings.simplefilter(action='ignore', category=DeprecationWarning)
search(args)