Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras ignoring values in $HOME/.keras/keras.json file

I know the default backend for Keras has switched from Theano to TensorFlow, but with the dev version of Theano I can train on the GPU with OpenCL (I have an AMD card).

However, when I import Keras, it only uses the TensorFlow backend even after I changed the values in the Keras configuration file:

~ $ 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

In addition, I know that Keras is reading the configuration file after import because if I fill some non-valid value for "backend" I get an error:

~ $ 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

System details:

  • Mac OSX 10.11.6
  • Anaconda Python v 3.5
  • Keras v 2.0.2

I would like to have Keras use Theano as the default backend. Anyone know how to set it as such?

EDIT:

To answer @Marcin Możejko 's question:

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

Which is the conda virtual environment that Keras is installed in as well.

like image 749
themantalope Avatar asked Mar 27 '17 19:03

themantalope


Video Answer


1 Answers

Same issue here, system setup:

  • Ubuntu 16.04
  • Anaconda + Python 3.6
  • Keras 2.0.2

The only way to change backend is to use KERAS_BACKEND environment variable. Json field is ignored.

EDIT: The issue is Anaconda, open 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

You'll see that tensorflow is forced for MAC, and Theano for Linux.

I have no idea who creates this file, keras or anaconda, and the reasoning behind this forcing. I'm just ignoring it and doing my own way:)

like image 174
d.lime Avatar answered Oct 15 '22 07:10

d.lime