Inconsistent loading of keras backend between theano and tensorflow - python

My keras.json has backend specified to be tensorflow and if I open Spyder and Jupyter IDE then tensorflow is used as a backend.
Strangely if I open python or ipython shell within my WinPython installation, the backend defaults into theano. Has anyone seen this behaviour before and if so what was the solution?
I have tried playing with environment variables to no effect.

Looks like existing notebooks still say Theano. But If I create new one and enter the following then I get proper result as tensorflow
import os
os.environ['KERAS_BACKEND']='tensorflow'
import keras
keras.backend.backend()
Using TensorFlow backend.
Out[1]:
'tensorflow'
In [ ]:

I have exactly same issue. Some how after I installed shell kernel Ipython/Jupyter is pointing properly to tensor flow
import os
os.environ['KERAS_BACKEND']='tensorflow'
import keras
keras.backend.backend()

Related

How does conda interact with VS code?

I am trying to learn python using the conda platform, I added conda to VS Code but when I run my hello world I get this.Terminal Output
The code runs but still has an error. Also, I added tensorflow to my anaconda folders, the tensor flow base and folders show up in my conda environment page, but when I import
tensorflow using import tensorflow as tf and check the version with print(tf.__version__)
I get this error message

Installing Tensorflow and Keras on Intel Pentium

For a university we are supposed to implement a TensorFlow project using the python libraries for tensorflow and keras. I can install both of them just fine using pip3, but executing any piece of code results in some kind of error.
I've settled on testing the very complicated code:
import keras
Using python 3.6 and the newest tensorflow and keras (pip3 install tensorflow keras) I get the error ModuleNotFoundError: No module named 'tensorflow.python'; 'tensorflow' is not a package. I checked, and import tensorflow finds the package, but returns some error about AVX instructions and dumps the core.
I researched, and my CPU does not support AVX instructions which are part of tensorflow >= 1.6.0. I could not find a precompiled version that runs on my laptop without AVX, and I don't have the time to compile myself.
I tried downgrading to tensorflow == 1.5.0 and keras == 2.1.3 which was the version when tensorflow == 1.5.0 was around, but I still get missing errors, for each version and import statement a different one.
For example when I use the code:
import keras
from keras.datasets import mnist
I instead get the error AttributeError: module 'keras.utils' has no attribute 'Sequence'. I'm on an Intel Pentium, which I assume is the problem. I am fully aware that my setup is in no way suitable for machine learning, and it isn't supposed to be, but nevertheless I'd like to work on that assignment.
Anyone got experience with installing TensorFlow on older machines?
System:
Ubuntu 18.04.2 LTS
Intel(R) Pentium(R) 3556U # 1.70GHz (Dual Core)
4GB RAM
I had the same trouble, but it seems to have solved it. (However, the Python version shall be 3.5. )
For CPUs that do not support AVX, the tensorflow must be version 1.5 or lower.
If you want to install Tensorflow 1.5, the Python version must be 3.5 or lower.
The successful procedure is as follows.
(1) Uninstall your Anaconda.
(2) Download the following version of Anaconda from the following
URL. Version: Anaconda3-4.2.0-Windows-x86_64.exe
URL:https://repo.anaconda.com/archive/ or https://repo.anaconda.com/archive/Anaconda3-4.2.0-Windows-x86_64.exe
(3) Double-click the anaconda icon of “(2)” above, and install the
anaconda according to the GUI instructions.
(4) Start Anaconda Prompt
(5) Enter “pip install tensorflow==1.5” in Anaconda Prompt and press
the return key. Wait for the installation to finish. (See the log)
(6) Enter "pip install keras==2.2.4" in Anaconda Prompt and press the
return key. Wait for the installation to finish.(See the log)
This completes the installation. If you Enter " import tensorflow " on Jupiter notebook, some future error may displayed.(See this log.)
System:
My PC does not support AVX like your PC. My PC's specs are as follows.
PC:Surface Go
CPU:Intel(R) Pentium(R) CPU 4415Y @ 1.60 GHz
Windows10:64bit
How to test ?
Enter and execute the following command on Jupiter Note. Or use this file.
import tensorflow as tf
print(tf.__version__)
print(tf.keras.__version__)
or
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
If your install is successful, then following message will be displayed on your Jupiter notebook
1.5.0
2.1.2-tf
P.S.
I'm not very good at English, so I'm sorry if I have some impolite or unclear expressions.
Sticking to the Pentium configuration is not recommended for default tensorflow builds because of AVX dependencies. Also many recent advances in this area are not available in earlier builds of TF and you will find it difficult to replicate research works. Options below:
Get a Google Colab (https://colab.research.google.com/) notebook, install Keras and TF and get going with your work
There have been genuine requests for this support, refer to this link [https://github.com/tensorflow/tensorflow/issues/18689] where unofficial builds are provided. See if one of them works
Build Tensorflow from scratch (very hard option), with the right set of flags for Bazel (remove all AVX/threading options)

Setting of backend for keras

I need to change the keras backend from default tensorflow to theano. But my default python version is 3.7, which does not seem to work with keras (the import line crashes). So, I first had to create a specific environment.
After creating a specific python environment with anaconda, as suggested by 47263006, I did the following:
vi ~/.keras/keras.json (and change the backend name in it)
But with a virtualenv, editing the keras.json file had no effect. So, I resorted to the following solution in the python code:
import os
os.environ['KERAS_BACKEND'] = 'theano'
So I thought that maybe the latter is the more generic soution, and I tried to use it with my anaconda env, but surprise - that did not work there.
So my current solution is that, for anaconda edit the keras.json file and for virtualenv use os.environ.
Is there a more generic solution for setting keras backend which will work for both conda and virtualenv?

Importing theano with GPU device on Windows conda

I'm having some troubles working with a workstation with Conda for Windows. I'm not too familiar with the OS and this is the first time I try GPU support for theano there, to no avail.
The thing is, when I use an Anaconda bash, I can put this:
set "MKL_THREADING_LAYER=GNU"
set THEANO_FLAGS=device=cuda
python
import theano
This works fine, with GPU support. However, I need the script to switch between devices (GPUs and CPU) during the execution. I read somewhere it can be done by setting the environment variables directly on the code, but I tried this to no avail:
import os
os.environ["THEANO_FLAGS"] = "device=cuda"
import theano
The MKL_THREADING_LAYER environment variable is already put in the system, so I guess the error isn't there. Anyways, the code can't run:
RuntimeError: 'path' must be None or a list, not <class '_frozen_importlib_external._NamespacePath'>
Any ideas? Thanks.

How do I use TensorFlow backend in keras without changing keras.json?

Well, I am working on a GPU server. All other users are using keras with Theano backend.
I have installed TensorFlow using virtualenv and later I installed keras using usual pip command. When I try to run keras in this virtual environment, it is using Theano backend because of the /home/user/.keras/keras.json file. I know I can use TensorFlow by making changes in the json file but for some reason other users are not allowing this.
I would like to know if there is a way to use keras with TensorFlow installed in my virtual environment other than the globally installed "Theano".
NOTE: The other users still should be able to use Theano
OS: Ubuntu 16.04
Using #GPhilo documentation reference;
I successfully used TensorFlow backend by adding following lines at the starting of my code:
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
# rest of the code
Make sure you have activated your virtual environment.
From Keras' documentation:
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"
Using TensorFlow backend.

Categories