Install Keras on Anaconda OSX - tensorflow

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

Related

Difficulty updating keras/tensorflow on mac

I am working on a jupyter notebook script which I used last year to train a neural network.
When I try to import the keras tokenizer:
from keras.preprocessing.text import Tokenizer
I receive this error
I have seen other posts which suggest that I need to update tensorflow. My anaconda environment tells me I have 1.13.1 installed. But when I try to update tensorflow-base to 1.15 in the anaconda navigator, I receive this error:
I can update tensorflow from my command line using:
conda install tensorflow=1.15.0
But this doesn't update tensorflow in my anaconda environment and the error persists in my notebook.
Any help would be much appreciated! As you can probably tell, I am a novice python user.
The error says some packages needed to update Tensorflow/Andaconda requires Python 3.11 or newer. Since not all of the error log can be seen however, I would upgrade to python 3.7 to be safe. You can download this from the official page: https://www.python.org/downloads/
If the problem persists, try using pip to update the packages(In bash) :
pip install tensorflow
pip install conda
If you get an error while using pip, try:
pip3 install tensorflow
pip3 install conda
This same method can be used to update keras:
pip install keras
or if that does not work:
pip3 install keras
If pip is not recognized at a command, Python 3.7 is not added to path. I do not have experience with macOS, but this article should go into enough depth.
https://realpython.com/add-python-to-path/#how-to-add-python-to-path-on-linux-and-macos

tensorflow on crestle with python 3.7

I am using crestle.ai site to build some models. In the Jupyter notebook when I try to import something from keras I get :
****ModuleNotFoundError: No module named 'tensorflow'****
I found that tensorflow does not work with Python 3.7 which is pre-installed in crestle. So I tried several ways to install Python 3.5 but nothing worked
!conda install python=3.5.0 --yes
I got:
Solving environment: failed UnsatisfiableError: The following specifications were found to be in conflict:
- jupyter_contrib_nbextensions -> jupyter_highlight_selected_word[version='>=0.1.1'] ->
python[version='>=3.7,<3.8.0a0'] -> readline[version='>=7.0,<8.0a0']
- jupyter_contrib_nbextensions -> jupyter_highlight_selected_word[version='>=0.1.1'] ->
python[version='>=3.7,<3.8.0a0'] -> tk[version='>=8.6.8,<8.7.0a0']
- jupyter_contrib_nbextensions -> jupyter_highlight_selected_word[version='>=0.1.1'] ->
python[version='>=3.7,<3.8.0a0'] -> xz[version='>=5.2.4,<6.0a0']
- python=3.5.0 Use "conda info " to see the dependencies for each package.
!pip3 install --upgrade tensorflow-gpu
I got
Collecting tensorflow-gpu Could not find a version that satisfies
the requirement tensorflow-gpu (from versions: ) No matching
distribution found for tensorflow-gpu
!pip install --upgrade tensorflow
I got:
Collecting tensorflow Could not find a version that satisfies the
requirement tensorflow (from versions: ) No matching distribution
found for tensorflow
I am following suggestions from different blogs and don't know what I am doing or if I am doing it right.
As per Anand's suggestion
!conda env list
​
**# conda environments:
#
base /home/nbuser/.anaconda3
new_environment /home/nbuser/.anaconda3/envs/new_environment
py36 /home/nbuser/.anaconda3/envs/py36
tensorflow /home/nbuser/.anaconda3/envs/tensorflow**
!source activate py36
/bin/sh: 1: source: not found
Do I need some path command?
Edit:
!activate py36
I got no output!
!pip3 install --upgrade tensorflow
Collecting tensorflow
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
enter image description here
enter image description here
Edit:
Thanks. I was not aware of the terminal and was doing it wrong!
This is what I got after $pip install tensorflow-gpu
after pip command
What do I need to do next? I tried in Jupyter importing modules from keras but again I got:
Using TensorFlow backend.
ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 from keras.models import Sequential
I also tried to use keras on crestle.com instead (previous was crestle.ai). I got this:
keras on crestle.com
It shows python 3.6 but the similar issue..
python[version='>=3.7,<3.8.0a0'] This is part of your error, this basically means that you are still unable to get out of using Python3.7, hence the no module found error, I would suggest you to make a new environment in conda using.
conda create --name py36 python=3.6
In your conda prompt, then activate this environment by using
After that use conda env list and you should see an Enviornment named py36.
Activate this environment by using source activate py36
Edit If you are using conda prompt not terminal, directly use activate py36
Then try pip commands again, once the environment is activated.
If it still fails, let me know and we will see what the problem might be.
Edit You have to use this commands in the terminal that comes with crestle and not in its's Jupyter notebook.
The terminal can be accessed from the top right, you can see the New_>Terminal Button. And if it says conda is not installed, you might follow this link. https://www.digitalocean.com/community/tutorials/how-to-install-the-anaconda-python-distribution-on-ubuntu-16-04

Jupyter Notebook kernel dies when importing tensorflow 1.5.0

Jupyter Notebook kernel dies when importing tensorflow 1.5.0
I have read a lot of posts relating to this but they have all had higher version numbers of tensorflow and have solved it by downgrading to 1.5.0. I also had higher version number and followed the advice to downgrade but I still have the problem.
Does anyone know what to try next?
pip install h5py==2.8.0
worked for me
When trying using the command prompt I got an error message not related to the tensorflow issue (I think);
"Warning! HDF5 library version mismatched error"
The key information from that message body was "Headers are 1.10.1, library is 1.10.2" so I downgraded hdf5 library by "conda install -c anaconda hdf5=1.10.1" and now the error message is gone and the kernel does not die when importing tensorflow.
I got similar problems, any tensorflow or tensorflow related packages (e.g. keras) made my kernel to die when loading, from any interface (jupyter, spyder, console....)
For those having this kind of problems, try running python from the console with verbose mode (python -v) then import tensorflow and look for errors.
I spot errors related to h5py, similar to the reply of #DBSE. I just upgraded the h5py package then everything was solved !
If you are using a conda environment, then the easiest method for fixing this issue is to just create a new environment and install tensorflow with just a single command. I had the same issue, I have tried a lot on most of the version of python and tensorflow. But at the last I have successfully configured it with just a single steps.
Run this command for installing GPU version
conda create --name tf_gpu tensorflow-gpu
The above line of code will automatically install that version of python and tf which is comaptible with your GPU or CPU.
For CPU, Run this command
conda create --name tf_env tensorflow
Both of these command work 100 % with my system for GPU and CPU access and will download the latest version which are compatible with system. It will resolved/fixed "Illegal Instruction (code dumps)" error.
pip install h5py==3.1.0
This is the most updated version which worked for me.
Try using import numpy before Keras and Tensorflow.

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

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.