自动调参神器NNI

尝试了几款调参神器后,还是选择了一款微软出的一款调参神器NNI . 除了各方面性能都挺好之外,完备的官方文档也是一个值得选择的原因。另外,weight & bias 也是一款比较优秀的调参神器。

NNI (Neural Network Intelligence)是一个轻量但强大的工具包,帮助用户自动的进行特征工程神经网络架构搜索超参调优以及模型压缩

微软还很贴心的推出了中文版,详情见官方GitHub NNINNI中文文档. 官方给出了许多的样例,比如mnist,effcientnet,等。

手上刚好在跑effcientdet, 所以以此为例。

安装

Linux 和 macOS
python3 -m pip install --upgrade nni

启动 Experiment 的三个步骤

第一步:编写 JSON 格式的搜索空间文件,包括所有需要搜索的超参的名称分布(离散和连续值均可)。

search_space.json

{
    "lr":{"_type":"choice", "_value":[0.0001, 0.0002, 0.0004, 0.001,0.005]},
    "optimizer":{"_type":"choice", "_value":["SGD", "ASGD", "Adagrad", "Adadelta", "RMSprop", "Adam","AdamW", "SparseAdam"]},
    "hue_shift_limit":{"_type":"choice", "_value":[0.1, 0.2, 0.3, 0.4]},
    "sat_shift_limit":{"_type":"choice", "_value":[0.1, 0.2, 0.3, 0.4]},
    "val_shift_limit":{"_type":"choice", "_value":[0.1, 0.2, 0.3, 0.4]},
    "brightness_limit":{"_type":"choice", "_value":[0.1, 0.2, 0.3, 0.4]},
    "contrast_limit":{"_type":"choice", "_value":[0.1, 0.2, 0.3, 0.4]},
    "num_holes":{"_type":"choice", "_value":[4, 8,10,12]}
}

第二步:定义 YAML 格式的配置文件,其中声明了搜索空间和 Trial 文件的路径。 它还提供其他信息,例如调整算法,最大 Trial 运行次数和最大持续时间的参数。

config.yml

authorName: default
experimentName: gwd
trialConcurrency: 1 # 实验需要 GPU 数量
maxExecDuration: 24h # 最大执行时间
maxTrialNum: 50 # 最大实验次数
#choice: local, remote, pai
trainingServicePlatform: local # 本地 服务器
searchSpacePath: search_space.json
#choice: true, false
useAnnotation: false
tuner: # 调参器
  #choice: TPE, Random, Anneal, Evolution, BatchTuner, MetisTuner, GPTuner
  #SMAC (SMAC should be installed through nnictl)
  builtinTunerName: TPE # TPE:基于序列模型的优化方法,根据历史指标来确定下一步参数
  classArgs:
    #choice: maximize, minimize
    optimize_mode: minimize # loss 选最小  ,精度 选 最大
trial:
  command: python3 nni_train.py
  codeDir: . #
  gpuNum: 1
localConfig:
  useActiveGpu: true

注意各个文件路径


第三步:修改 Trial 代码来从 NNI 获取超参,并返回 NNI 最终结果。

导入NNI

import nni

从Tuner获得参数值
RECEIVED_PARAMS = nni.get_next_parameter()
  • 定期返回指标数据(可选)
nni.report_intermediate_result(metrics)
  • 返回配置的最终性能,如精度、loss等
nni.report_final_result(metrics)
启动
nnictl create --config ./config.yml

nni_train.py

import nni
'''
pass
nni.report_intermediate_result(metrics)
nni.report_final_result(metrics)
'''
if __name__ == '__main__':
    try:
        tuner_params = nni.get_next_parameter()
        # tuner_params = {
        #     "lr":0.0002,
        #     "optimizer":"AdamW",
        #     "hue_shift_limit":0.2,
        #     "sat_shift_limit":0.2,
        #     "val_shift_limit":0.2,
        #     "brightness_limit":0.2,
        #     "contrast_limit":0.2,
        #     "num_holes":8
        # }
        # logger.debug(tuner_params)
        
        '''
        pass
        '''
        
        run_training(tuner_params)
    except Exception as exception:
        print(exception)
        raise

输出

在命令行中等待输出 INFO: Successfully started experiment!。 此消息表明 Experiment 已成功启动。 期望的输出如下:

INFO: Starting restful server...
INFO: Successfully started Restful server!
INFO: Setting local config...
INFO: Successfully set local config!
INFO: Starting experiment...
INFO: Successfully started experiment!
-----------------------------------------------------------------------
The experiment id is egchD4qy
The Web UI urls are: [Your IP]:8080
-----------------------------------------------------------------------

You can use these commands to get more information about the experiment
-----------------------------------------------------------------------
         commands                       description

1. nnictl experiment show        show the information of experiments
2. nnictl trial ls               list all of trial jobs
3. nnictl top                    monitor the status of running experiments
4. nnictl log stderr             show stderr log content
5. nnictl log stdout             show stdout log content
6. nnictl stop                   stop an experiment
7. nnictl trial kill             kill a trial job by id
8. nnictl --help                 get help information about nnictl

使用[IP 地址]:8080就能在浏览器上打开相应的UI界面来查看详细信息和运行状况。efficientDet按照如上配置运行的状态如下:

在这里插入图片描述

等运行结束后,经过分析就可以大致获得想要的超参数了。

结束NNI

nnictl stop

注解

nnictl 是一个命令行工具,用来控制 NNI Experiment,如启动、停止、继续 Experiment,启动、停止 NNIBoard 等等。 点击 这里 查看 nnictl 的更多用法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烤粽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值