Jupyter Notebook 中的 TensorFlow 问题

Trouble with TensorFlow in Jupyter Notebook

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

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

这引出了一个更普遍的问题:我在 Jupyter/Anaconda 中的 python 内核似乎与 python 内核(或环境?不确定这里的术语) 在系统范围内使用。如果这些重合就好了,这样一来,如果我安装一个新的 python 库,我可以通过各种方式访问​​它 运行 python.

您的 Anaconda 安装目录可能与 Python 安装目录不同

例如在我的机器上我可以在这里找到位置

yaroslavvb-macbookpro:~ yaroslavvb$ which ipython
/Users/yaroslavvb/anaconda/bin/ipython

当您键入 python 时,它会尝试按从左到右的顺序在 PATH 中找到它。所以你可能在 Anaconda 文件夹之前的文件夹中有另一个版本的 python,它会使用它。要修复,你可以做 export PATH=.... 来更改路径,并将 Anaconda 目录放在前面,这样它就从那里获取 python 而不是默认值,即

export PATH=/Users/yaroslavvb/anaconda/bin:$PATH

我认为您的问题与此处的问题 post 非常相似。 Windows 7 jupyter notebook executing tensorflow。正如 Yaroslav 提到的,您可以尝试

conda install -c http://conda.anaconda.org/jjhelmus tensorflow .

我用 Conda conda install pip 而不是 apt-get install python-pip python-dev 安装了 PIP。

然后安装tensorflow 使用 Pip Installation:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

...

pip install --upgrade $TF_BINARY_URL

然后它会在jupyter notebook中工作。

更新

TensorFlow website支持五个安装。

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

使用virtualenv install and conda install需要在新建的TensorFlow环境中安装jupyter才能让TensorFlow在Jupyter Notebook中运行(见下文原文post部分了解更多详情)。

我相信docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook ()。

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

原版Post

要在Ipython and/or Jupyter(Ipython) Notebook中使用tensorflow,需要在tensorflow下安装Ipython和Jupyter(安装tensorflow后)激活环境。

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

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 时,它使用的是安装在 "environments" 中安装的那个,那里安装了 tensorflow。因此,您实际上可以成功导入 tensorflow。但是,如果你是想运行 ipython and/or jupyter notebook,这些都没有安装在"environments"下配备tensorflow,所以得回去用正规的没有tensorflow模块的环境,因此会出现导入错误。

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

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

你会看到没有"ipython"and/or"jupyer"列出来。

要将 tensorflow 与 Ipython and/or Jupyter 笔记本一起使用,只需将它们安装到 tensorflow 环境中:

(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter #(use pip3 for python3)

安装后,envs/tensorflow/bin/ 目录中应该会出现 "jupyer" 和 "ipython"。

备注: 在尝试在 jupyter notebook 中导入 tensorflow 模块之前,请尝试关闭 notebook。并先 "source deactivate tensorflow",然后重新激活它 ("source activate tensorflow") 以确保事情是 "on the same page"。然后重新打开笔记本并尝试导入 tensorflow。它应该导入成功(至少在我的工作)。

我有另一个解决方案,您不需要在每次使用 jupyter notebook 之前 source activate tensorflow

分区 1

首先,你应该确保你已经在你的虚拟环境中安装了jupyter。如果已经安装,可以跳过本节(使用which jupyter查看)。如果没有,你可以 运行 source activate tensorflow,然后通过 conda install jupyter 在你的 virtualenv 中安装 jupyter。 (您也可以使用 pip。)

第 2 部分

1.From 在你的虚拟环境中,运行

username$ source activate tensorflow
(tensorflow)username$ ipython kernelspec install-self --user

这将为您的 virtualenv 创建一个 kernelspec 并告诉您它在哪里:

(tensorflow)username$ [InstallNativeKernelSpec] Installed kernelspec pythonX in /home/username/.local/share/jupyter/kernels/pythonX

其中 pythonX 将匹配您的 virtualenv 中 Python 的版本。

2.Copy 新的内核规范在某个地方很有用。为你的新内核选择一个 kernel_name,它不是 python2python3 或你以前使用过的内核,然后:

(tensorflow)username$ mkdir -p ~/.ipython/kernels
(tensorflow)username$ mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>

3.If 你想更改 IPython 显示的内核名称,你需要编辑 ~/.ipython/kernels/<kernel_name>/kernel.json 并更改名为 [=23 的 JSON 键=] 取一个你喜欢的名字。

4.You 现在应该能够在 IPython 笔记本菜单中看到您的内核:Kernel -> Change kernel 并且能够切换到它(您可能需要在它之前刷新页面出现在列表中)。 IPython 将记住从那时起该笔记本使用哪个内核。

Reference.

我在使用自定义 Ubuntu 16 图片时遇到了类似的问题。该问题与我的系统上已安装的 numpy 的现有版本有关。

我最初尝试过

sudo pip3 install tensorflow

这导致了以下异常:

Exception: Traceback (most recent call last): File "/anaconda/envs/py35/lib/python3.5/shutil.py", line 538, in move os.rename(src, real_dst) PermissionError: [Errno 13] Permission denied: '/anaconda/envs/py35/lib/python3.5/site-packages/numpy' -> '/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/numpy'

文档建议,如果您在使用此命令时遇到任何问题,请尝试以下操作:

sudo pip3 install --upgrade \ 
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

但是,我的系统无法找到 pip3

sudo: pip3 command not found

终极解决方案是为 pip3

创建一个 symlink
sudo ln -s /anaconda/envs/py35/bin/pip3.5 /usr/local/bin/pip3

最后,以下命令顺利运行

sudo /usr/local/bin/pip3 install --upgrade \ 
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

我在 terminal 中验证了安装,也在我的 Jupyter Notebook

中验证了成功导入
import tensorflow as tf

接受的答案(旷中宇)刚刚帮助了我。我在这里创建了一个 environment.yml 文件,使我能够使这个 conda / tensorflow 安装过程可重复。

第 1 步 - 创建 Conda environment.yml 文件

environment.yml 看起来像这样:

name: hello-tensorflow
dependencies:
  - python=3.6
  - jupyter
  - ipython
  - pip:
    - https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp36-cp36m-linux_x86_64.whl

注:

  • 只需将名称替换为您想要的任何名称即可。 (我的是 hello-tensorflow
  • 只需将 python 版本替换为您想要的任何版本即可。 (我的是 3.6
  • 只需将 tensorflow pip install URL 替换为任何你想要的(我的是 Tensorflow URL,其中 Python 3.6 具有 GPU 支持)

第 2 步 - 创建 Conda 环境

由于 environment.yml 位于您所在的当前路径中,此命令会创建环境 hello-tensorflow(或您将其重命名为的任何内容):

conda env create -f environment.yml

第 3 步:源激活

激活新创建的环境:

source activate hello-tensorflow

第 4 步 - python/jupyter/ipython

哪个python...

(hello-tensorflow) $ which python
/home/johnny/anaconda3/envs/hello-tensorflow/bin/python

哪个 jupyter...

(hello-tensorflow) $ which jupyter
/home/johnny/anaconda3/envs/hello-tensorflow/bin/jupyter

哪个ipython...

(hello-tensorflow) $ which ipython
/home/johnny/anaconda3/envs/hello-tensorflow/bin/ipython

第 5 步

您现在应该可以从 python、jupyter(console / qtconsole / notebook 等)和 ipython.

导入 tensorflow

这是我在 Anaconda -> Jupyter 中启用 tensorflow 所做的。

  1. 使用
  2. 中提供的说明安装 Tensorflow
  3. 转到 /Users/username/anaconda/env 并确保已安装 Tensorflow
  4. 打开 Anaconda 导航器并转到 "Environments"(位于左侧导航中)
  5. Select "All" 在第一个下拉菜单中搜索 Tensorflow
  6. 如果未启用,请在复选框中启用它并确认接下来的过程。
  7. 现在打开一个新的 Jupyter 笔记本,tensorflow 应该可以工作

我在 virtualenv 中使用了以下这些。

pip3 install --ignore-installed ipython
pip3 install --ignore-installed jupyter

这会在我的 tensorflow 虚拟环境中重新安装 ipython 和 jupyter notebook。安装后可以通过which ipythonwhich jupyter验证。 bin 将在虚拟环境下。

NOTE I am using python 3.*

我想知道从 tensorflow 环境中简单地启动 ipython 是否还不够。那是 1)首先激活tensorflow virtualenv:

source ~/tensorflow/bin/activate

2) 在 tensorflow 环境下启动 ipython

(tensorflow)$ ipython notebook --ip=xxx.xxx.xxx.xxx

我从别人的post中找到了解决方案。简单又好用!

http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

只需在命令提示符中安装以下内容,并在 Jupyter Notebook 中将内核更改为 Python 3。它会成功导入tensorflow。

pip install tornado==4.5.3

pip install ipykernel==4.8.2

(原post:https://github.com/tensorflow/tensorflow/issues/11851)

Jupyter 实验室:ModuleNotFound 张量流

对于我或 运行 进入此问题的同事的未来版本:

conda install -c conda-forge jupyter jupyterlab keras tensorflow

原来 jupyterlabjupyter 的插件。

因此,即使您处于 jupyter 没有 jupyterlab 的环境中,如果您尝试 运行:

jupyter lab

然后 jupyter 将在 (base) 环境中查找 jupyterlab 插件。

那么您在 jupyter lab 中的导入将与该插件相关,并且 而不是 您的 conda 环境。

打开 Anaconda 提示屏幕: (base) C:\Users\YOU>conda create -n tf tensorflow

创建环境后输入:conda activate tf

提示移动到(tf)环境,即:(tf) C:\Users\YOU>

然后在此 (tf) 环境中安装 Jupyter Notebook: conda install -c conda-forge jupyterlab - jupyter notebook

还在(tf)环境中,即类型 (tf) C:\Users\YOU>jupyter notebook

笔记本屏幕启动!!

一个新笔记本然后可以import tensorflow

从那时起 打开会话 点击 Anaconda 提示, 输入 conda activate tf

提示移动到tf环境 (tf) C:\Users\YOU>

然后键入 (tf) C:\Users\YOU>jupyter notebook

pip install tensorflow

这在我的 conda 虚拟环境中对我有用。

我试图在已经安装了 jupyter notebooks 的 conda 虚拟环境中使用 conda install tensorflow,导致许多冲突和失败。但是 pip install 工作正常。