LoginSignup
3
5

More than 5 years have passed since last update.

keras / Anacondaの罠: 環境変数 KERAS_BACKEND の悪行

Last updated at Posted at 2017-12-09

Windows7 64bit 環境の Anaconda プロンプトから keras (CPUのみ)を使っていて、気のせいか、なんとなく処理速度が遅い気がしていた。
なお、バックエンドは tensorflow を入れて設定していた つもり でいた。

と、実行時の警告を見逃してた。。。
python kerasのサンプルプログラム.py

Using Theano backend.
WARNING (theano.configdefaults): g++ not available, if using conda:
conda install m2w64-toolchain
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and G
PU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx
to an empty string.

なになに!? なんかがないので死ぬほど遅くなるって?
そんな話きいてなかったけど。。
つまり、バックエンドを Theano でやろうとして、それがないので、
しかたなく死ぬほど遅い Python 実装を使うってこと?

本家のページ:
https://keras.io/ja/backend/
によると、
$HOME/.keras/keras.json
これが、Windows だと、

type \Users\[ユーザー名]\.keras\keras.json
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}

ふむ、tensorflow を指定してて、問題なし。

え? なになに? 本家のページによると、
環境変数 KERAS_BACKENDも定義することができて,かつあなたの設定ファイルで定義されているものを上書きします

んで、どこに定義したらええねん?

https://qiita.com/TomokIshii/items/7de052565719add8e8ad
によると、
export KERAS_BACKEND=tensorflow
とのことだけど、これってLinux の話かな?

ええと、ウチの Python は Windows で Anaconda3 の環境を使ってるので、
type C:\ProgramData\Anaconda3\etc\conda\activate.d\keras_activate.bat
set "KERAS_BACKEND=theano"

こいつが犯人か! けしからん!!

こっちでも怒ってる人がいました。
http://ossyaritoori.hatenablog.com/?page=1496332747

しかし、keras_activate.bat が上書きできん!? けしからん!

というわけで、とりあえず、keras_activate.bat のとなりに、
keras_activate_local.bat
というファイルを無理矢理つくって、中身を
set "KERAS_BACKEND=tensorflow"

にしちゃえ! これで、Anaconda 起動時に無理矢理書き換え。( 他の方法もあるとは思うが・・)

これで、Anaconda3 を再起動して、

python kerasのサンプルプログラム.py
Using TensorFlow backend.
:

なるほど、・・・・10倍ぐらい速くなった気がする。。。。

ていうか、Anaconda プロンプトを使わなければいいのか。。
Anaconda はインストールには便利だけど、プロンプトは使う意味はあまりないのか。。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5