How to change Keras backend (where's the json file)?
Asked Answered
S

12

36

I have installed Keras, and wanted to switch the backend to Theano. I checked out this post, but still have no idea where to put the created json file. Also, below is the error I got when running import keras in Python Shell:

Using TensorFlow backend.

Traceback (most recent call last): File "", line 1, in import keras File "C:\Python27\lib\site-packages\keras__init__.py", line 2, in from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py", line 1, in import tensorflow as tf ImportError: No module named tensorflow

When running python -c "import keras; print(keras.__version__)" from Windows command line, I got:

Using TensorFlow backend. Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\site-packages\keras__init__.py", line 2, in from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py", line 1, in import tensorflow as tf ImportError: No module named tensorflow

Can someone please help? Thanks!

Speller answered 28/10, 2016 at 17:11 Comment(2)
I am using mac, could you let me know how to change the keras's backend from tensorflow to theanoHardiness
To find keras.json in Windows, a shortcut is %USERPROFILE%/.keras, referenece: datascience.stackexchange.com/a/15615/83048Coccid
L
50

After looking at keras sources (this place):

Start up your python-binary and do the following

import os
print(os.path.expanduser('~'))
# >>> C:\\Users\\Sascha'  # will look different for different OS
  • This should be the base-directory
  • Keras will build an folder .keras there where keras.json resides (if it was already created). If it's not there, create it there
  • Example: C:\\Users\\Sascha\\.keras\\keras.json'
Libertarian answered 28/10, 2016 at 18:13 Comment(2)
@GeorgeLiu Please go ahead to accept the answer if it answers your question.Sometime
a shortcut is %USERPROFILE%/.keras by datascience.stackexchange.com/a/15615/83048Coccid
B
10

In case of Ubuntu,the following worked for me:

The '.keras' folder is present in your home directory,but is hidden.So,you need to unhide the hidden files in your home directory. You can see the hidden files in Ubuntu by

  • View-> show hidden files or
  • pressing ctrl+H.

You can now see the '.keras' folder in your home directory.Inside that folder,you will see the 'keras.json' file which you can modify to switch the keras backend to theano according to the official documentation https://keras.io/backend/

Bartholemy answered 23/12, 2016 at 9:23 Comment(2)
the OP is using windows so the dot files and folders are not hidden by default. Moreover, ctrl+H or view->show hidden files are ubuntu specific commandsLoony
solved my issue even though OP asked for windows. I think it is not that bad to answer such question in a way you know. So it can help people anyway.Manchester
B
7

"Can’t find your keras.json file? : Windows
On most systems the keras.json file (and associated sub-directories) will not be created until you open up a Python shell and directly import the keras package itself.

If you find that the ~/.keras/keras.json file does not exist on your system, simply open up a shell, (optionally) access your Python virtual environment (if you are using virtual environments), and then import Keras:

$ workon keras_tf
$ python
>>> import keras
>>> quit()

"

Referenced from : keras-with-tensorflow/theano-backend

Bey answered 20/1, 2017 at 8:33 Comment(0)
P
6

For those with a python shell open:

import os

with open(os.path.expanduser('~')+'\\.keras\\keras.json','w') as f:
    new_settings = """{\r\n
    "epsilon": 1e-07,\r\n
    "image_data_format": "channels_last",\n
    "backend": "theano",\r\n
    "floatx": "float32"\r\n
    }"""
    f.write(new_settings)

import keras
Participle answered 28/4, 2017 at 12:45 Comment(0)
O
3

You can directly use,

import os
os.environ['KERAS_BACKEND']='theano'

or

os.environ['KERAS_BACKEND']='tensorflow'
Oxidase answered 6/9, 2018 at 19:44 Comment(0)
O
2

In case you want to change the config, the json is available here: ~/.keras/keras.json

To do this dynamically in python 2.7 you can run:

from keras import backend as K
import os

def set_keras_backend(backend):

    if K.backend() != backend:
        os.environ['KERAS_BACKEND'] = backend
        reload(K)
        assert K.backend() == backend

set_keras_backend("theano")
Ocieock answered 8/6, 2017 at 22:59 Comment(0)
R
2

For Linux systems, the hidden .keras directory will be created in the user’s home directory. To observe whether or not it has been created, run the following command from your home directory (the -a allows you to see hidden files and directories).

    ls –a 

If the directory is there, then cd into it and modify the keras.json file. If it’s not there, then create the directory with

    mkdir .keras

Then create the file with

    touch keras.json 

Then edit the file to make the config changes you referenced to change the backend engine to Theano.

This process is covered fully in this video.

Ridden answered 28/8, 2017 at 3:34 Comment(1)
Instead of using just ls -a use ls -a | grep keras to check if .keras directory is present or not. It will just be more easier.Resource
L
1

The official Keras Documentation may help you. This link shows how to change the backend of Keras. You have to change the ~/.keras/keras.json from 'backend': 'tensorflow', to 'backend': 'theano'.

Lucullus answered 28/10, 2016 at 18:14 Comment(0)
L
1

Just to add this informative post. I'm using anaconda for my task. And imported keras through my anaconda python. So keras got installed in

C:\Users\username\AppData\Local\Continuum\Anaconda3\Lib\site-packages

There are 2 folders in site-packages: keras & Keras-1.0.8.dist-info.

In Keras-1.0.8.dist-info, there's a file called metadata.json. This by default has "Theano" as backend. So, if you change that to tensorflow, you will get tensoflow backend.

Hope it will help someone who has might type of issues.

Litalitany answered 24/7, 2017 at 10:2 Comment(0)
A
1

Simplest Solution:

Google's TensorFlow is default backend for keras but for example if u want to change it with theano then First check if the alternate backend you wish to work with is installed successfully by importing it in python shell:

import theano as th

if that works fine

Step 2: if you have installed keras, theano in virtualenv then go to virtualenv directory

e.g virtualenv/virtual/lib/python2.7/site-packages/keras/backend/

open init.py change line 27

Default backend: TensorFlow.
_BACKEND = 'theano'

and thats it

open python shell and import keras

import keras as kd

Adversary answered 20/6, 2019 at 16:19 Comment(0)
A
0

Type following and press enter on command prompt:

%USERPROFILE%/.keras/keras.json

Change backend in the opened text file and save it.

Addia answered 21/8, 2017 at 11:17 Comment(0)
A
0

In ubuntu you can use this command to open keras.json file in vi editor and editing and saveing

sudo vi $HOME/.keras/keras.json

or use the following for opening in gedit

sudo gedit $HOME/.keras/keras.json
Apples answered 5/5, 2018 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.