Microsoft NNI入門

Microsoft NNI入門

【GiantPandaCV導語】Neural Network Intelligence 是一個工具包,可以有效幫助用戶設計并調(diào)優(yōu)汲取學習模型的神經(jīng)網(wǎng)絡架構(gòu),以及超參數(shù)。具有易于使用、可擴展、靈活、高效的特點。本文主要講NNI基礎的概念以及一個訓練MNIST的入門教程。本文首發(fā)于GiantPandaCV,未經(jīng)允許,不得轉(zhuǎn)載。

1. 概述

NNI有以下幾個特性:

  • 易于使用:可以通過pip進行安裝,通過命令行工具查看效果。
  • 可擴展:支持不同計算資源,多種計算平臺,可以在不同平臺并行運行。
  • 靈活:NNI內(nèi)部有超參數(shù)調(diào)優(yōu)算法、NAS算法、early stop算法等
  • 高效:NNI在系統(tǒng)和算法級別上進行不斷優(yōu)化。

基礎概念:

  • Experiment:表示一次任務,比如尋找最好的神經(jīng)網(wǎng)絡架構(gòu)。由automl算法+多個Trial構(gòu)成。
  • Search Space: 搜索空間,需要預定義的空間,比如超參數(shù)范圍,block個數(shù)限制等。
  • Configuration: 配置文件是搜索空間的實例化,比如從搜索空間中固定下來一定的超參數(shù)。
  • Trial:獨立嘗試,基于某個Configuration來進行運行的一次實驗。
  • Tuner:調(diào)優(yōu)器內(nèi)含有automl算法,可以為下一個trial生成新的Configuration。
  • Assessor: 評估器,分析trial的中間結(jié)果,來確定trial是否應該提前終止掉。
  • 訓練平臺:Trial的具體執(zhí)行環(huán)境,比如本機、遠程服務器、集群等等。

體系結(jié)構(gòu)如下圖所示:

  • nnictl: 這是命令行工具,用于控制web 服務器,和其他管理功能,用戶可以使用這個命令來進行管理。
  • NNI Core: 內(nèi)部核心,實現(xiàn)了web UI, nnimanager控制器,訓練服務等核心內(nèi)容。
  • Advisor: 包括Tuner和Assessor,分別負責生成下一個trial和評估該trial。
  • 右側(cè)代表訓練平臺,將許多trial進行分配到各個平臺中,完成一次嘗試。
體系結(jié)構(gòu)

2. 使用邏輯

一個Experiment的運行邏輯是:

  • Tuner 接收搜索空間,生成configuration
  • 將這些生成的configuration提交到很多訓練平臺上。
  • 將各個平臺上執(zhí)行的訓練結(jié)果返回給Tuner
  • 繼續(xù)生成新的configuration。

用戶的使用邏輯是:

  • 定義搜索空間,按照格式要求編寫json文件
  • 改動原有模型代碼,添加上nni的api
  • 定義實驗配置,在config.yml文件中,根據(jù)要求,設置好對應的參數(shù)要求。

3. 功能

  • 超參數(shù)調(diào)優(yōu):最核心的功能,提供了許多流行的自動調(diào)優(yōu)算法和提前種豬算法。

  • 通用NAS框架:指定候選的架構(gòu),并且可以為NAS的研究人員提供了簡單的接口,便于開發(fā)新的NAS算法。NNI支持多種one-shot NAS算法,使用這些算法不需要啟動NNI experiment,只需直接運行。但是如果需要調(diào)整超參數(shù),就需要啟動NNI experiement。

  • 模型壓縮:壓縮后的網(wǎng)絡通常具有更小的模型尺寸和更快的推理速度, 模型性能也不會有明顯的下降。 NNI 上的模型壓縮包括剪枝和量化算法

  • 自動特征工程:為下游任務找到最有效的特征。

4. 安裝

Linux下安裝:

python3 -m pip install --upgrade nni

Docker中使用NNI:

docker pull msranni/nni:latest

Window下安裝:

pip install cython wheel
python -m pip install --upgrade nni

5. 入門實驗

用MNIST進行演示如何找到MNIST模型最佳超參數(shù),官方教程以tensorflow1.x為例的,并且暫時還沒有支持tensorflow2.x,筆者本地只有tf2和pytorch環(huán)境,所以選擇pytorch進行演示。演示代碼來自官方庫:https://github.com/microsoft/nni/blob/master/examples/trials/mnist-pytorch

偽代碼:

輸出: 一組最優(yōu)的參數(shù)配置

1: For t = 0, 1, 2, ..., maxTrialNum,
2:      hyperparameter = 從搜索空間選擇一組參數(shù)
3:      final result = run_trial_and_evaluate(hyperparameter)
4:      返回最終結(jié)果給 NNI
5:      If 時間達到上限,
6:          停止實驗
7: 返回最好的實驗結(jié)果

網(wǎng)絡結(jié)構(gòu)定義:

class Net(nn.Module):
    def __init__(self, hidden_size):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4*4*50, hidden_size)
        self.fc2 = nn.Linear(hidden_size, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4*4*50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

基本上和pytorch網(wǎng)絡是一樣的,不過構(gòu)建類的時候有一個超參數(shù),hidden size是nni負責搜索的。

第一步: 搜索空間文件構(gòu)建

{
    "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]}
}

可以看出,搜索對象有batch size、hidden size、lr、momentum等參數(shù),里邊涉及到幾種類型 type。

choice代表從后邊value中選擇其中一個值,uniform代表生成一個均勻分布的超參數(shù)。

第二步:添加nni api從nni獲取超參數(shù),并返回運行結(jié)果

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

第三行,nni.get_next_parameter()就是tuner,獲取下一個configuration,將參數(shù)傳遞給main(第七行)中,開始根據(jù)configuration執(zhí)行一次trial。

在main函數(shù)中,通過args得到對應hidden_size、lr、momentum等的參數(shù)

def main(args):
    use_cuda = not args['no_cuda'] and torch.cuda.is_available()

    torch.manual_seed(args['seed'])

    device = torch.device("cuda" if use_cuda else "cpu")

    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    data_dir = args['data_dir']

    train_loader = torch.utils.data.DataLoader(
        datasets.MNIST(data_dir, train=True, download=True,
                       transform=transforms.Compose([
                           transforms.ToTensor(),
                           transforms.Normalize((0.1307,), (0.3081,))
                       ])),
        batch_size=args['batch_size'], shuffle=True, **kwargs)

    test_loader = torch.utils.data.DataLoader(
        datasets.MNIST(data_dir, train=False, transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,))
        ])),
        batch_size=1000, shuffle=True, **kwargs)

    hidden_size = args['hidden_size']

    model = Net(hidden_size=hidden_size).to(device)
    
    optimizer = optim.SGD(model.parameters(), lr=args['lr'],
                          momentum=args['momentum'])

    for epoch in range(1, args['epochs'] + 1):
        train(args, model, device, train_loader, optimizer, epoch)
        test_acc = test(args, model, device, test_loader)

        # report intermediate result
        nni.report_intermediate_result(test_acc)
        logger.debug('test accuracy %g', test_acc)
        logger.debug('Pipe send intermediate result done.')

    # report final result
    nni.report_final_result(test_acc)
    logger.debug('Final result is %g', test_acc)
    logger.debug('Send final result done.')

返回運行結(jié)果:

for epoch in range(1, args['epochs'] + 1):
    train(args, model, device, train_loader, optimizer, epoch)
    test_acc = test(args, model, device, test_loader)

    # report intermediate result
    nni.report_intermediate_result(test_acc)
    logger.debug('test accuracy %g', test_acc)
    logger.debug('Pipe send intermediate result done.')

# report final result
nni.report_final_result(test_acc)
logger.debug('Final result is %g', test_acc)
logger.debug('Send final result done.')

主要是nni.report_intermediate_result 返回中間結(jié)果 和 nni.report_final_result 返回最終結(jié)果。

第三步 定義配置文件,聲明搜索空間和Trial

authorName: pprp
experimentName: example_mnist_pytorch
trialConcurrency: 1 # 設置并發(fā)數(shù)量
maxExecDuration: 1h # 每個trial 最長執(zhí)行時間
maxTrialNum: 10 # 實驗重復運行次數(shù)
#choice: local, remote, pai
trainingServicePlatform: local
searchSpacePath: search_space.json # 搜索空間對應json文件
#choice: true, false
useAnnotation: false
tuner:
  #choice: TPE, Random, Anneal, Evolution, BatchTuner, MetisTuner, GPTuner
  #SMAC (SMAC should be installed through nnictl)
  builtinTunerName: TPE # 指定tuner算法
  classArgs:
    #choice: maximize, minimize
    optimize_mode: maximize
trial:
  command: python3 mnist.py # 命令行
  codeDir: .
  gpuNum: 1 # 使用gpu數(shù)目

一切準備就緒,在命令行啟動MNIST Experiment:

nnictl create --config config.yml
運行命令,開始在后臺執(zhí)行

訪問上圖展示的連接,可以看到NNI Web UI界面。

image

官方提供的教程基于tensorflow 1.x,詳細了解請看 https://nni.readthedocs.io/zh/stable/Tutorial/QuickStart.html

后續(xù)會陸陸續(xù)續(xù)出關于NAS使用教程,敬請期待。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容