新人指南-NNI 超参搜索

想用 NNI 做超参搜索、怎么写超参空间 json、代码怎么接收超参与上报结果、Web 怎么发起和查看实验时读这篇

平台使用 / 入门与权限
nni超参搜索hyperparameter超参空间search spacetunerTPERandom SearchAnnealSMACGrid SearchHyperbandreport_intermediate_resultreport_final_resultget_next_parameter

七、nni 超参搜索

可以参考 nni 官网 的书写方式,简体中文版链接

超参空间

必须是标准的 json。示例:

{
    "batch_size": {"_type":"choice", "_value": [16, 32, 64, 128]},
    "hidden_size":{"_type":"choice","_value":[128, 256, 512, 1024]},
    "lr":{"_type":"choice","_value":[0.0001, 0.001, 0.01, 0.1]},
    "momentum":{"_type":"uniform","_value":[0, 1]}
}

不同超参算法支持不同的超参空间:

| 算法 / Tuner | choice | choice(nested) | randint | uniform | quniform | loguniform | qloguniform | normal | qnormal | lognormal | qlognormal | | :----- | :----- | :----- | :----- | :----- | :----- | :----- | :----- | :----- | :----- | :----- | | TPE Tuner | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Random Search Tuner | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Anneal Tuner | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Evolution Tuner | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | SMAC Tuner | ✓ | | ✓ | ✓ | ✓ | ✓ | | | | | | | Batch Tuner | ✓ | | | | | | | | | | | | Grid Search Tuner | ✓ | | ✓ | | ✓ | | | | | | | | Hyperband Advisor | ✓ | | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Metis Tuner | ✓ | | ✓ | ✓ | ✓ | | | | | | | | GP Tuner | ✓ | | ✓ | ✓ | ✓ | ✓ | ✓ | | | | |

说明:上表为 NNI 各 Tuner 支持的搜索空间类型,源自 NNI 官方文档(非平台代码定义),以 NNI 对应版本文档为准。

代码要求

参数接收

启动超参搜索,会根据用户配置的超参搜索算法,选择好超参的可选值,并将选择值传递给用户的容器。例如上面的超参定义会在用户 docker 运行时传递下面的参数。所以用户不需要在启动命令或参数中添加这些变量,系统会自动添加,用户只需要在自己的业务代码中接收这些参数,并根据这些参数输出值就可以了。

--lr=0.021593113434583065 --num-layers=5 --optimizer=ftrl

结果上报

业务方容器和代码启动接收超参进行迭代计算,通过主动上报结果来进行迭代。示例如下,用户代码需要能接受超参可取值为输入参数,同时每次迭代通过 nni.report_intermediate_result 上报每次 epoch 的结果值,并使用 nni.report_final_result 上报每次实例的结果值。

import os
import argparse
import logging,random,time
import nni
from nni.utils import merge_parameter

logger = logging.getLogger('mnist_AutoML')

def main(args):
    test_acc=random.randint(30,50)
    for epoch in range(1, 11):
        test_acc_epoch= random.randint(3,5)
        time.sleep(3)
        test_acc+=test_acc_epoch
        # 上报当前迭代目标值
        nni.report_intermediate_result(test_acc)
    # 上报最终目标值
    nni.report_final_result(test_acc)


def get_params():
    # 必须接收超参数为输入参数
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch_size', type=int, default=64, help='input batch size for training (default: 64)')

    args, _ = parser.parse_known_args()
    return args


if __name__ == '__main__':
    try:
        # get parameters form tuner
        tuner_params = nni.get_next_parameter()
        params = vars(merge_parameter(get_params(), tuner_params))
        print(tuner_params,params)
        main(params)
    except Exception as exception:
        logger.exception(exception)
        raise

web 发起一个搜索实验

发起搜索实验

web 查看搜索效果

可以参考:https://nni.readthedocs.io/zh/stable/Tutorial/WebUI.html

总览界面可以看到实验的 id,和当前示例运行的状态。

总览

实验状态

可以看每次 trial 的运行情况,计算出来的目标值。

trial

也可以看某次 trial 中每次 epoch 得到的结果值。


相关专题文档

本节为快速上手版,NNI 超参搜索的完整使用方式见本段专题文档 超参搜索。超参搜索算子模板见 05-任务模板/ 段。

返回 指南目录。下一步:07-内部服务

最后更新 2026-06-30完整文档以官方仓库为准:GitHub Wiki