sklearn.model_selection.ParameterSampler 使用给定分布生成参数,进行参数搜索

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值