在 Jupyter Notebook 中运行 Tensorflow [英] Running Tensorflow in Jupyter Notebook

查看:90
本文介绍了在 Jupyter Notebook 中运行 Tensorflow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些深度学习工作.为此,我首先在我的 Python 环境中安装了所有用于深度学习的包.

I am trying to do some deep learning work. For this, I first installed all the packages for deep learning in my Python environment.

这就是我所做的.

在Anaconda中,我创建了一个名为tensorflow的环境,如下

In Anaconda, I created an environment called tensorflow as follows

conda create -n tensorflow

然后在其中安装数据科学 Python 包,如 Pandas、NumPy 等.我还在那里安装了 TensorFlow 和 Keras.这是该环境中的软件包列表

Then installed the data science Python packages, like Pandas, NumPy, etc., inside it. I also installed TensorFlow and Keras there. Here is the list of packages in that environment

(tensorflow) SFOM00618927A:dl i854319$ conda list
# packages in environment at /Users/i854319/anaconda/envs/tensorflow:
#
appdirs                   1.4.3                     <pip>
appnope                   0.1.0                    py36_0  
beautifulsoup4            4.5.3                    py36_0  
bleach                    1.5.0                    py36_0  
cycler                    0.10.0                   py36_0  
decorator                 4.0.11                   py36_0  
entrypoints               0.2.2                    py36_1  
freetype                  2.5.5                         2  
html5lib                  0.999                    py36_0  
icu                       54.1                          0  
ipykernel                 4.5.2                    py36_0  
ipython                   5.3.0                    py36_0  
ipython_genutils          0.2.0                    py36_0  
ipywidgets                6.0.0                    py36_0  
jinja2                    2.9.5                    py36_0  
jsonschema                2.5.1                    py36_0  
jupyter                   1.0.0                    py36_3  
jupyter_client            5.0.0                    py36_0  
jupyter_console           5.1.0                    py36_0  
jupyter_core              4.3.0                    py36_0  
Keras                     2.0.2                     <pip>
libpng                    1.6.27                        0  
markupsafe                0.23                     py36_2  
matplotlib                2.0.0               np112py36_0  
mistune                   0.7.4                    py36_0  
mkl                       2017.0.1                      0  
nbconvert                 5.1.1                    py36_0  
nbformat                  4.3.0                    py36_0  
notebook                  4.4.1                    py36_0  
numpy                     1.12.1                    <pip>
numpy                     1.12.1                   py36_0  
openssl                   1.0.2k                        1  
packaging                 16.8                      <pip>
pandas                    0.19.2              np112py36_1  
pandocfilters             1.4.1                    py36_0  
path.py                   10.1                     py36_0  
pexpect                   4.2.1                    py36_0  
pickleshare               0.7.4                    py36_0  
pip                       9.0.1                    py36_1  
prompt_toolkit            1.0.13                   py36_0  
protobuf                  3.2.0                     <pip>
ptyprocess                0.5.1                    py36_0  
pygments                  2.2.0                    py36_0  
pyparsing                 2.1.4                    py36_0  
pyparsing                 2.2.0                     <pip>
pyqt                      5.6.0                    py36_2  
python                    3.6.1                         0  
python-dateutil           2.6.0                    py36_0  
pytz                      2017.2                   py36_0  
PyYAML                    3.12                      <pip>
pyzmq                     16.0.2                   py36_0  
qt                        5.6.2                         0  
qtconsole                 4.3.0                    py36_0  
readline                  6.2                           2  
scikit-learn              0.18.1              np112py36_1  
scipy                     0.19.0              np112py36_0  
setuptools                34.3.3                    <pip>
setuptools                27.2.0                   py36_0  
simplegeneric             0.8.1                    py36_1  
sip                       4.18                     py36_0  
six                       1.10.0                    <pip>
six                       1.10.0                   py36_0  
sqlite                    3.13.0                        0  
tensorflow                1.0.1                     <pip>
terminado                 0.6                      py36_0  
testpath                  0.3                      py36_0  
Theano                    0.9.0                     <pip>
tk                        8.5.18                        0  
tornado                   4.4.2                    py36_0  
traitlets                 4.3.2                    py36_0  
wcwidth                   0.1.7                    py36_0  
wheel                     0.29.0                    <pip>
wheel                     0.29.0                   py36_0  
widgetsnbextension        2.0.0                    py36_0  
xz                        5.2.2                         1  
zlib                      1.2.8                         3  
(tensorflow) SFOM00618927A:dl i854319$

可以看到jupyter也安装了.

现在,当我在此环境中打开 Python 解释器并运行基本的 TensorFlow 命令时,一切正常.但是,我想在 Jupyter notebook 中做同样的事情.所以,我创建了一个新目录(在这个环境之外).

Now, when I open up the Python interpreter in this environment and I run the basic TensorFlow command, it all works fine. However, I wanted to do the same thing in the Jupyter notebook. So, I created a new directory (outside of this environment).

mkdir dl

在那,我激活了tensorflow环境

SFOM00618927A:dl i854319$ source activate tensorflow
(tensorflow) SFOM00618927A:dl i854319$ conda list

我可以在其中看到相同的软件包列表.

And I can see the same list of packages in that.

现在,我打开一个 Jupyter 笔记本

Now, I open up a Jupyter notebook

SFOM00618927A:dl i854319$ source activate tensorflow
(tensorflow) SFOM00618927A:dl i854319$ jupyter notebook

它会在浏览器中打开一个新笔记本.但是当我只导入基本的 python 库时,比如熊猫,它说没有可用的包".我不确定为什么当相同的环境具有所有这些包并且位于相同的目录中时,如果我使用 Python 解释器它会显示所有包.

It opens up a new notebook in the browser. But when I just import basic python libraries in that, like pandas, it says "no packages available". I am not sure why is that when the same environment has all those packages and in the same directory, if I use Python interpreter it shows all packages.

import pandas
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-d6ac987968b6> in <module>()
----> 1 import pandas

ModuleNotFoundError: No module named 'pandas'

为什么 jupyter notebook 没有选择这些模块?

Why jupyter notebook is not picking up these modules?

因此,Jupyter notebook 不会将 env 显示为解释器

So, Jupyter notebook doesn't show env as the interpreter

推荐答案

我想出了你的案例.我就是这样整理的

I came up with your case. This is how I sort it out

  1. 安装蟒蛇
  2. 创建虚拟环境 - conda create -n tensorflow
  3. 进入你的虚拟环境 -(在 macOS/Linux 上:)source activate tensorflow(在 Windows 上:activate tensorflow)
  4. 在里面安装 tensorflow.您可以使用 pip
  5. 安装它
  6. 完成安装
  1. Install Anaconda
  2. Create a virtual environment - conda create -n tensorflow
  3. Go inside your virtual environment - (on macOS/Linux:) source activate tensorflow (on Windows: activate tensorflow)
  4. Inside that install tensorflow. You can install it using pip
  5. Finish install

接下来,当你启动它时:

So then the next thing, when you launch it:

  1. 如果你不在虚拟环境类型中 - Source Activate Tensorflow
  2. 然后再在里面安装你的 Jupiter notebook 和 Pandas 库,因为在这个虚拟环境中可能会缺少一些

在虚拟环境中输入:

  1. pip 安装 jupyter notebook
  2. pip install pandas

然后你可以启动 jupyter notebook 说:

Then you can launch jupyter notebook saying:

  1. jupyter notebook
  2. 选择正确的终端 python 3 或 2
  3. 然后导入这些模块

这篇关于在 Jupyter Notebook 中运行 Tensorflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆