Python Jupyter Notebook 中的 TensorFlow 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37061089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble with TensorFlow in Jupyter Notebook
提问by Surgical Commander
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.
我之前通过 Anaconda 在 Ubuntu 14.04 中安装了 Jupyter notebooks,刚刚我安装了 TensorFlow。无论我是在笔记本上工作还是简单地编写脚本,我都希望 TensorFlow 能够正常工作。为了实现这一目标,我最终安装了两次 TensorFlow,一次使用 Anaconda,一次使用 pip。Anaconda 安装有效,但我需要在对 python 的任何调用之前使用“source activate tensorflow”。并且 pip install 运行良好,如果以标准方式(在终端中)启动 python,那么 tensorflow 加载就好了。
My question is: how can I also have it work in the Jupyter notebooks?
我的问题是:我怎样才能让它在 Jupyter 笔记本中工作?
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.
这让我想到了一个更普遍的问题:我在 Jupyter/Anaconda 中的 python 内核似乎与系统范围内使用的 python 内核(或环境?不确定这里的术语)是分开的。如果这些重合,那就太好了,这样如果我安装一个新的 python 库,我运行 python 的所有不同方式都可以访问它。
回答by Zhongyu Kuang
Update
更新
TensorFlow websitesupports five installations.
TensorFlow 网站支持五种安装。
To my understanding, using Pip installationdirectly 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.
据我了解,直接使用Pip 安装可以在 Jupyter Notebook 中导入 TensorFlow(只要安装了 Jupyter Notebook 并且没有其他问题)b/z 它没有创建任何虚拟环境。
Using virtualenv installand conda installwould 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).
使用virtualenv install和conda install需要将 jupyter 安装到新创建的 TensorFlow 环境中,以允许 TensorFlow 在 Jupyter Notebook 中工作(有关更多详细信息,请参阅以下原始帖子部分)。
I believe docker installmay require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).
我相信docker install可能需要在 VirtualBox 中设置一些端口才能使 TensorFlow 在 Jupyter Notebook 中工作(请参阅此帖子)。
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.
对于从源代码安装,它还取决于源代码构建和安装到的环境。如果它安装到新创建的虚拟环境或未安装 Jupyter Notebook 的虚拟环境中,则还需要将 Jupyter Notebook 安装到虚拟环境中才能在 Jupyter Notebook 中使用 Tensorflow。
Original Post
原帖
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.
要在 Ipython 和/或 Jupyter(Ipython) Notebook 中使用 tensorflow,您需要在 tensorflow 激活环境下安装 Ipython 和 Jupyter(安装 tensorflow 后)。
Before install Ipython and Jupyter under tensorflow environment, if you do the following commands in terminal:
在 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
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.
这告诉你,当你从终端打开 python 时,它使用的是安装在安装 tensorflow 的“环境”中的那个。因此,您实际上可以成功导入 tensorflow。但是,如果您尝试运行 ipython 和/或 jupyter notebook,则这些未安装在配备 tensorflow 的“环境”下,因此必须返回使用没有 tensorflow 模块的常规环境,因此您将获得导入错误。
You can verify this by listing out the items under envs/tensorflow/bin directory:
您可以通过列出 envs/tensorflow/bin 目录下的项目来验证这一点:
(tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/
You will see that there are no "ipython" and/or "jupyer" listing out.
您会看到没有列出“ipython”和/或“jupyer”。
To use tensorflow with Ipython and/or Jupyter notebook, simply install them into the tensorflow environment:
要将 tensorflow 与 Ipython 和/或 Jupyter notebook 一起使用,只需将它们安装到 tensorflow 环境中:
(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter #(use pip3 for python3)
After installing them, there should be a "jupyer" and a "ipython" show up in the envs/tensorflow/bin/ directory.
安装它们后,envs/tensorflow/bin/ 目录中应该会出现一个“jupyer”和一个“ipython”。
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 模块之前,请尝试关闭 notebook。并且首先“源停用 tensorflow”,然后重新激活它(“源激活 tensorflow”)以确保事情“在同一页面上”。然后重新打开笔记本并尝试导入 tensorflow。它应该成功导入(至少在我的工作过)。
回答by as - if
i used these following which in virtualenv.
我在virtualenv中使用了以下这些。
pip3 install --ignore-installed ipython
pip3 install --ignore-installed jupyter
This re-installs both ipython and jupyter notebook in my tensorflow virtual environment. You can verify it after installation by which ipython
and which jupyter
. The bin
will be under the virtual env.
这会在我的 tensorflow 虚拟环境中重新安装 ipython 和 jupyter notebook。安装后可以通过which ipython
和进行验证which jupyter
。在bin
将虚拟包膜下。
NOTEI am using python 3.*
注意我使用的是 python 3.*
回答by user5746429
I have another solution that you don't need to source activate tensorflow
before using jupyter notebook
every time.
我有另一种解决方案,您source activate tensorflow
在jupyter notebook
每次使用之前都不需要。
Partion 1
第 1 部分
Firstly, you should ensure you have installed jupyter in your virtualenv. If you have installed, you can skip this section (Use which jupyter
to check). If you not, you could run source activate tensorflow
, and then install jupyter in your virtualenv by conda install jupyter
. (You can use pip
too.)
首先,你应该确保你已经在你的 virtualenv 中安装了 jupyter。如果已经安装,可以跳过本节(which jupyter
用于检查)。如果没有,您可以运行source activate tensorflow
,然后在您的 virtualenv 中安装 jupyter conda install jupyter
。(你也可以使用pip
。)
Partion 2
第 2 部分
1.From within your virtualenv, run
1.从你的virtualenv中,运行
username$ source activate tensorflow
(tensorflow)username$ ipython kernelspec install-self --user
This will create a kernelspec for your virtualenv and tell you where it is:
这将为您的 virtualenv 创建一个 kernelspec 并告诉您它在哪里:
(tensorflow)username$ [InstallNativeKernelSpec] Installed kernelspec pythonX in /home/username/.local/share/jupyter/kernels/pythonX
Where pythonX will match the version of Python in your virtualenv.
pythonX 将匹配你的 virtualenv 中的 Python 版本。
2.Copy the new kernelspec somewhere useful. Choose a kernel_name
for your new kernel that is not python2
or python3
or one you've used before and then:
2.将新的内核规范复制到有用的地方。kernel_name
为您的新内核选择一个不是python2
或python3
您以前使用过的内核,然后:
(tensorflow)username$ mkdir -p ~/.ipython/kernels
(tensorflow)username$ mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>
3.If you want to change the name of the kernel that IPython shows you, you need to edit ~/.ipython/kernels/<kernel_name>/kernel.json
and change the JSON key called display_name
to be a name that you like.
3.如果你想改变IPython显示给你的内核的名字,你需要编辑~/.ipython/kernels/<kernel_name>/kernel.json
并改变调用的JSON键display_name
为你喜欢的名字。
4.You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel
and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
4.您现在应该能够在 IPython notebook 菜单中看到您的内核:Kernel -> Change kernel
并且能够切换到它(您可能需要在它出现在列表中之前刷新页面)。从那时起,IPython 将记住该笔记本使用哪个内核。
参考。
回答by Ankur Shrivastav
Here is what I did to enable tensorflow in Anaconda -> Jupyter.
这是我在 Anaconda -> Jupyter 中启用 tensorflow 所做的工作。
- Install Tensorflow using the instructions provided at
- Go to /Users/username/anaconda/env and ensure Tensorflow is installed
- Open the Anaconda navigator and go to "Environments" (located in the left navigation)
- Select "All" in teh first drop down and search for Tensorflow
- If its not enabled, enabled it in the checkbox and confirm the process that follows.
- Now open a new Jupyter notebook and tensorflow should work
- 使用提供的说明安装 Tensorflow
- 转到 /Users/username/anaconda/env 并确保已安装 Tensorflow
- 打开 Anaconda 导航器并转到“环境”(位于左侧导航中)
- 在第一个下拉列表中选择“全部”并搜索 Tensorflow
- 如果未启用,请在复选框中启用它并确认随后的过程。
- 现在打开一个新的 Jupyter notebook,tensorflow 应该可以工作了
回答by Yaroslav Bulatov
Your Anaconda install probably went to different directory than your Python install
您的 Anaconda 安装可能与 Python 安装所在的目录不同
For instance on my machine I can find location here
例如在我的机器上,我可以在这里找到位置
yaroslavvb-macbookpro:~ yaroslavvb$ which ipython
/Users/yaroslavvb/anaconda/bin/ipython
When you type python
, it tries to find it in PATH
going in left-to-right order. So you may have another version of python
in a folder before Anaconda folder, and it'll use that. To fix, you can do export PATH=....
to change the path, and put Anaconda directory in front, so that it takes python
from there instead of the default, ie
当您键入 时python
,它会尝试按PATH
从左到右的顺序查找它。所以你可能python
在 Anaconda 文件夹之前的文件夹中有另一个版本,它会使用它。要修复,您可以export PATH=....
更改路径,并将 Anaconda 目录放在前面,以便python
从那里而不是默认值,即
export PATH=/Users/yaroslavvb/anaconda/bin:$PATH
回答by lucky6qi
I installed PIP with Conda conda install pip
instead of apt-get install python-pip python-dev
.
我用 Condaconda install pip
而不是apt-get install python-pip python-dev
.
Then installed tensorflow use Pip Installation:
然后安装 tensorflow 使用Pip 安装:
# 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
pip install --upgrade $TF_BINARY_URL
Then it will work in jupyter notebook.
然后它将在 jupyter notebook 中工作。
回答by Atlas7
The accepted answer (by Zhongyu Kuang) has just helped me out. Here I've create an environment.yml
file that enables me to make this conda / tensorflow installation process repeatable.
接受的答案(由邝中宇)刚刚帮助了我。在这里,我创建了一个environment.yml
文件,使我能够使这个 conda / tensorflow 安装过程可重复。
Step 1 - create a Conda environment.yml File
步骤 1 - 创建 Conda environment.yml 文件
environment.yml
looks like this:
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
Note:
笔记:
- Simply replace the name to whatever you want. (mine is
hello-tensorflow
) - Simply replace the python version to whatever you want. (mine is
3.6
) - Simply replace the tensorflow pip install URL to whatever you want (mine is the Tensorflow URL where Python 3.6 with GPU support)
- 只需将名称替换为您想要的任何名称。(我的是
hello-tensorflow
) - 只需将 python 版本替换为您想要的任何版本。(我的是
3.6
) - 只需将 tensorflow pip install URL 替换为您想要的任何内容(我的是 Python 3.6 支持 GPU 的 Tensorflow URL)
Step 2 - create the Conda environment
第 2 步 - 创建 Conda 环境
With the environment.yml
being in the current path you are on, this command creates the environment hello-tensorflow
(or whatever you have renamed it to):
在environment.yml
您所在的当前路径中,此命令创建环境hello-tensorflow
(或您已将其重命名为的任何内容):
conda env create -f environment.yml
Step 3: source activate
第 3 步:源激活
Activate the newly created environment:
激活新创建的环境:
source activate hello-tensorflow
Step 4 - which python / jupyter / ipython
第 4 步 - which python / jupyter / ipython
which python...
哪个蟒蛇...
(hello-tensorflow) $ which python
/home/johnny/anaconda3/envs/hello-tensorflow/bin/python
which jupyter...
哪个jupyter...
(hello-tensorflow) $ which jupyter
/home/johnny/anaconda3/envs/hello-tensorflow/bin/jupyter
which ipython...
哪个 ipython...
(hello-tensorflow) $ which ipython
/home/johnny/anaconda3/envs/hello-tensorflow/bin/ipython
Step 5
第 5 步
You should now be able to import tensorflow from python, jupyter (console / qtconsole / notebook, etc.) and ipython.
您现在应该能够从 python、jupyter(控制台/qtconsole/笔记本等)和 ipython 导入 tensorflow。
回答by Yan Zhao
I think your question is very similar with the question post here. Windows 7 jupyter notebook executing tensorflow. As Yaroslav mentioned, you can try
我认为您的问题与此处的问题帖子非常相似。Windows 7 jupyter notebook 执行 tensorflow。正如雅罗斯拉夫提到的,你可以试试
conda install -c http://conda.anaconda.org/jjhelmus tensorflow
.
conda install -c http://conda.anaconda.org/jjhelmus tensorflow
.
回答by Brian
I had a similar issue when using a custom Ubuntu 16
image. The problem was related to an existing version of numpy
that was already installed on my system.
使用自定义Ubuntu 16
图像时我遇到了类似的问题。该问题与numpy
我的系统上已安装的现有版本有关。
I initially tried
我最初尝试
sudo pip3 install tensorflow
This resulted in the following exception:
这导致了以下异常:
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'
例外:回溯(最近一次调用最后一次):文件“/anaconda/envs/py35/lib/python3.5/shutil.py”,第 538 行,移动 os.rename(src, real_dst) PermissionError: [Errno 13] 权限拒绝:'/anaconda/envs/py35/lib/python3.5/site-packages/numpy' -> '/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/麻木'
The docs advise that if you encounter any issues with this command to try the following:
文档建议,如果您遇到此命令的任何问题,请尝试以下操作:
sudo pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl
However, my system was unable to locate pip3
但是,我的系统无法定位 pip3
sudo: pip3 command not found
须藤:找不到 pip3 命令
The ulitmate solution was to create a symlink
for pip3
终极解决方案是symlink
为 pip3创建一个
sudo ln -s /anaconda/envs/py35/bin/pip3.5 /usr/local/bin/pip3
Finally, the following command worked without trouble
最后,以下命令毫无问题地工作
sudo /usr/local/bin/pip3 install --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl
I verified the installation in the terminal
and also verified a successful import in my Jupyter Notebook
我验证了安装terminal
并验证了我的成功导入Jupyter Notebook
import tensorflow as tf
回答by sph
I wonder if it is not enough to simply launch ipython from tensorflow environnement. That is 1) first activate tensorflow virtualenv with:
我想知道仅仅从 tensorflow 环境启动 ipython 是否还不够。即 1) 首先使用以下命令激活 tensorflow virtualenv:
source ~/tensorflow/bin/activate
2) launch ipython under tensorflow environnement
2) 在 tensorflow 环境下启动 ipython
(tensorflow)$ ipython notebook --ip=xxx.xxx.xxx.xxx