2017-03-27 81 views
5

我知道Keras的默認後端已經從Theano切換到TensorFlow,但是使用Theano的dev版本,我可以在OpenCL上訓練GPU(我有AMD卡)。

然而,當我輸入Keras,它僅使用TensorFlow後端即使我的Keras配置文件在修改的數值:

~ $ cat $HOME/.keras/keras.json 
{"epsilon": 1e-07, "floatx": "float32", "backend": "theano"} 

~ $ python -c 'import keras' 
Using TensorFlow backend. 

~ $ KERAS_BACKEND=theano python -c 'import keras' 
Using Theano backend. 
Mapped name None to device opencl0:2: AMD Radeon R9 M370X Compute Engine 

另外,我知道Keras被讀取配置文件導入後,因爲如果我填一些非有效的價值"backend"我得到一個錯誤:

~ $ cat $HOME/.keras/keras.json 
{"epsilon": 1e-07, "floatx": "float32", "backend": "foobar"} 


~ $ python -c 'import keras' 
Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "/Users/antalek/anaconda/envs/ENVPy3/lib/python3.5/site-packages/keras/__init__.py", line 3, in <module> 
    from . import activations 
    File "/Users/antalek/anaconda/envs/ENVPy3/lib/python3.5/site-packages/keras/activations.py", line 3, in <module> 
    from . import backend as K 
    File "/Users/antalek/anaconda/envs/ENVPy3/lib/python3.5/site-packages/keras/backend/__init__.py", line 34, in <module> 
    assert _backend in {'theano', 'tensorflow'} 
AssertionError 

系統細節:

  • 的Mac OSX 10.11.6
  • 蟒蛇Python的v 3.5
  • Keras v 2.0.2

我想有Keras使用Theano作爲默認的後臺。任何人都知道如何設置它?

編輯:

要回答@MarcinMożejko的問題:

~ $ which python 
/Users/<my name>/anaconda/envs/ENVPy3/bin/python 

它是Keras安裝在爲好暢達虛擬環境。

+0

是在您的控制檯匹配'conda'分佈的'python'嗎? –

+0

增加了一個編輯來解決你的問題 – themantalope

回答

6

同樣的問題在這裏,系統設置:

  • 的Ubuntu 16.04
  • 蟒蛇+的Python 3.6
  • Keras 2.0.2

改變後端的唯一方法就是使用KERAS_BACKEND環境變量。 Json字段被忽略。

編輯: 問題是蟒蛇,開放anaconda3/envs/ENV-NAME/etc/conda/activate.d/keras_activate.sh

#!/bin/bash 
if [ "$(uname)" == "Darwin" ] 
then 
    # for Mac OSX 
    export KERAS_BACKEND=tensorflow 
elif [ "$(uname)" == "Linux" ] 
then 
    # for Linux 
    export KERAS_BACKEND=theano 
fi 

你會看到,tensorflow被迫for Mac和Theano的Linux版本。

我不知道是誰創建這個文件,keras或anaconda,以及這個強制背後的推理。我只是無視它,做我自己的方式:)

+0

謝謝你讓我知道。我在keras-feedstock上打開了一個問題(https://github.com/conda-forge/keras-feedstock/issues/13)conda-forge回購。希望他們能夠澄清爲什麼選擇這些設計。 – themantalope

+1

我在[github回購](https://github.com/conda-forge/keras-feedstock/pull/14)上提交了一個PR。希望會被接受。 – themantalope

+0

通過玩升級/降級在Anaconda Keras我發現Keras 1.2.2創建了這些文件 – user1118094