Theano used as default backend with Keras - tensorflow

I have just installed Tensorflow and Keras in a conda environment (with miniconda).
In the keras.json file, the backend is tensorflow and there is no environment variable KERAS_BACKEND defined, however, when I import keras backend, I am told that Theano is used !
I don't know what's wrong....

Two solutions in fact:
1- When installing Keras with conda install keras, the solution I found is to overwrite the __init.py__ file of the keras.backend module. In the file, I simply commented the portion of code where it checks for an environment variable. However, I have not found where the environment variable was set.
2- The proposed solution in other posts like this one is the solution to find where the environment variable is set. For this, keras has to be installed with the channel of conda-forge conda install -c conda-forge keras.

Related

Why keras always says using theano backend?

I installed keras using conda in my virtual environment and checked $HOME/.keras/config.json file.
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
I already set backend to tensorflow but when I run this in the python console
import keras
It is showing me that keras is using theano backend. Why?
Using Theano backend.
WARNING (theano.configdefaults): install mkl with `conda install mkl-service`: No module named mkl
I added export KERAS_BACKEND=tensorflow at the end of my .bashrc and restart the command line and activate my source. Still seeing the above error again. Can anyone help me with this?
We had also faced the same issue when installed keras using conda environment. Since we already had keras installed using pip, where the backend was set as theano, it was taking that keras. The problem got fixed when we removed the pip version of keras using the command pip uninstall keras
Well you can start your editor with line:
KERAS_BACKEND=tensorflow
KERAS_BACKEND=tensorflow spyder
This would force use the Tensorflow backend.
But before using this ensure that you have tensorflow installed with all the required dependencies.
Source

tensorflow is only available in virtual env in anaconda

I installed tensorflow-gpu in anaconda3.
The steps I followed are:
conda create -n tensorflow pip python=3.6
source activate tensorflow
pip install tensorflow_gpu-1.8.0-cp36-cp36m-linux_x86_64.whl
After these steps, I find that if I use the python and ipython in anaconda3/bin, I can't import tensorflow (no module named tensorflow)
But If I activate the environment of tensorflow or I use the python and ipython in anaconda3/env/tensorflow/bin, I can import tensorflow.
And there is also no directory for tensorflow inanaconda3/lib/python3.6/site-package
Anyone knows why?
You created a conda environment and installed Tensorflow to that environment. Therefore you can only use Tensorflow inside that environment.
If you wish to use Tensorflow outside of an environment then don't activate the environment you created and just pip install Tensorflow.

Dataset of MNIST not found from Tensorflow

I have a python script whose initial lines read
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
When I run the script it gives this error
'no module such as tensorflow.tutorials.examples.mnist'.
I have already pip installed Tensorflow, what do i do now?
From your comment I can see that you are using Anaconda. Have you tried to install it with conda install tensorflow? Also, you could try to create an environment first, and install tensorflow afterwards with conda create -n tensorflow pip python=2.7 (or 3.x depending on your system) within it.

Configuring Keras to use Tensorflow instead of Theano

I'm trying to configure Keras install under an Anaconda virtualenv with all of that running under Ubuntu 17.04. I've installed keras-gpu via conda, and have generated a bootstrap ~/.keras directory by running python -c 'import keras'; finally, I've updated my keras.json within that directory to include tensorflow as the backend rather than theano.
I've also tried those steps with the regular, non-GPU keras available on conda.
The issue I'm getting is that the backend option in my keras.json is being read (since invalid values raise exceptions), but is being overruled by an environment variable that gets exported by Anaconda itself -- according to grep, there are a few instances of:
export KERAS_BACKEND=tensorflow
export KERAS_BACKEND=theano
... Scattered around a number of files in ~/miniconda3/pkgs/keras-2.0.2-py36_1/.
I'm hesitant to manually edit these files since they're automatically put there by the package manager, but I also want to avoid explicitly specifying KERAS_BACKEND=tensorflow at the start of each session, and I'd like to avoid solutions involving tools like direnv.
How can I get conda's keras to use tensorflow by default?
The problem is probably in the file activate.sh of the keras package on conda-forge. The export statements in this file are unnecessary and should be removed IMO. There's just no reason to restrict linux users to use theano as Keras backend (or TensorFlow for Mac OSX).
#!/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 could solve the problem by:
Removing those environment settings from activate.sh.
Removing currently installed keras and keras-gpu, and then
install keras with conda install -c defaults keras: the
non-conda-forge version of keras seems to be okay. I didn't find any of those env settings on my machine.
pip install keras: removing currently installed keras and keras-gpu, and then install the python package only.

Install Keras on Anaconda OSX

I am trying to install keras on an anaconda environment (OSX), because I want to use it with spyder - ipython. To do that I just used pip install keras (I already have tensorflow). After the installation when I call python 2.7 from the terminal, keras works fine. But, when I call python 3.5 or spyder and try to import keras I receive:
No module named 'keras'
I assume the issue might be with the PATHS on my MacBook, because which python returns
/usr/local/bin/python2.7
while which python3.5 (or spyder) returns
/Users/georgiospapadopoulos/anaconda/bin/python3.5
/Users/georgiospapadopoulos/anaconda/bin/spyder
Also, during pip install keras shows that
Requirement already satisfied: keras in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
My ~/.bash_profile contains
# added by Anaconda3 2.4.0 installer
export PATH="/Users/georgiospapadopoulos/anaconda/bin:$PATH"
# added by Anaconda3 4.2.0 installer
export PATH="/Users/georgiospapadopoulos/anaconda/bin:$PATH"
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
PATH="/usr/local/bin/python:$PATH"
export PATH
You are probably mixing up the virtual environments. The best way to handle this is to create a vertual env in Anaconda - say, neural-net-venv, and then open the terminal for that venv, and install keras and other related modules. Then go back to Anaconda dash and select that venv as active environment to work on. Then select Jupyter and Spyder and run your imports.
Note that you also should not mix your Python versions - if you must work on Py2 and Py3 - create separate virtual environments for both, install keras, theanos/tensorflows separately in these environments, and you should be good to go.
I have run this setup on MacOS and it works like a charm.
For installing keras in Anaconda, the best and hassle free way is just use open the anaconda prompt and then type:
conda install keras
Keras runs on either tensorflow or theano backends. Once the keras install is complete, just open the python shell and type
>>>import keras
If some error is thrown, then there must be some problem with the backend. So just open the anaconda prompt, and type
conda import tensorflow
theano also can be used. Nevertheless tensorflow is the default one.
I wanted to insatll keras on Anaconda, tried the above approach, but it still did not work. Specifically, I started Anaconda Navigator and then opened a Mac OS terminal in the base environment. Then I followed the conda install commands for keras and tensorflow. It worked fine for keras. But with tensorflow, I got the following error message:
Downloading and Extracting Packages
_tflow_select-2.3.0 | 3 KB | ######################################################### | 100%
ChecksumMismatchError: Conda detected a mismatch between the expected content and downloaded content
for url 'https://conda.anaconda.org/Anaconda/osx-64/_tflow_select-2.3.0-mkl.tar.bz2'.
download saved to: /Users/dlin/opt/anaconda3/pkgs/_tflow_select-2.3.0-mkl.tar.bz2
expected sha256: cc155b27e7bf91ec5370ce1fd2d5fceccbf13ac19706229674ba971fa3751446
actual sha256: aad248699de112a7a5ead1695dfdf51b5693c2927303844b29dd7d9138dc95b9