みらいテックラボ

音声・画像認識や機械学習など, 週末プログラマである管理人が興味のある技術の紹介や実際にトライしてみた様子などメモしていく.

AutoML:NNIを試してみた! (2)

(株)クラスキャットが先日(2019.2.25)から投稿されている「AutoML」に関する記事[1][2][3]を読んで, マイクロソフト社がオープンソース化したAuto MLツールキットNNI(Neural Network Intelligence)[4][5]に興味を持ち, 少し試してみた.


関連記事:
AutoML:NNIを試してみた! (1)
・AutoML:NNIを試してみた! (2)


今回は, NNIでリモートモードの使い方を試してみた.

1. インストール [4]
1.1 nni
クライアント, サーバともにnniをpipインストールする.

$ pip install --upgrade nni

1.2 nni-sdk
サーバにnni-sdkをインストールする.
(2019.3.2時点のバージョンは, v0.5.1)

$ git clone -b v0.5.1 https://github.com/Microsoft/nni.git
$ cd nni/src/sdk/pynni
$ pip install -e .


2. configファイル設定
前回のmnist-kerasをリモート学習するように, configファイルを設定する.

authorName: default
experimentName: example_mnist-keras-remote
trialConcurrency: 1
maxExecDuration: 1h
maxTrialNum: 10
#choice: local, remote, pai
trainingServicePlatform: remote  # <-- remoteに設定
searchSpacePath: search_space.json
#choice: true, false
useAnnotation: false
tuner:
  #choice: TPE, Random, Anneal, Evolution, BatchTuner
  #SMAC (SMAC should be installed through nnictl)
  builtinTunerName: TPE
  classArgs:
    #choice: maximize, minimize
    optimize_mode: maximize
trial:
  command: python3 mnist-keras.py  # <-- 注意!! (後で説明)
  codeDir: .
  gpuNum: 0
#machineList can be empty if the platform is local
machineList:
  - ip: 192.168.11.16    # <-- サーバの情報記述
    username: xxxx
    passwd: yyyy
    #port can be skip if using default ssh port 22
    #port: 22
  # ip: 192.168.11.17   # <-- 複数使用する場合は同様に記述
  # username: xxxx
  # passwd: yyyy
    #port can be skip if using default ssh port 22
    #port: 22


3. 実行
nnictlを使って実行する.

$ nnictl create --config config_remote.yml
INFO: expand searchSpacePath: search_space.json to /home/aska/src/nni/examples/trials/mnist-keras/search_space.json 
INFO: expand codeDir: . to /home/aska/src/nni/examples/trials/mnist-keras/. 
INFO: Starting restful server...
INFO: Successfully started Restful server!
INFO: Setting remote config...
INFO: Successfully set remote config!
INFO: Starting experiment...
INFO: Successfully started experiment!
-----------------------------------------------------------------------
The experiment id is JvHVr4Pm
The Web UI urls are: http://127.0.0.1:8080   http://192.168.159.128: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
-----------------------------------------------------------------------


これで動いたと思ったが, 進捗を確認すると失敗してる.
f:id:moonlight-aska:20190302141211p:plain

私の場合はここでハマった!!

[ハマりどころ]
サーバで開発環境をcondaなどの仮想環境上に構築している場合, "python3 mnist.py"をコマンドとして渡すと, 実行に失敗する.

[サーバエラーログ]

Traceback (most recent call last):
  File "mnist-keras.py", line 22, in <module>
    import keras
ModuleNotFoundError: No module named 'keras'

失敗原因を調べるのに, エラーログ等がどこにあるかなど, 結構手間取った.

原因:[5]
nnictlはsshを使ってクライアントからサーバにコマンドを送っているが, sshの仕様でnon-intractive modeのときは profile(~/.bash_profileなど)が読み込まれないため.

対策:
他にもやり方あると思うが, 以下のようなシェルスクリプトをコマンドとしてサーバに渡すことで, 今回は対応.

#! /bin/bash
export ENV_NAME=tensorflow
export CONDA_PATH=$HOME/anaconda3
. $CONDA_PATH/bin/activate $ENV_NAME
python3 mnist-keras.py

エラーが発生した場合, ログ等が以下に出力されるので, これらを見ながら対策することになる.
・クライアント
 - /home/(user name)/nni/experiments/XXXX/log <-- nnictlのログ
・サーバ
 /tmp/nni/experiments/XXXX/log <-- nnictlのログ
 /tmp/nni/experiments/XXXX/trails/YYYY/stderr <-- command実行のエラーログ

少しハマったが, リモート学習できることを一応確認した.

----
[1] Auto ML : Neural Network Intelligence : 概要 - TensorFlow
[2] Auto ML : Neural Network Intelligence : QuickStart - TensorFlow
[3] Auto ML : NNI Tutorials : NNI で Trial Run を書く - TensorFlow
[4] Microsoft/nn: An open source AutoML toolkit for neural architecture search and hyper-parameter tuning.
[5] [FREEBSD][LINUX] SSH経由でコマンド実行すると環境変数を読まないでござる | Nobwak's Lair




[試して理解]Linuxのしくみ ~実験と図解で学ぶOSとハードウェアの基礎知識

[試して理解]Linuxのしくみ ~実験と図解で学ぶOSとハードウェアの基礎知識


ハッキング・ラボのつくりかた 仮想環境におけるハッカー体験学習

ハッキング・ラボのつくりかた 仮想環境におけるハッカー体験学習



新しいLinuxの教科書

新しいLinuxの教科書