from sklearn.model_selection import ParameterSampler
class UniformDistribution(object):
def __init__(self, low, high):
assert low <= high
self.low = low
self.high = high
def rvs(self, size=None, random_state=None):
uniform = random_state.uniform if random_state else np.random.uniform
return uniform(self.low, self.high, size)
class LogUniformDistribution(object):
def __init__(self, low, high):
assert low <= high
self.low = low
self.high = high
def rvs(self, size=None, random_state=None):
uniform = random_state.uniform if random_state else np.random.uniform
return np.exp(uniform(np.log(self.low), np.log(self.high), size))
# 生成参数并训练
random_search_mode = True
random_state = None
num_runs = 10000
# 是否搜索
if random_search_mode:
#参数分布
param_distributions = dict(
learning_rate=LogUniformDistribution(low=5e-5, high=2e-4),
alpha=UniformDistribution(low=4, high=15),
l2_weight_decay=LogUniformDistribution(low=1e-6, high=5e-2),
# optimizer=['RMSProp', 'Adam'], # 'RMSPeop' or 'Adam'
# out_dim=[128, 256, 512],
)
# 静态参数
static_params = dict(
num_epochs=15,
crop_size=224,
)
# 从分布生成参数
sampler = ParameterSampler(param_distributions, num_runs, random_state)
# 遍历参数,训练
for random_params in sampler:
params = {}
params.update(random_params)
params.update(static_params)
stop = train(__file__, lossfun_one_batch, params,
save_distance_matrix)
if stop:
break