keras, tensorflow, jupyter的虚拟环境安装, using virtulaenv

16 篇文章 0 订阅
13 篇文章 1 订阅

转载自: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:

  1. Create and activate a virtual env. Conda[1] is (according to me) the easiest way to do it.
  2. Install Jupyter[2] andKeras[3] inside the virtual env.
  3. 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[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.

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. 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  可以将虚拟环境与主环境隔离,互相不影响。

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.

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值