tisPrimeTime
tisPrimeTime

Reputation: 123

Keras backend json is defined to be tensorflow, but Keras still can't find tensorflow

I have changed the json file for keras to be the following:

{
    "image_dim_ordering": "tf", 
    "epsilon": 1e-07, 
    "floatx": "float32", 
    "backend": "tensorflow"
}

But when i run the following simple Keras tutorial for a neural network:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, input_dim=20, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(10, init='uniform'))
model.add(Activation('softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(X_train, y_train,
          nb_epoch=20,
          batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)

As taken from: https://keras.io/getting-started/sequential-model-guide/

I still get the following error:

Using TensorFlow backend.
Traceback (most recent call last):
  File "./keras_test", line 3, in <module>
    from keras.models import Sequential
  File "/usr/local/lib/python2.7/dist-packages/keras/__init__.py", line 2, in <module>
    from . import backend
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/__init__.py", line 67, in <module>
    from .tensorflow_backend import *
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 1, in <module>
    import tensorflow as tf
ImportError: No module named tensorflow

I'm out of ideas on what the issue could be, so some help would be much appreciated.

Initially I thought that it may be a python versioning issue. As I'm a bit new to python coding, and Linux, I've been installing/upgrading all my python versions without really thinking, and I am only hoping that Keras would be using the same python version as my Tensorflow backend (which should be the one used by anaconda3). In hindsight I should really be using virtualenv, but I'm not sure if this is my problem or not (just trying to give as much info as I can).

I installed Keras using:

 sudo pip install keras

If I run it again I get the following output:

Requirement already satisfied (use --upgrade to upgrade): keras in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): theano in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): pyyaml in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from keras)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.11 in /usr/local/lib/python2.7/dist-packages (from theano->keras)
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Also I am 100% sure my Tensorflow installation works, as I have run (and have coded up) some GPU-Cuda examples for it.

Thanks!

Upvotes: 1

Views: 850

Answers (2)

Shabaz Patel
Shabaz Patel

Reputation: 291

You can install all the dependencies for tensorflow along with keras as follows,

This setup is for Ubuntu 14.04 server

# Pick up some TF dependencies
apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    git \
    cmake \
    libfreetype6-dev \
    libpng12-dev \
    libzmq3-dev \
    pkg-config \
    python \
    python-dev \
    rsync \
    software-properties-common \
    unzip \
    && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

curl -O https://bootstrap.pypa.io/get-pip.py && \
python get-pip.py && \
rm get-pip.py

pip --no-cache-dir install --upgrade ipython && \
pip --no-cache-dir install \
    ipykernel \
    jupyter \
    matplotlib \
    numpy \
    scipy \
    sklearn \
    pandas \
    Pillow \
    && \
python -m ipykernel.kernelspec

# Install TensorFlow CPU version from central repo
pip --no-cache-dir install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp27-none-linux_x86_64.whl

# h5py is optional dependency for keras
apt-get update && apt-get install -y libhdf5-dev libhdf5-serial-dev
pip install keras h5py

If you're still having environment issues, I would suggest using this Dockerfile. This allows us to work independently of local python, which I have found very helpful to replicate the environment on any other system. You might also find Datmo conversion useful to facilitate this.

DISCLAIMER: I work at this company called Datmo, and we are building a community of developers by simplifying the machine learning workflow

Upvotes: 1

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

I think you forgot the most obvious thing, TensorFlow is not installed and it is not a Keras dependency. I recommend you to install TensorFlow with:

pip install --user tensorflow

This will install TensorFlow in our user folder (~/.local) and does not require root privileges.

Upvotes: 2

Related Questions