Python 在 Jupyter Notebook 中运行 Tensorflow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43216256/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:39:13  来源:igfitidea点击:

Running Tensorflow in Jupyter Notebook

pythontensorflowjupyter-notebookkeras

提问by Baktaawar

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

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

Here is what I did.

这是我所做的。

In Anaconda, I created an environment called tensorflowas follows

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

conda create -n tensorflow

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

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

(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$

You can see that jupyteris also installed.

可以看到jupyter也安装了。

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

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

mkdir dl

In that, I activated tensorflowenvironment

在那,我激活了tensorflow环境

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

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

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

Now, I open up a Jupyter notebook

现在,我打开一个 Jupyter notebook

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

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.

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

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

ModuleNotFoundError: No module named 'pandas'

Why jupyter notebook is not picking up these modules?

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

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

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

enter image description here

在此处输入图片说明

回答by Shamane Siriwardhana

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

我想出了你的情况。这就是我整理的方式

  1. Install Anaconda
  2. Create a virtual environment - conda create -n tensor flow
  3. Go inside your virtual environment - Source activate tensorflow
  4. Inside that install tensorflow. You can install it using pip
  5. Finish install
  1. 安装蟒蛇
  2. 创建虚拟环境—— conda create -n tensor flow
  3. 进入你的虚拟环境—— Source activate tensorflow
  4. 在里面安装tensorflow。您可以使用安装它pip
  5. 完成安装

So then the next thing, when you launch it:

那么接下来,当你启动它时:

  1. If you are not inside the virtual environment type - Source Activate Tensorflow
  2. Then inside this again install your Jupiter notebook and Pandas libraries, because there can be some missing in this virtual environment
  1. 如果您不在虚拟环境类型中 - Source Activate Tensorflow
  2. 然后在里面再次安装你的 Jupiter notebook 和 Pandas 库,因为在这个虚拟环境中可能会缺少一些

Inside the virtual environment just type:

在虚拟环境中输入:

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

Then you can launch jupyter notebook saying:

然后你可以启动 jupyter notebook 说:

  1. jupyter notebook
  2. Select the correct terminal python 3 or 2
  3. Then import those modules
  1. jupyter notebook
  2. 选择正确的终端python 3或2
  3. 然后导入这些模块

回答by rsc05

I believe a short video showing all the details if you have Anaconda is the following for mac (it is very similar to windows users as well) just open Anaconda navigator and everything is just the same (almost!)

如果你有 Anaconda,我相信一个简短的视频显示了所有细节,如果你有 Anaconda 是以下 Mac(它也与 Windows 用户非常相似)只需打开 Anaconda 导航器,一切都一样(几乎!)

https://www.youtube.com/watch?v=gDzAm25CORk

https://www.youtube.com/watch?v=gDzAm25CORk

Then go to jupyter notebook and code

然后去 jupyter notebook 和 code

!pip install tensorflow

Then

然后

import tensorflow as tf

It work for me! :)

它对我有用!:)

回答by Swati Rathore

  1. Install Anaconda
  2. Run Anaconda command prompt
  3. write "activate tensorflow" for windows
  4. pip install tensorflow
  5. pip install jupyter notebook
  6. jupyter notebook.
  1. 安装蟒蛇
  2. 运行 Anaconda 命令提示符
  3. 为 Windows 编写“激活 tensorflow”
  4. pip 安装张量流
  5. pip 安装 jupyter 笔记本
  6. jupyter 笔记本。

Only this solution worked for me. Tried 7 8 solutions. Using Windows platform.

只有这个解决方案对我有用。尝试了 7 8 个解决方案。使用Windows平台。

回答by sushil gupta

  1. install tensorflow by running these commands in anoconda shell or in console:

    conda create -n tensorflow python=3.5
    activate tensorflow
    conda install pandas matplotlib jupyter notebook scipy scikit-learn
    pip install tensorflow
    
  2. close the console and reopen it and type these commands:

    activate tensorflow 
    jupyter notebook 
    
  1. 通过在 anoconda shell 或控制台中运行这些命令来安装 tensorflow:

    conda create -n tensorflow python=3.5
    activate tensorflow
    conda install pandas matplotlib jupyter notebook scipy scikit-learn
    pip install tensorflow
    
  2. 关闭控制台并重新打开它并键入以下命令:

    activate tensorflow 
    jupyter notebook 
    

回答by Utkarsh Mankad

I have found a fairly simple way to do this.

我找到了一个相当简单的方法来做到这一点。

Initially, through your Anaconda Prompt, you can follow the steps in this official Tensorflow site - here. You have to follow the steps as is, no deviation.

最初,通过您的 Anaconda Prompt,您可以按照此官方 Tensorflow 站点中的步骤进行操作 -此处。您必须按原样执行步骤,不得有任何偏差。

Later, you open the Anaconda Navigator. In Anaconda Navigator, go to Applications On --- section. Select the drop down list, after following above steps you must see an entry - tensorflow into it. Select tensorflow and let the environment load.

稍后,您打开 Anaconda Navigator。在 Anaconda Navigator 中,转到 Applications On --- 部分。选择下拉列表,按照上述步骤操作后,您必须看到一个条目 - tensorflow 进入其中。选择 tensorflow 并让环境加载。

Then, select Jupyter Notebook in this new context, and install it, let the installation get over.

然后,在这个新上下文中选择 Jupyter Notebook,并安装它,让安装结束。

After that you can run the Jupyter notebook like the regular notebook in tensorflow environment.

之后,您可以像在 tensorflow 环境中运行普通笔记本一样运行 Jupyter 笔记本。

回答by Statistic Dean

I would suggest launching Jupyter lab/notebook from your base environment and selecting the right kernel.

我建议从您的基本环境启动 Jupyter 实验室/笔记本并选择正确的内核。

How to add conda environment to jupyter labshould contains the info needed to add the kernel to your base environment.

如何将 conda 环境添加到 jupyter 实验室应包含将内核添加到基本环境所需的信息。

Disclaimer : I asked the question in the topic I linked, but I feel it answers your problem too.

免责声明:我在我链接的主题中提出了这个问题,但我觉得它也能回答你的问题。

回答by Fatemeh

Although it's a long time after this question is being asked since I was searching so much for the same problem and couldn't find the extant solutions helpful, I write what fixed my trouble for anyone with the same issue: The point is, Jupyter should be installed in your virtual environment, meaning, after activating the tensorflowenvironment, run the following in the command prompt (in tensorflowvirtual environment):

虽然在问这个问题之后很长一段时间,因为我搜索了很多相同的问题并且找不到有用的现有解决方案,但我写了为遇到相同问题的人解决了我的问题的内容:重点是,Jupyter 应该安装在您的虚拟环境中,这意味着在激活tensorflow环境后,在命令提示符下运行以下命令(tensorflow虚拟环境中):

conda install jupyter
jupyter notebook

and then the jupyter will pop up.

然后jupyter会弹出。

回答by Armin

It is better to create new environment with new name ($newenv):conda create -n $newenv tensorflow

最好用新名称($newenv)创建新环境:conda create -n $newenv tensorflow

Then by using anaconda navigator under environment tab you can find newenv in the middle column.

然后通过在环境选项卡下使用 anaconda 导航器,您可以在中间列中找到 newenv。

By clicking on the play button open terminal and type: activate tensorflow

通过单击播放按钮打开终端并键入: activate tensorflow

Then install tensorflow inside the newenv by typing: pip install tensorflow

然后通过键入以下内容在 newenv 中安装 tensorflow: pip install tensorflow

Now you have tensorflow inside the new environment so then install jupyter by typing: pip install jupyter notebook

现在你在新环境中有了 tensorflow,然后输入以下命令安装 jupyter: pip install jupyter notebook

Then just simply type: jupyter notebookto run the jupyter notebook.

然后只需简单地输入:jupyter notebook运行 jupyter notebook。

Inside of the jupyter notebook type: import tensorflow as tf

jupyter 笔记本类型的内部: import tensorflow as tf

To test the the tf you can use THIS LINK

要测试 tf,您可以使用此链接

回答by Vivek

For Anaconda users in Windows 10 and those who recently updated Anaconda environment, TensorFlow may cause some issues to activate or initiate. Here is the solution which I explored and which worked for me:

对于 Windows 10 中的 Anaconda 用户以及最近更新 Anaconda 环境的用户,TensorFlow 可能会导致一些问题无法激活或启动。这是我探索过的并且对我有用的解决方案:

  • Uninstall current Anaconda environment and delete all the existing files associated with Anaconda from your C:\Users or where ever you installed it.
  • Download Anaconda (https://www.anaconda.com/download/?lang=en-us#windows)
  • While installing, check the "Add Anaconda to my PATH environment variable"
  • After installing, open the Anaconda command prompt to install TensorFlow using these steps:
  • Create a conda environment named tensorflow by invoking the following command:

    conda create -n tensorflow python=3.5 (Use this command even if you are using python 3.6 because TensorFlow will get upgraded in the following steps)

  • Activate the conda environment by issuing the following command:

    activate tensorflow After this step, the command prompt will change to (tensorflow)

  • After activating, upgrade tensorflow using this command:

    pip install --ignore-installed --upgrade Now you have successfully installed the CPU version of TensorFlow.

  • Close the Anaconda command prompt and open it again and activate the tensorflow environment using 'activate tensorflow' command.
  • Inside the tensorflow environment, install the following libraries using the commands: pip install jupyter pip install keras pip install pandas pip install pandas-datareader pip install matplotlib pip install scipy pip install sklearn
  • Now your tensorflow environment contains all the common libraries used in deep learning.
  • Congrats, these libraries will make you ready to build deep neural nets. If you need more libraries install using the same command 'pip install libraryname'
  • 卸载当前的 Anaconda 环境并从 C:\Users 或安装它的任何位置删除与 Anaconda 关联的所有现有文件。
  • 下载 Anaconda ( https://www.anaconda.com/download/?lang=en-us#windows)
  • 安装时,选中“将 Anaconda 添加到我的 PATH 环境变量”
  • 安装后,打开 Anaconda 命令提示符以使用以下步骤安装 TensorFlow:
  • 通过调用以下命令创建一个名为 tensorflow 的 conda 环境:

    conda create -n tensorflow python=3.5(即使您使用的是 python 3.6,也请使用此命令,因为 TensorFlow 将在以下步骤中升级)

  • 通过发出以下命令激活 conda 环境:

    激活 tensorflow 这一步后,命令提示符会变成(tensorflow)

  • 激活后,使用以下命令升级 tensorflow:

    pip install --ignore-installed --upgrade 现在你已经成功安装了 TensorFlow 的 CPU 版本。

  • 关闭 Anaconda 命令提示符并再次打开它并使用“activate tensorflow”命令激活 tensorflow 环境。
  • 在 tensorflow 环境中,使用以下命令安装以下库: pip install jupyter pip install keras pip install pandas pip install pandas-datareader pip install matplotlib pip install scipy pip install sklearn
  • 现在,您的 tensorflow 环境包含深度学习中使用的所有常用库。
  • 恭喜,这些库将使您准备好构建深度神经网络。如果您需要使用相同的命令“pip install libraryname”安装更多库

回答by Alexander Moiseev

You will need to add a "kernel" for it. Run your enviroment:

您需要为它添加一个“内核”。运行你的环境:

>activate tensorflow

Then add a kernel by command (after --name should follow your env. with tensorflow):

然后通过命令添加内核(在 --name 之后应该跟随你的 env. with tensorflow):

>python -m ipykernel install --user --name tensorflow --display-name "TensorFlow-GPU"

After that run jupyter notebook from your tensorflow env.

之后从你的 tensorflow 环境运行 jupyter notebook。

>jupyter notebook

And then you will see the following enter image description here

然后你会看到下面的 输入图片描述here

Click on it and then in the notebook import packages. It will work out for sure.

单击它,然后在笔记本中导入包。它肯定会奏效。