美文网首页Python学习我爱编程
Anaconda+Jupyter Notebook+Tensor

Anaconda+Jupyter Notebook+Tensor

作者: 科学Jia | 来源:发表于2018-02-06 15:17 被阅读1381次

    愿我以后都活的像个混世大魔王,没心没肺,风生水起,什么牵制我,我就放弃什么

    自从混世大魔王给我安利了YJango的文章后,我就对深度学习着迷了,这说明,其实不是DL算法太难,而是讲算法的水平直接影响了观众是否继续的欲望,如果一个复杂的事情能简单描述,并再升华到人生哲理,啊!基本我就投降了。

    从入门到动手感性实践,也就是准备一个Ubuntu的虚拟环境,再接上网络,各种下载和配置环境的过程。其实,整个过程真的很简单。

    以下过程全是在Ubuntu系统下完成的!

    Anaconda安装

    虽然我现在也不确定是否正确念对了它的读音,但是因为它真的是众多大神推荐的旗舰款(包含很多数值计算的python包),先不管它的优缺点,撸下来再说吧。

    安装步骤很简单:
    Anaconda官方网站下载跟系统对应的版本,例如,本人下载的是Anaconda3-5.0.1-Linux-x86_64.sh,运行命令安装

    sudo bash Anaconda2-2.5.0-Linux-x86_64.sh

    安装好以后,就使用conda命令对Anaconda进行管理(这个不是重点,可以不看conda这块命令使用)

    • 更新conda
    conda update conda
    
    • 显示可用packages
    conda list
    
    • 从conda安装package
    conda install package-name
    
    • 删除package
    conda remove package-name
    
    • 更新package
    conda update package-name
    

    Jupyter Notebook

    安装完毕以后,直接在terminal对话框里输入

    Jupyter Notebook

    将会启动传说中非常好用的Jupyter Notebook,每个输入和输出都可以记录下来,好用的magic外部语法命令,而且还可以支持40多种脚本语言,外加Markdown的文章说明,真是好到没朋友。呃呃呃。

    Jupyter Notebook大概长这样

    上图中,左边一栏的目录结构,在读者默认安装后,并不会出现,而是需要额外安装一个插件才能看到,运行如下命令安装该插件,耐心等待安装,并重新打开jupyter notebook,你会看到

    conda install -c conda-forge jupyter_contrib_nbextensions

    安装Nbextensions后Jupyter大概长这样

    点击这个扩展栏,然后勾选Table of Contents(2),你再次打开你的笔记*.ipynb,就会看到如第一个图所示的左侧目录栏。

    勾选笔记内容目录形式

    Tensorflow安装

    前面讲了那么多跟Tensorflow无关的环境,现在终于到了重头戏了,然鹅,可能会比你想象中简单很多。因为我是虚拟机环境,没有真实的显卡,所以,只有放弃GPU的配置,选择了只有有CPU的Tensorflow。

    第二步:划下重点,这里会遇到一个坑,可能你到了第四步,你根本就下载不了这个cpu版本的tensorflow,因为你连不上googleapis.com,所以,你必须得可以翻墙出去,或者说其他的地方可以下载这个文件。

    Python3.6_Tensorflow只支持CPU的版本下载路径

    因为我安装的是Python 3.6版本,所以我翻墙在官网上找到相对应的tensorflow的版本,并下载到本地windows系统,这里还有一个坑是,下载下来到windows系统的包是zip结尾的,,,但是,只需要稍微改成.whl结尾的包,就可以在Linux下愉快的安装了。
    这里提供本人已经下载下来的版本地址,需要的朋友可以直接获取
    链接:https://pan.baidu.com/s/1kXqvnin 密码:akv1

    将下载下来的包放在Ubuntu后,执行安装命令如下,即可

    pip install tensorflow-1.5.0-cp36-cp36m-linux_x86_64.whl

    最后输入python代码验证下tensorflow是否安装成功?

    import tensorflow as tf
    sess = tf.Session()
    a = tf.constant(10)
    b = tf.constant(25)
    print(sess.run(a + b ))
    
    安装成功!!!

    =====================================================
    前方高能 → Jupyter “import tensorflow” 的巨坑和如何填掉巨坑
    =====================================================
    因为是重点,所以一定要加粗
    *开始以为装好了tensorflow,就可以在众星捧月的Jupyter Notebook里愉快玩耍Tensorflow了,结果是,把同样的代码写入到Jupyter Notebook里,它却告知我它没有引入Tensorflow这个模块,找了半天,终于找到了解决方案,赞!

    TensorFlow website supports five installations.
    To my understanding, using Pip installation directly would be fine to import TensorFlow in Jupyter Notebook (as long as Jupyter Notebook was installed and there were no other issues) b/z it didn't create any virtual environments.
    Using virtualenv install and conda install would need to install jupyter into the newly created TensorFlow environment to allow TensorFlow to work in Jupyter Notebook (see the following original post section for more details).
    I believe docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).
    For installing from sources, it also depends on which environment the source code is built and installed into. If it's installed into a freshly created virtual environment or an virtual environment which didn't have Jupyter Notebook installed, it would also need to install Jupyter Notebook into the virtual environment to use Tensorflow in Jupyter Notebook.

    To use tensorflow in Ipython and/or Jupyter(Ipython) Notebook, you'll need to install Ipython and Jupyter (after installing tensorflow) under the tensorflow activated environment.

    Before install Ipython and Jupyter under tensorflow environment, if you do the following commands in terminal:
    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

    This is telling you that when you open python from terminal, it is using the one installed in the "environments" where tensorflow is installed. Therefore you can actually import tensorflow successfully. However, if you are trying to run ipython and/or jupyter notebook, these are not installed under the "environments" equipped with tensorflow, hence it has to go back to use the regular environment which has no tensorflow module, hence you get an import error.
    You can verify this by listing out the items under envs/tensorflow/bin directory:

    (tensorflow) username$ conda list

    You will see that there are no "ipython" and/or "jupyer" listing out.
    To use tensorflow with Ipython and/or Jupyter notebook, simply install them into the tensorflow environment:

    (tensorflow) username$ conda install ipython
    (tensorflow) username$ pip install jupyter

    After installing them, there should be a "jupyer" and a "ipython" show up in the envs/tensorflow/bin/ directory.

    Notes: Before trying to import tensorflow module in jupyter notebook, try close the notebook. And "source deactivate tensorflow" first, and then reactivate it ("source activate tensorflow") to make sure things are "on the same page". Then reopen the notebook and try import tensorflow. It should be import successfully (worked on mine at least).

    **重点就是,因为tensorflow的bin目录下并没有安装ipython和jupyter,所以它无法再jupyter notebook里正常import,需要额外安装ipython和jupyter包,安装成功后,通过 conda list命令可以看到jupyter和ipython。

    最后关闭下开始打开的jupyter notebook,执行以下命令,再打开jupyter notebook即可。

    source deactivate tensorflow
    source activate tensorflow

    以上。

    参考链接,都非常棒

    你为什么要用Jupyter进行分析,知乎答
    第一次亲密接触Jupyter Notebook
    Installing TensorFlow on Ubuntu
    介绍Jupyter扩展插件的安装和使用
    Trouble with TensorFlow in Jupyter Notebook

    简书App

    相关文章

      网友评论

        本文标题:Anaconda+Jupyter Notebook+Tensor

        本文链接:https://www.haomeiwen.com/subject/dgrgzxtx.html