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?
Related
I have python working on WSL2 along with ubuntu20.04. I then installed miniconda and then, also installed all common data package, such as: tensorflow, pandas, scikit-learn, matplotlib, sqlalchemy, seaborn pip git
Everything is working fine.
I also have PYCHARM professional installed and as a python interpreter, I am using WSL2(ubuntu20.04). When I try to run the same code that rans fine from WSL2 terminal, PYCHARM complains about unresolved reference to "sklearn" and offers to download that package. Two questions:
i. Should not PYCHARM has access to whatever packages are available from WSL2/Ubuntu20.04 terminal, as I am using WSL2 as the PYTHON interpreter?
ii. If I let PYCHARM download package regardless, would not it create duplicate packages that could be possibly different versions?
# import the necessary packages
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
I am also attaching python interpreter screenshot to show that I a doing it correctly.
UPDATE:
based on #batuhand suggestion, I would like to try using the virtual enviroment. However, the problem is that WSL interpreter is not available.
When I choose WSL interpreter, then \usr\bin\python3 is available.
When I choose virtual environment, \usr\bin\python is not available.
So, it seems that I can not follow #batuhand 's suggestion.
Thanks #PavelKarateev. He pointed out to me on JetBrains.com that my interpreter was pointing to /usr/bin/python3 and I have point it to current location. This in my case is:
wsl://UBUNTU2004/home/$USER/miniconda3/envs/PipInConda_DKU/bin/python3.
Here "PipInConda_DKU" was the virtual environment that I created inside the Conda. As the name suggest, I was also using pip to install some package from Anaconda.
You can create a virtual environment for each project in pycharm. If you do that, all you have to do is install packages with pip in pycharm terminal and you will not see any duplication error.
I have installed tensorflow on my Ubuntu, I can import it from shell but I can't from pycharm. Here is my screen :
How can I solve this?
You should name your project differently: now you are trying to import package tensorflow (your project) and use it inside of it.
Try to rename your working directory to tensorflow_test or something different than tensorflow.
You have to change the python interpreter that PyCharm is using.
Go to options->project->project interpreter and select the correct one, most likely a virtualenv you created, where you installed tensorflow instead of the default.
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.
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.
I'm trying to run the TensorFlow Python examples for with Eclipse Mars for Mac OS X, but I'm getting the following error:
Unresolved import: tensorflow.python.platform
The error is raised on the 3rd line:
from __future__ import absolute_import
from __future__ import print_function
import tensorflow.python.platform
I'm new to Python and TensorFlow. I'm using PyDev for Eclipse. I read somewhere that I can add code to the "External Libraries" folder but I'm not sure where the TensorFlow library is.
I installed TensorFlow following the installation directions on the TensorFlow website.
What am I missing?
It sounds like you are running into a problem with your Python environment. The easiest way to deal with this is to follow these steps:
Install TensorFlow into a virtualenv, by following the instructions here. This avoids the need to acquire root privileges, and makes it easier to experiment with different versions of TensorFlow. (It also avoids conflicts with other system-installed versions of different libraries.)
Set up PyDev to use your new virtualenv, by following step 5 onwards in this guide. This involves setting up a new "interpreter" whose library will be the set of libraries in the virtualenv. You may need to recreate your Eclipse project to use this interpreter.
install tensorflow.
in eclipse , assume you have pydev installed, go to preferences>pydev>python interpreter.
in the libraries tab add the path to the lib folder of your tensorflow lib folder.
here is screenshot:
I also had import issues. I fixed it by creating a new project and specifying interpreter as ~/tensorflow/lib/python2.7
Eclipse seems to then point to the right interpreter and offers to import other relevant files - result is similar to answer above.
The answer from #mrry above was helpful - only the link to the 5 step guide seems to be unavailable.
Hope this helps!