labs.beatcraft.com Pylearn2 setup †Pylearn2 Ubntu14.04インストールとCUDAセットアップ †Pylearn2 を GPU バックエンドで動作させるためには Python modules †Pylean2 や Theano をインストールする際に必要になる Python 関連モジュールは Theano インストール †まず
また optional になっている以下のパッケージもインストールします。
$ sudo apt-get install git python-dev python-numpy python-scipy python-pip python-nose python-sphinx python-pydot BLAS は OpenBLAS を $ 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 は $ sudo apt-get install cmake check python-mako cython ソースコードを git で取得します。 $ git clone https://github.com/Theano/libgpuarray.git $ cd libgpuarray そのまま CMake のビルドを実行すると pthread のリンク時にエラーが出るので、 $ 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 のインストールに必要なものが全部入ったので $ pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git Theano 設定調整 †上記のようにインストールした Theano は CPU バックエンドで動作しています。 [global] floatX=float32 device=gpu [mode]=FAST_RUN) [nvcc] fastmath=True [cuda] root=/usr/local/cuda [blas] ldflags = -lopenblas これで Theano 実行時に .theanorc が読み込まれて実行されます。 $ 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 インストール †Pylearn2 のインストールには Theano の他に PyYAML と PIL が必要なのでインストールします。 $ sudo apt-get install python-yaml python-pil これで Pylearn2 のインストールに必要なものが全て揃ったので $ git clone git://github.com/lisa-lab/pylearn2.git $ cd pylearn2 $ sudo python setup.py develop Pylearn2 動作時に必要になるので、インストール後 Data path の設定を .bashrc に追加しておきます。 $ mkdir -p pylearn2data $ echo 'export PYLEARN2_DATA_PATH=/home/beat/pylearn2data' >> .bashrc $ . ~/.bashrc Pylearn2 の tutorial を実行すると要求されるので、matplotlib をインストールします。 $ sudo apt-get install Pylearn2 動作確認 †Pylearn2 が正しくセットアップできたか確認するため、 $ 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 が出力されます。 $ export PYLEARN2_VIEWER_COMMAND="eog --new-instance" としてから再度 $ show_weights.py cifar_grbm_smd.pkl を実行すると、学習により作成されたガボールフィルタが Eye of Gnome で表示されます。
$ 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 |