keras ignoring values in $HOME/.keras/keras.json file
Asked Answered
F

1

13

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.

Fellini answered 27/3, 2017 at 19:17 Comment(2)
Is a python in your console matching a conda distribiution?Mcleroy
added an edit to address your questionFellini
U
15

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:)

Uncontrollable answered 1/4, 2017 at 9:20 Comment(4)
Thank you for letting me know. I opened an issue on the keras-feedstock conda-forge repo. Hopefully they can give some clarity on why those design choices were made.Fellini
I submitted a PR on the github repo. Hopefully will be accepted.Fellini
by playing around with upgrading/downgrading Keras in Anaconda I found out that Keras 1.2.2 created these filesUncontrollable
yeah, if you're just using Keras (not through the anaconda conda-forge installation) this should all work as expected. it's a problem with a script that ships with the conda-forge installation.Fellini

© 2022 - 2024 — McMap. All rights reserved.