自动化调参NNI学习(一):官网代码学习

文档

github主页:https://github.com/microsoft/nni

文档:https://nni.readthedocs.io/en/stable/Overview.html

quickstart:https://nni.readthedocs.io/en/stable/Tutorial/QuickStart.html

案例地址:https://github.com/microsoft/nni/tree/master/examples

(sklearn)案例地址:https://github.com/microsoft/nni/tree/master/examples/trials/sklearn

安装

pip install --upgrade nni

调参思路

  1. json文件:确定参数的取值范围

    json格式中的search_space配置参考:
    https://nni.readthedocs.io/en/stable/Tutorial/SearchSpaceSpec.html#search-space

    当然,建议把search_space单独写成一个json文件,不过也可以写在yml文件中,可以参考:
    https://nni.readthedocs.io/en/latest/reference/experiment_config.html#local-mode-inline-search-space

  2. yml文件:nni框架的配置文件

    yml文件的配置参考:
    https://nni.readthedocs.io/en/latest/reference/experiment_config.html#experimentconfig

  3. python文件:进行单次的训练

  4. 然后使用NNI框架进行迭代训练,得出一个较好的参数

官网案例与解析

以官方案例sklearnsvm为例:https://github.com/microsoft/nni/tree/master/examples/trials/sklearn/classification

config文件:search_space.json

{
    "C": {"_type":"uniform","_value":[0.1, 1]},
    "kernel": {"_type":"choice","_value":["linear", "rbf", "poly", "sigmoid"]},
    "degree": {"_type":"choice","_value":[1, 2, 3, 4]},
    "gamma": {"_type":"uniform","_value":[0.01, 0.1]},
    "coef0": {"_type":"uniform","_value":[0.01, 0.1]}
}

yml文件:config.yml

searchSpaceFile: search_space.json
trialCommand: python main.py
trialConcurrency: 1
maxTrialNumber: 100
maxExperimentDuration: 1h
tuner:
  name: TPE
  classArgs:
    optimize_mode: maximize
trainingService: # For other platforms, check mnist-pytorch example
  platform: local

注意这里的trialCommand,在部署的时候可能需要使用绝对路径来指定python的解释环境

python代码:main.py

import nni
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
import logging

LOG = logging.getLogger('sklearn_classification')


def load_data():
    '''Load dataset, use 20newsgroups dataset'''
    digits = load_digits()
    X_train, X_test, y_train, y_test = train_test_split(
        digits.data, digits.target, random_state=99, test_size=0.25)

    ss = StandardScaler()
    X_train = ss.fit_transform(X_train)
    X_test = ss.transform(X_test)

    return X_train, X_test, y_train, y_test


def get_default_parameters():
    '''get default parameters'''
    params = {
        'C': 1.0,
        'kernel': 'linear',
        'degree': 3,
        'gamma': 0.01,
        'coef0': 0.01
    }
    return params


def get_model(PARAMS):  # 加载模型
    '''Get model according to parameters'''
    model = SVC()
    model.C = PARAMS.get('C')
    model.kernel = PARAMS.get('kernel')
    model.degree = PARAMS.get('degree')
    model.gamma = PARAMS.get('gamma')
    model.coef0 = PARAMS.get('coef0')
    return model


def run(X_train, X_test, y_train, y_test, model):
    '''Train model and predict result'''
    model.fit(X_train, y_train)
    score = model.score(X_test, y_test)
    LOG.debug('score: %s', score)
    nni.report_final_result(score)  # 灵魂代码:nni根据这个值判定模型的好坏


if __name__ == '__main__':
    X_train, X_test, y_train, y_test = load_data()
    try:
        # get parameters from tuner
        RECEIVED_PARAMS = nni.get_next_parameter()  # 这里是使用nni得到参数
        LOG.debug(RECEIVED_PARAMS)
        PARAMS = get_default_parameters()  # 使用一个默认参数
        PARAMS.update(RECEIVED_PARAMS)  # 将默认参数更新为新的参数
        LOG.debug(PARAMS)
        model = get_model(PARAMS)  # 用这个参数训练模型
        run(X_train, X_test, y_train, y_test, model)  # 得到结果
    except Exception as exception:
        LOG.exception(exception)
        raise

然后运行

nnictl create --config config.yml -p 12388 --debug

得到运行结果:
在这里插入图片描述

部分nnictl命令

indexcommandsdescription
1nnictl experiment showshow the information of experiments
2nnictl trial lslist all of trial jobs
3nnictl topmonitor the status of running experiments
4nnictl log stderrshow stderr log content
5nnictl log stdoutshow stdout log content
6nnictl stopstop an experiment
7nnictl trial killkill a trial job by id
8nnictl --helpget help information about nnictl
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呆萌的代Ma

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

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

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

打赏作者

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

抵扣说明:

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

余额充值