Jupyter Notebook 中的 TensorFlow 问题 [英] Trouble with TensorFlow in Jupyter Notebook

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

问题描述

我之前通过 Anaconda 在 Ubuntu 14.04 中安装了 Jupyter notebooks,刚刚我安装了 TensorFlow.无论我是在笔记本上工作还是简单地编写脚本,我都希望 TensorFlow 能够正常工作.为了实现这一目标,我最终安装了两次 TensorFlow,一次使用 Anaconda,一次使用 pip.Anaconda 安装有效,但我需要在对 python 的任何调用之前使用source activate tensorflow".并且 pip install 运行良好,如果以标准方式(在终端中)启动 python,那么 tensorflow 加载就好了.

I installed Jupyter notebooks in Ubuntu 14.04 via Anaconda earlier, and just now I installed TensorFlow. I would like TensorFlow to work regardless of whether I am working in a notebook or simply scripting. In my attempt to achieve this, I ended up installing TensorFlow twice, once using Anaconda, and once using pip. The Anaconda install works, but I need to preface any call to python with "source activate tensorflow". And the pip install works nicely, if start python the standard way (in the terminal) then tensorflow loads just fine.

我的问题是:我怎样才能让它在 Jupyter 笔记本中工作?

My question is: how can I also have it work in the Jupyter notebooks?

这让我想到了一个更普遍的问题:我在 Jupyter/Anaconda 中的 python 内核似乎与系统范围内使用的 python 内核(或环境?不确定这里的术语)是分开的.如果这些重合,那就太好了,这样如果我安装一个新的 python 库,我运行 python 的所有不同方式都可以访问它.

This leads me to a more general question: it seems that my python kernel in Jupyter/Anaconda is separate from the python kernel (or environment? not sure about the terminology here) used system wide. It would be nice if these coincided, so that if I install a new python library, it becomes accessible to all the varied ways I have of running python.

推荐答案

更新

TensorFlow 网站支持五种安装.

据我所知,使用 Pip 安装 直接在 Jupyter Notebook 中导入 TensorFlow 就可以了(只要安装了 Jupyter Notebook 并且没有其他问题)b/z 它没有创建任何虚拟环境.

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.

使用 virtualenv 安装并且 conda install 需要将 jupyter 安装到新创建的 TensorFlow 环境以允许 TensorFlow 在 Jupyter Notebook 中工作(有关更多详细信息,请参阅以下原始帖子部分).

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).

我相信 docker install 可能需要一些端口设置VirtualBox 使 TensorFlow 在 Jupyter Notebook 中工作(参见这篇文章).

I believe docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).

对于从源安装,这还取决于源代码构建和安装到哪个环境中.如果安装在新创建的虚拟环境或未安装 Jupyter Notebook 的虚拟环境中,则还需要将 Jupyter Notebook 安装到虚拟环境中才能在 Jupyter Notebook 中使用 Tensorflow.

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.

原帖

要在 Ipython 和/或 Jupyter(Ipython) Notebook 中使用 tensorflow,您需要在 tensorflow 激活环境下安装 Ipython 和 Jupyter(安装 tensorflow 后).

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.

在tensorflow环境下安装Ipython和Jupyter之前,如果在终端执行如下命令:

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

这告诉你,当你从终端打开 python 时,它使用的是安装在安装 tensorflow 的环境"中的那个.因此,您实际上可以成功导入 tensorflow.但是,如果您尝试运行 ipython 和/或 jupyter notebook,则这些未安装在配备 tensorflow 的环境"下,因此必须返回使用没有 tensorflow 模块的常规环境,因此您将获得导入错误.

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.

您可以通过列出 envs/tensorflow/bin 目录下的项目来验证这一点:

You can verify this by listing out the items under envs/tensorflow/bin directory:

(tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/

您会看到没有列出ipython"和/或jupyer".

You will see that there are no "ipython" and/or "jupyer" listing out.

要将 tensorflow 与 Ipython 和/或 Jupyter notebook 一起使用,只需将它们安装到 tensorflow 环境中:

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 #(use pip3 for python3)

安装它们后,envs/tensorflow/bin/目录中应该会出现一个jupyer"和一个ipython".

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

注意事项:在尝试在 jupyter notebook 中导入 tensorflow 模块之前,请尝试关闭 notebook.并且首先源停用张量流",然后重新激活它(源激活张量流")以确保事情在同一页面上".然后重新打开笔记本并尝试导入 tensorflow.它应该成功导入(至少在我的工作中).

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).

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

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