本文共 5217 字,大约阅读时间需要 17 分钟。
转载自: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安装教程:
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 in 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 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 tensorflowusername$ source activate tensorflow(tensorflow)username$ jupyter notebook
Also note that you might have to configure the Keras backend to use Tensorflow. According to 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:
- Create and activate a virtual env. Conda is (according to me) the easiest way to do it.
- Install Jupyter andKeras inside the virtual env.
- Edit the
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 image.
Here is one: . 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.
1. Install virtualenv with pip if you don't have it:
pip install virtualenv
2. With virtualenv, create a new environment in the ~/deeplearn
directory. We will be using python 2.7.
virtualenv ~/deeplearn
3. Enter the new environment (you might want to make an alias for this - alias=):
source ~/deeplearn/bin/activate
在这里,可以安装使用virtualenvwrapper (首先要安装 sudo pip install virtualenvwrapper),可以看我另一个博客第4部分 包进行虚拟环境的管理,使得管理命令更加方便简介(mkvirtualenv env_name 可以创建新的环境,此外可以删除rmvirtualenv old_env和复制cpvirtualenv oldenv newenv。使用lsvirtualenv可以列出所有已经按照的虚拟环境)
4. Install tensorflow to Mac OS X:
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. See 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 可以将虚拟环境与主环境隔离,互相不影响。
Testing the environment
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
.
Change Keras backend
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.
转载地址:https://blog.csdn.net/xiaojiajia007/article/details/60116021 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!