1
votes

I installed keras. When I import keras, it says tensorflow not installed. I tried changing the backend in keras.json file to theano. But this doesn't help as it still says no module named tensorflow. Please help me fix this.

3

3 Answers

4
votes

You can use an env variable as follow:

import os
os.environ["KERAS_BACKEND"] = "theano"
2
votes

Did you try this?

you will find the Keras configuration file at:

$HOME/.keras/keras.json

The default configuration file looks like this:

{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}

Simply change the field backend to "theano", "tensorflow", or "cntk", and Keras will use the new configuration next time you run any Keras code.

You can also define the environment variable KERAS_BACKEND and this will override what is defined in your config file :

KERAS_BACKEND=tensorflow python -c "from keras import backend"

0
votes

There is two ways to switch keras backend from tensorflow to theano, or theano to tensorflow.

1- Go to /home/user/.keras folder and open keras.json file. it looks like

{

"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}
from here you can switch from tensorflow to theano as backend, by default here 
tensorflow is keras backend. but if you your keras is showing theano as backend and you want tensorflow as backend and in this file already  tensorflow is backend then you need to follow.

second option.

  1. when you are using virtual conda environment then go to

    "/home/user/anaconda3/envs/opensim-rl/etc/conda/activate.d"

    In this folder you get a file "keras_activate.sh" file and change it here.

    from export KERAS_BACKEND="theano" to export KERAS_BACKEND="tensorflow".