hyperopt调参

本文介绍如何使用Hyperopt库进行参数寻优,通过定义搜索空间、目标函数和优化算法,实现对参数的有效优化,同时展示了如何获取最优参数及其对应的损失值。
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval
import numpy as np
def func(param_dict):
    loss=(param_dict['x'] ** 2 - 20 * param_dict['x'])
    ret = {
        "loss": loss,
        "attachments": {
            "x": param_dict['x'],
            'y':34
        },
        "status": STATUS_OK
    }
    return ret

def run():
    param_dict = {#搜索空间
        'x': hp.quniform('x', 5, 100, 5)
    }
    trials = Trials()#Trials检查实验期间计算的所有返回值
    best = fmin(func,param_dict, tpe.suggest, 10, trials)#fmin优化函数,func目标函数
    print('best:',best)
    best_params = space_eval(param_dict, best)# at the point best using the hyperopt.space_eval function
                                              #to see param_dict's parameter values in the output
    print('best_params:',best_params)
    trial_loss = np.asarray(trials.losses(), dtype=float)
    best_ind = np.argmin(trial_loss)
    best_loss = -trial_loss[best_ind]
    best_x = trials.trial_attachments(trials.trials[best_ind])["x"]#检索次序为best_ind的参数。
    print('best_x:',best_x)

 

参考文档:

https://github.com/FontTian/hyperopt-doc-zh/wiki/FMin

https://github.com/hyperopt/hyperopt/issues/284

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值