FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

CNTK 2.0のBrainScriptを使用できるようにするための環境構築

f:id:msyksphinz:20170606011549p:plain

CNTK 2.0のインストール方法は複数存在する。まず、一見簡単そうに見える、pipを使ったインストール、そして、スクリプトを使ってセットアップファイルをダウンロードしてインストールする方式だ。

pipを使ったインストール方法は簡単なのだが、Pythonインタフェースしか利用できないという制約がある。そもそもCNTK 2.0には2種類の言語インタフェースが用意されており、

が存在している。残念ながらpipを使った方式ではBrainScriptはサポートされない。 たぶん、これはBrainScriptがあまりにも普及しなかったことに起因していると思う。CNTK 1.0がリリースされた時に私も勉強したが、そりゃPythonで記述できた方が簡単に決まっている。

BrainScriptNetworkBuilder = ({
    include "myNetworks.bs"
    network = CreateMyNetworkOfType42()
}.network)

とか大量に書くのだ。そりゃ、訳が分からないし、Pythonの豊富な関数群が使える方が楽に決まっている。

ただし、そうは言ってもBrainScriptが残っているということは、ある程度速度面などで優位性があるはずだ。一応、BrainScript込みのインストール方法について調査しておく。

BrainScript込みのCNTK 2.0をインストールするための方法

まず、以下のCNTK 2.0のドキュメントを参照すると、 Linuxへのインストール方法は

  • Python-only installation
  • Script-driven installation
  • Manual installation

が存在する。ここでは、"Script-driven installation"を選択する。

f:id:msyksphinz:20170614011037p:plain

そこでCNTK binary installation with scripts on Linuxのページに飛ぶので、そこから"CNTK Release page"に飛ぶ。 githubのページに飛ぶので、そこからCNTK for Linux v.2.0 CPU onlyを選ぶ。(今回はVirtualBox上にインストールするので、GPUは不要)。

CNTK-2-0-Linux-64bit-CPU-Only.tar.gzというファイルがダウンロードされるので、VirtualBox上のUbuntu Linuxに転送し、展開する。

そこから先はスクリプトを実行するだけだ。

f:id:msyksphinz:20170614011229p:plain

tar xvfz CNTK-2-0-Linux-64bit-CPU-Only.tar.gz
./Scripts/install/linux/install-cntk.sh

これでインストールが開始される。数分でインストールは完了した。

....
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
+ set +x
+ LD_LIBRARY_PATH_SETTING=/home/msyksphinz/work/download/cntk/cntk/lib:/home/msyksphinz/work/download/cntk/cntk/dependencies/lib
+ '[' 0 = 1 ']'
+ LD_LIBRARY_PATH_SETTING+=':$LD_LIBRARY_PATH'
+ ATIVATE_SCRIPT_NAME=activate-cntk
+ cat
+ cat

************************************************************
CNTK install complete.

To activate the CNTK environment, run
  source "/home/msyksphinz/work/download/cntk/activate-cntk"

Please checkout tutorials and examples here:
  /home/msyksphinz/work/download/cntk/Tutorials
  /home/msyksphinz/work/download/cntk/Examples

To deactivate the environment run

  source /home/msyksphinz/anaconda3/bin/deactivate

************************************************************
+ '[' 0 = 1 ']'

BrainScriptで記述されたMNISTを実行する

実行できるようにするためには、上記に書いてあるように、 activate-cntkをsourceする必要がある。

source ~/work/download/cntk/activate-cntk

さっそく、MNISTの例題を実行してみよう。

cd CNTK/Examples/Image/Classification/MLP/BrainScript
cntk configFile=MLP_MNIST.cntk

実行できた。

...
Allocating matrices for forward and/or backward propagation.

Memory Sharing: Out of 28 matrices, 15 are shared as 3, and 13 are not shared.

Here are the ones that share memory:
        { z : [10 x *1]
          z.x : [256 x *1]
          z.x._._.x.PlusArgs[0] : [256 x *1]
          z.x._._.x._ : [512 x *1]
          z.x._._.x._._.x.PlusArgs[0] : [512 x *1]
          z.x._._.x._._.x._ : [768 x *1]
          z.x._._.x._._.x._._.x.PlusArgs[0] : [768 x *1] }
        { z.PlusArgs[0] : [10 x *1]
          z.x._._.x : [256 x *1] }
        { _z.x._._.x : [512 x *1]
          _z.x._._.x._._.x : [768 x *1]
          _z.x._._.x._._.x._._.x : [28 x 28 x 1 x *1]
          z.x._ : [256 x *1]
          z.x._._.x._._.x : [512 x *1]
          z.x._._.x._._.x._._.x : [768 x *1] }

Here are the ones that don't share memory:
        {errs : [1]}
        {model.arrayOfFunctions[7].arrayOfFunctions[0].W : [256 x 512]}
        {ce : [1]}
        {model.arrayOfFunctions[10].W : [10 x 256]}
        {model.arrayOfFunctions[1].arrayOfFunctions[0].b : [768]}
        {labels : [10 x *1]}
        {features : [28 x 28 x 1 x *1]}
        {z.x._._.x._._.x._._.x.ElementTimesArgs[0] : [1 x 1]}
        {model.arrayOfFunctions[4].arrayOfFunctions[0].W : [512 x 768]}
        {model.arrayOfFunctions[1].arrayOfFunctions[0].W : [768 x 28 x 28 x 1]}
        {model.arrayOfFunctions[4].arrayOfFunctions[0].b : [512]}
        {model.arrayOfFunctions[10].b : [10]}
        {model.arrayOfFunctions[7].arrayOfFunctions[0].b : [256]}

Minibatch[1-10]: errs = 1.550% * 10000; ce = 0.05761302 * 10000
Final Results: Minibatch[1-10]: errs = 1.550% * 10000; ce = 0.05761302 * 10000; perplexity = 1.05930499

Action "test" complete.

COMPLETED.