[[labs.beatcraft.com]]~
[[Deep Learning]]~

#contents

*Pylean2 setup [#oc7dae76]
Pylearn2~
http://deeplearning.net/software/pylearn2/~
は Python で書かれた Deep Learning の実装です。~
~
機能の多くが数値計算ライブラリ Theano 上に構築されているため~
Pylearn2 のプラグインとして新しいモデルやアルゴリズムを作成する際に数式で記述することができ、~
またそれらを GPU バックエンド上でコンパイルすることもできます。~
~
本稿では NVIDIAの GPGPU 専用ボード Tesla K20c を搭載した PC に、~
GPU バックエンドを利用できる設定で Pylearn2 をインストールする手順を記述します。


**Ubntu14.04インストールとCUDAセットアップ [#ee322f6b]
Pylearn2 を GPU バックエンドで動作させるためには~
CUDA がサポートされている OS 上で CUDA が正しくセットアップされている必要があります。~
Ubuntu 14.04 LTS 上に CUDA Toolkit 6.5 をセットアップする手順は~
[[CUDA6.5/Ubuntu14.04]] を参照してください。~

***Python modules [#a5a42ebc]
Pylean2 や Theano をインストールする際に必要になる Python 関連モジュールは~
Ubuntu の repositry にある場合には apt-get install でインストールします。~
repositry にないもののみ、 pip でインストールします。~


**Theano インストール [#b53f0e7f]
まず~
Install Theano~
http://deeplearning.net/software/theano/install.html#install~
の Requirements にある以下のライブラリなどをインストールして行きます。~
-Python2.6 以上(Ubntu14.04 は default で 2.7.6 インストール済み)
-g++
-python-dev
-Numpy 1.5.0 以上
-SciPy
-BLAS(Basic Linear Algebra Subprograms、要 Level3 の機能)

また optional になっている以下のパッケージもインストールします。
-node
-Sphinx 0.5.1 以上
-Git
-pydot
-CUDA(上記でインストール済み)
-libgpuarray

~
Ubuntu 環境へのインストールに関する注意~
[[Easy Installation of an Optimized Theano on Current Ubuntu:http://deeplearning.net/software/theano/install_ubuntu.html]]~
も参照してください。~
~
git と Python modules を apt-get でインストールします。
 $ sudo apt-get install git python-dev python-numpy python-scipy python-pip 
   python-nose python-sphinx python-pydot

BLAS は OpenBLAS を~
http://deeplearning.net/software/theano/install_ubuntu.html#manual-openblas-instruction~
に従ってインストールします。~
(Ubuntu の package 版が同時スレッド数2に制限してビルドされているので、自分でビルドしてインストールします。)~
 $ sudo apt-get install gfortran
 $ git clone git://github.com/xianyi/OpenBLAS
 $ cd OpenBLAS
 $ make FC=gfortran
 $ sudo make PREFIX=/usr/local install
 $ sudo ldconfig

libgpuarray は~
http://deeplearning.net/software/libgpuarray/installation.html~
に従い、~
まず必要なものをインストールします。
 $ sudo apt-get install cmake check python-mako cython

ソースコードを git で取得します。
 $ git clone https://github.com/Theano/libgpuarray.git
 $ cd libgpuarray

そのまま CMake のビルドを実行すると pthread のリンク時にエラーが出るので、~
CMakeLists.txt を以下のように修正します。
 $ cd src
 $ vim CMakeLists.txt

 修正前
 if(CUDA_FOUND)
 target_link_libraries(pthread ${CUDADRV_LIBRARY} ${CUDA_CUBLAS_LIBRARIES})
 target_link_libraries(gpuarray-static ${CUDADRV_LIBRARY} ${CUDA_CUBLAS_LIBRARY})
 endif()
 ↓
 修正後
 if(CUDA_FOUND)
 target_link_libraries(gpuarray pthread ${CUDADRV_LIBRARY} ${CUDA_CUBLAS_LIBRARIES})
 target_link_libraries(gpuarray-static pthread ${CUDADRV_LIBRARY} ${CUDA_CUBLAS_LIBRARY})
 endif()

修正完了後 libgpuarray 直下に戻り、以下のようにビルドしてインストールします。
 $ cd ..
 $ mkdir Build
 $ cd Build
 $ cmake .. -DCMAKE_BUILD_TYPE=Release
 $ make
 $ sudo make install
 $ sudo ldconfig
 $ cd ..

libgpuarray 同梱の pygpu は以下のように setup.py でインストールします。
 $ python setup.py build
 $ sudo python setup.py install

以上で Theano のインストールに必要なものが全部入ったので~
Theano をインストールしますが、~
Pylearn2 がなるべく最新の Theano をインストールするよう要求している~
http://deeplearning.net/software/pylearn2/#download-and-installation~
ので~
Bleeding-edge install~
http://deeplearning.net/software/theano/install.html#bleeding-edge-install-instructions~
に従って以下のように git から取得した最新の状態の Theano をインストールします。~
 $ pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

**Theano 設定調整 [#ff964aa0]
上記のようにインストールした Theano は CPU バックエンドで動作しています。~
そこで~
http://deeplearning.net/software/theano/install.html#gpu-linux
にあるように Theano が GPU を使うよう設定を行います。~
ホームディレクトリに .theanorc を作り、以下を書き込みます。`
 [global]
 floatX=float32
 device=gpu
 [mode]=FAST_RUN)
 [nvcc]
 fastmath=True
 [cuda]
 root=/usr/local/cuda
 [blas]
 ldflags = -lopenblas

これで Theano 実行時に .theanorc が読み込まれて実行されます。~
GPU が有効になっているかどうかのテストは~
http://deeplearning.net/software/theano/tutorial/using_gpu.html#using-gpu~
にあるものや、~
Theano の check_blas.py を以下のように device option を変えて実行し、~
出力内容や所要時間を比べてみるなどを行います。
 $ THEANO_FLAGS=floatX=float32,device=cpu python /usr/local/lib/python2.7/dist-packages/theano/misc/check_blas.py 
 -- 中略 -- 
 mkl_info:
   NOT AVAILABLE
 Numpy dot module: numpy.core._dotblas
 Numpy location: /usr/lib/python2.7/dist-packages/numpy/__init__.pyc
 Numpy version: 1.8.2
 
 We executed 10 calls to gemm with a and b matrices of shapes (2000, 2000) and (2000, 2000).
 
 Total execution time: 1.09s on CPU (with direct Theano binding to blas).
 
 Try to run this script a few times. Experience shows that the first time is not as fast as followings calls. The difference is not big, but consistent.
 

 $ THEANO_FLAGS=floatX=float32,device=gpu python /usr/local/lib/python2.7/dist-packages/theano/misc/check_blas.py
 Using gpu device 0: Tesla K20c
 -- 中略 --
 mkl_info:
   NOT AVAILABLE
 Numpy dot module: numpy.core._dotblas
 Numpy location: /usr/lib/python2.7/dist-packages/numpy/__init__.pyc
 Numpy version: 1.8.2
 nvcc version:
 nvcc: NVIDIA (R) Cuda compiler driver
 Copyright (c) 2005-2014 NVIDIA Corporation
 Built on Thu_Jul_17_21:41:27_CDT_2014
 Cuda compilation tools, release 6.5, V6.5.12 
 
 We executed 10 calls to gemm with a and b matrices of shapes (2000, 2000) and (2000, 2000).
 
 Total execution time: 0.08s on GPU.
 
 Try to run this script a few times. Experience shows that the first time is not as fast as followings calls. The difference is not big, but consistent.


**Pylearn2 インストール [#x859b5b8]
Pylearn2 のインストールには Theano の他に PyYAML と PIL が必要なのでインストールします。~
(PIL は CUDA インストール時に依存関係を満たすためすでに入っています。)~
 $ sudo apt-get install python-yaml python-pil

これで Pylearn2 のインストールに必要なものが全て揃ったので~
Pylearn2 のソースコードを git clone し、インストールします。
 $ git clone git://github.com/lisa-lab/pylearn2.git
 $ cd pylearn2
 $ sudo python setup.py develop

Pylearn2 動作時に必要になるので、インストール後 Data path の設定を .bashrc に追加しておきます。~
write パーミッションがある場所であればどこでもかまいませんが、~
home ディレクトリの下に Data 置き場を作成しておき、そこを指定します。
 $ mkdir -p pylearn2data
 $ echo 'export PYLEARN2_DATA_PATH=/home/beat/pylearn2data' >> .bashrc
 $ . ~/.bashrc

Pylearn2 の tutorial を実行すると要求されるので、matplotlib をインストールします。
 $ sudo apt-get install

**Pylearn2 動作確認 [#i4942acb]
Pylearn2 が正しくセットアップできたか確認するため、~
Quick-start example~
http://deeplearning.net/software/pylearn2/tutorial/index.html#tutorial~
を実行してみます。
 $ cd /home/beat/work/pylearn2/pylearn2/scripts/tutorials/grbm_smd
 $ python make_dataset.py
 Using gpu device 0: Tesla K20c
 Traceback (most recent call last):
 File "make_dataset.py", line 27, in <module>
 train = cifar10.CIFAR10(which_set="train")
 File "/home/beat/work/pylearn2/pylearn2/datasets/cifar10.py", line 76, in __init__
 raise IOError(fname + " was not found. You probably need to "
 IOError: /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/data_batch_1 was not found. You probably need to download the CIFAR-10 dataset by using the download script in pylearn2/scripts/datasets/download_cifar10.sh or manually from http://www.cs.utoronto.ca/~kriz/cifar.html

あらかじめ dataset をダウンロードしておかないといけないというワーニングが出るのでダウンロードします。
 $ cd ../../datasets
 $ ./download_cifer10.sh
 Downloading and unzipping CIFAR-10 dataset into /home/beat/pylearn2/data/cifar10...
 cifar-10-batches-py/
 cifar-10-batches-py/data_batch_4
 cifar-10-batches-py/readme.html
 cifar-10-batches-py/test_batch
 cifar-10-batches-py/data_batch_3
 cifar-10-batches-py/batches.meta
 cifar-10-batches-py/data_batch_2
 cifar-10-batches-py/data_batch_5
 cifar-10-batches-py/data_batch_1
 2015-01-16 15:39:45 URL:http://www.cs.utoronto.ca/~kriz/cifar-10-python.tar.gz [170498071/170498071] -> "-" [1]

ダウンロードできたので再度実行します。
 $ cd ../tutorials/grbm_smd/
 $ python make_dataset.py
 Using gpu device 0: Tesla K20c
 loading file /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/data_batch_1
 loading file /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/data_batch_2
 loading file /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/data_batch_3
 loading file /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/data_batch_4
 loading file /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/data_batch_5
 loading file /home/beat/pylearn2/data/cifar10/cifar-10-batches-py/test_batch
 /home/beat/work/pylearn2/pylearn2/datasets/preprocessing.py:1187: UserWarning: This ZCA preprocessor class is known to yield very different results on different platforms. If you plan to conduct experiments with this preprocessing on multiple machines, it is probably a good idea to do the preprocessing on a single machine and copy the preprocessed datasets to the others, rather than preprocessing the data independently in each location.
 warnings.warn("This ZCA preprocessor class is known to yield very "
 computing zca of a (150000, 192) matrix
 cov estimate took 0.27054309845 seconds
 eigh() took 0.0118489265442 seconds
 /home/beat/work/pylearn2/pylearn2/datasets/preprocessing.py:1280: UserWarning: Implicitly converting mat from dtype=float64 to float32 for gpu
 '%s for gpu' % (mat.dtype, floatX))
 /home/beat/work/pylearn2/pylearn2/datasets/preprocessing.py:1283: UserWarning: Implicitly converting diag from dtype=float64 to float32 for gpu
 '%s for gpu' % (diags.dtype, floatX))

pylearn2/scripts/ の下にあるスクリプトを使用するので、ここへのPATHを通します。
 $ export PATH=/home/beat/work/pylearn2/pylearn2/scripts:$PATH

train.py を実行します。
 $ train.py cifar_grbm_smd.yaml
 Using gpu device 0: Tesla K20c
 Parameter and initial learning rate summary:
 W: 0.10000000149
 bias_vis: 0.10000000149
 bias_hid: 0.10000000149
 sigma_driver: 0.10000000149
 Compiling sgd_update...
 Compiling sgd_update done. Time elapsed: 7.771741 seconds
 compiling begin_record_entry...
 compiling begin_record_entry done. Time elapsed: 0.089102 seconds
 Monitored channels:
 bias_hid_max
 bias_hid_mean
 bias_hid_min
 bias_vis_max
 bias_vis_mean
 bias_vis_min
 h_max
 h_mean
 h_min
 learning_rate
 objective
 reconstruction_error
 total_seconds_last_epoch
 training_seconds_this_epoch
 Compiling accum...
 graph size: 91
 Compiling accum done. Time elapsed: 0.814388 seconds
 Monitoring step:
 Epochs seen: 0
 Batches seen: 0
 Examples seen: 0
 bias_hid_max: -2.00000023842
 bias_hid_mean: -2.00000023842
 bias_hid_min: -2.00000023842
 bias_vis_max: 0.0
 bias_vis_mean: 0.0
 bias_vis_min: 0.0
 h_max: 8.27688127174e-05
 h_mean: 1.74318574864e-05
 h_min: 9.55541054282e-06
 learning_rate: 0.100000016391
 objective: 14.4279642105
 reconstruction_error: 70.9217071533
 total_seconds_last_epoch: 0.0
 training_seconds_this_epoch: 0.0
 /home/beat/work/pylearn2/pylearn2/training_algorithms/sgd.py:586: UserWarning: The channel that has been chosen for monitoring is: objective.
 str(self.channel_name) + '.')
 Time this epoch: 25.525986 seconds
 Monitoring step:
 Epochs seen: 1
 Batches seen: 30000
 Examples seen: 150000
 bias_hid_max: -0.257617294788
 bias_hid_mean: -1.75261676311
 bias_hid_min: -2.36502599716
 bias_vis_max: 0.160428583622
 bias_vis_mean: -0.00086586253019
 bias_vis_min: -0.220651045442
 h_max: 0.410839855671
 h_mean: 0.0542325824499
 h_min: 0.0116947097704
 learning_rate: 0.100000016391
 objective: 3.62195086479
 reconstruction_error: 29.2136707306
 total_seconds_last_epoch: 0.0
 training_seconds_this_epoch: 25.5259819031
 monitoring channel is objective
 Saving to cifar_grbm_smd.pkl...
 Saving to cifar_grbm_smd.pkl done. Time elapsed: 0.025346 seconds
 Time this epoch: 25.384062 seconds
 Monitoring step:
 Epochs seen: 2
 Batches seen: 60000
 Examples seen: 300000
 bias_hid_max: -0.305719166994
 bias_hid_mean: -2.00991845131
 bias_hid_min: -2.78829908371
 bias_vis_max: 0.185681372881
 bias_vis_mean: -0.000737291120458
 bias_vis_min: -0.177558258176
 h_max: 0.394594907761
 h_mean: 0.0468980930746
 h_min: 0.0104174567387
 learning_rate: 0.100000016391
 objective: 3.38024163246
 reconstruction_error: 28.5441741943
 total_seconds_last_epoch: 25.89610672
 training_seconds_this_epoch: 25.3840618134
 monitoring channel is objective
 Saving to cifar_grbm_smd.pkl...
 Saving to cifar_grbm_smd.pkl done. Time elapsed: 0.025256 seconds
 Time this epoch: 25.465318 seconds
 Monitoring step:
 Epochs seen: 3
 Batches seen: 90000
 Examples seen: 450000
 bias_hid_max: -0.302897870541
 bias_hid_mean: -2.12691950798
 bias_hid_min: -3.09918379784
 bias_vis_max: 0.168909445405
 bias_vis_mean: 0.000913446128834
 bias_vis_min: -0.161776274443
 h_max: 0.389986425638
 h_mean: 0.0441780276597
 h_min: 0.00789143983275
 learning_rate: 0.100000016391
 objective: 3.30141615868
 reconstruction_error: 28.4002838135
 total_seconds_last_epoch: 25.7539100647
 training_seconds_this_epoch: 25.4653167725
 monitoring channel is objective
 Saving to cifar_grbm_smd.pkl...
 Saving to cifar_grbm_smd.pkl done. Time elapsed: 0.025410 seconds
 Time this epoch: 25.288767 seconds
 Monitoring step:
 Epochs seen: 4
 Batches seen: 120000
 Examples seen: 600000
 bias_hid_max: -0.329535990953
 bias_hid_mean: -2.19633841515
 bias_hid_min: -3.181681633
 bias_vis_max: 0.171140804887
 bias_vis_mean: -0.000430780899478
 bias_vis_min: -0.197250261903
 h_max: 0.39044636488
 h_mean: 0.0431808494031
 h_min: 0.00783428177238
 learning_rate: 0.100000016391
 objective: 3.28094577789
 reconstruction_error: 28.5033798218
 total_seconds_last_epoch: 25.8351802826
 training_seconds_this_epoch: 25.2887706757
 monitoring channel is objective
 growing learning rate to 0.101000
 Saving to cifar_grbm_smd.pkl...
 Saving to cifar_grbm_smd.pkl done. Time elapsed: 0.025562 seconds
 Saving to cifar_grbm_smd.pkl...
 Saving to cifar_grbm_smd.pkl done. Time elapsed: 0.025118 seconds
cifar_grbm_smd.pkl が出力されます。~
~
show_weights.py cifar_grbm_smd.pkl で結果を確認します。~
そのまま実行すると export PYLEARN2_VIEWER_COMMAND="eog --new-instance" を設定するようワーニングが出ます。~
 $ export PYLEARN2_VIEWER_COMMAND="eog --new-instance"
としてから再度
 $ show_weights.py cifar_grbm_smd.pkl
を実行すると、学習により作成されたガボールフィルタが Eye of Gnome で表示されます。~
#ref(01.png,,100%) ~
~
以下のように --out オプションを設定して結果を画像ファイルに出力することもできます。
 $ show_weights.py cifar_grbm_smd.pkl --out=weights.png
 Using gpu device 0: Tesla K20c
 making weights report
 loading model
 loading done
 loading dataset...
 ...done
 smallest enc weight magnitude: 3.91688871559e-07
 mean enc weight magnitude: 0.0586505495012
 max enc weight magnitude: 0.99245673418
 min norm: 0.899496912956
 mean norm: 1.37919783592
 max norm: 1.96336913109


BC::labsへの質問は、bc9-dev @ googlegroups.com までお願い致します。
トップ   新規 一覧 単語検索 最終更新   最終更新のRSS