转载自:https://www.quora.com/How-can-I-work-with-Keras-on-a-Jupyter-notebook-using-Tensorflow-as-backend
https://gist.github.com/USTClj/d3c9246cb44d25769acfa023a459abab#setup
github上多种tensorflow安装教程: https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/get_started/os_setup.md
After installing this configuration on different machines (both OSX and Ubuntu Linux) I will use this answer to at least document it for myself. I might be missing something obvious, but the installation of this simple combination is not as trivial as it seems. The issue is that following the simple procedure described inPriyank Mathur's answer to How can I work with Keras on a Jupyter notebook using Tensorflow as backend? does not work for me. And, again, I have tried in different computers and OS. And the reason that it does not work is that you need to make sure that iPython, Jupyter, Keras, and Tensorflow are all installed in the same environment. If you don’t install things in the right way and order, chances are you won’t get them installed in the right place. FWIW, getting Keras and Jupyter with the Theano backend is pretty easy, the complication comes from Tensorflow.
As described in this answer at Stackoverflow, once you have installed iPython, Jupyter, and Tensorflow, you can realize easily the mess you are in by issuing the following commands:
username$ source activate tensorflow
(tensorflow)username$ which ipython
(tensorflow)username$ /Users/username/anaconda/bin/ipython
(tensorflow)username$ which jupyter
(tensorflow)username$ /Users/username/anaconda/bin/jupyter
(tensorflow)username$ which python
(tensorflow)username$ /User/username//anaconda/envs/tensorflow/bin/python
It turns out that ipython and jupyter are in a different environment from tensorflow. The solution is to actually install iPython, Jupyter, and Kerasinside the tensorflow environment:
(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter
(tensorflow) username$ pip install keras
Once you have done this, you probably want to deactivate and activate tensorflow again. Then you are ready to go to call jupyter.
(tensorflow)username$ source deactivate tensorflow
username$ source activate tensorflow
(tensorflow)username$ jupyter notebook
Also note that you might have to configure the Keras backend to use Tensorflow. According tothe documentation it will use Tensorflow by default, but I remember having had to edit at least once by hand. In any case all you need to do is edit ~/.keras/keras.json to specify tensorflow instead of theano:
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
I hope this helps, and do let me know if there is an easier way to get this configuration working.
Here are the steps you need to follow:
keras.json
(you will find in the.keras
folder) config file: {"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"}
That’s it.
Another alternative would be to use a Docker[4] image.
Here is one: ivanvanderbyl/tensorflow-keras-docker. That being said, be warned that I haven’t tested it (the Dockerfile looks fine at first glance).
Enjoy
Let's set up a python environment with the tools we will need for deep learning. For simplicity and to avoid conflicts with other libraries installed on your system, we will use virtualenv to create a python environment and then set up Tensorflow, Keras, and IPython Notebook.
pip install virtualenv
~/deeplearn
directory. We will be using python 2.7.virtualenv ~/deeplearn
source ~/deeplearn/bin/activate
在这里,可以安装使用virtualenvwrapper (首先要安装 sudo pip install virtualenvwrapper),可以看我另一个博客第4部分 查看包进行虚拟环境的管理,使得管理命令更加方便简介(mkvirtualenv env_name 可以创建新的环境,此外可以删除rmvirtualenv old_env和复制cpvirtualenv oldenv newenv。使用lsvirtualenv可以列出所有已经按照的虚拟环境)
pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp27-none-any.whl
Install Keras:
pip install keras
Install IPython Notebook
pip install Jupyter
We will want some utilities from scikit-learn, so install that too:
pip install sklearn
Along with matplotlib
pip install matplotlib
NOTE: With this installation, you will only be able to use matplotlib inline in IPython Notebooks. Seehttp://matplotlib.org/faq/virtualenv_faq.html for alternatives. One easy option (though it opens you to possibly hard to debug environment problems) is to create the virtualenv with virtualenv --system-site-packages ~/deeplearn
and pip install matplotlib in your system python, outside of the virtualenv. 使用 --no-site-package 可以将虚拟环境与主环境隔离,互相不影响。
Now you should be able to run a simple test script that learns a shallow model on dummy data as well as plots it.
Start IPython notebook:
jupyter notebook
and open and run Test_Installation.ipynb
.
When you ran Keras in the test script, it created a configuration file located at~/.keras/keras.json
that probably looks like this:
{"epsilon": 1e-07, "floatx": "float32", "backend": "theano"}
It specifies the backend that it uses. Let's switch "theano"
to "tensorflow"
. When you rerun the test script, you should now see it print out that it is using the Tensorflow backend.