栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 文档
  • 安装
  • 调参思路
  • 官网案例与解析
  • 部分nnictl命令

文档

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框架进行迭代训练,得出一个较好的参数

官网案例与解析

以官方案例sklearn的svm为例: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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号