Theanoのインストール

fast.ai の Practical Deep Learning For Coders, Part 1 は Keras v1(バックエンドはTheano)を使用します。

なので、早速 Ubuntu14.04に Theanoをインストールしていきます。

インストール

公式ページ に書いてある通り、以下の1行で楽々インストール

$ conda install theano pygpu

.theanorcに手こずった..

しかし、やはりというか設定関係で少し苦労..。

ubuntu14.04 theanoとかgpgpu環境構築メモ - 備忘録とか日常とか を参考にして

cnmen = True としたら ValueError: could not convert string to float: True というエラーが出た。。

どうやら cnmem = True とするのはTheano 0.7までのときの設定のようで Theano 0.8からは cnmem = 1 のように float型の値を入れるらしい。

https://github.com/Theano/Theano/issues/4170

ところが、cnmem = 1 としたら先のエラーは解消したもののconvolutionを使った学習実行時に

RuntimeError: ('The following error happened while compiling the node', GpuDnnSoftmax{tensor_format='bc01', mode='channel', algo='log'}(GpuContiguous.0), '\n', 'could not create cuDNN handle: CUDNN_STATUS_INTERNAL_ERROR')

という新たなエラーが...! (。ŏ﹏ŏ)

いろいろ試してみると cnmem = 0〜0.9 までだったら問題ないことが分かりました。

公式ページの http://deeplearning.net/software/theano_versions/0.9.X/library/config.html#config.config.lib.cnmem を読む限りは

cnmem = 1cnmem = 0.95 でも設定としては大丈夫そうなのだが..

CNMeM

cnmem = 0 (CNMeM is disabled)がよいのか? cnmem = 0.9 (CNMeM is enabled with initial size: 90.0% of memory)がよいのか?

公式ページによると

Controls the use of CNMeM (a faster CUDA memory allocator). Applies to the old GPU backend CUDA backend up to Theano release 0.8.

This could cause memory fragmentation. So if you have a memory error while using CNMeM, try to allocate more memory at the start or disable it. If you try this, report your result on :reftheano-dev.

とあり、

  • CNMeMを使うとCUDAのメモリ割り当てが早くなる
  • ただし、メモリの断片化を引き起こす可能性がある

とのことです。

自分の環境(GeForce GTX 950)でLesson1の「The punchline: state of the art custom model in 7 lines of code」 のコードを使って比較してみたところ

cnmem = 0 (CNMeM disabled) cnmem = 0.9 (CNMeM enabled)
約806s / epoch 約785s / epoch

と 実際に CNMeMを有効にしたほうが速くなりました。:-)

Making Theano Faster with CuDNN and CNMeM on Windows 10 – Ankivil の記事でも CNMeM + CuDNN が速いという報告になっています。(赤い実線に注目)

https://ankivil.com/wp-content/uploads/2016/08/cuddn_cnmem_combined_graph_vgg16.png (図は https://ankivil.com/wp-content/uploads/2016/08/cuddn_cnmem_combined_graph_vgg16.png から引用)