如何在 python 中导入 Tensorflow 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45623403/
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
How to import the Tensorflow libraries in python?
提问by Doubt Dhanabalu
A very basic question.
一个非常基本的问题。
I am trying to install tensorflow library in Anaconda python(spyder).
我正在尝试在 Anaconda python(spyder) 中安装 tensorflow 库。
import tf.contrib.keras.preprocessing
Its is giving me error as "No module found".
I also tried import tensorflow.contrib.keras.preprocessing
它给了我错误,因为"No module found".
我也尝试了 import tensorflow.contrib.keras.preprocessing
I also tried
from tf.contrib.keras.preprocessing.text import Tokenizer
.
我也试过了
from tf.contrib.keras.preprocessing.text import Tokenizer
。
This also doesnt work
这也不起作用
However I verified this in the tensorflow website, and it is present. The link to the library is https://www.tensorflow.org/api_docs/python/tf/contrib/keras/preprocessing.
但是我在 tensorflow 网站上验证了这一点,并且它存在。该库的链接是https://www.tensorflow.org/api_docs/python/tf/contrib/keras/preprocessing。
I tried to pip and conda install. But that also throwing out error.
我尝试 pip 和 conda 安装。但这也会抛出错误。
From anaconda prompt i typed this:
在 anaconda 提示符下,我输入了以下内容:
activate tensorflow
pip install tf.contrib.keras.preprocessing
conda install tf.contrib.keras.preprocessing
Is there anything i miss out, Please correct me.
有什么我遗漏的地方,请纠正我。
回答by Y0da
You are doing it wrong as tf
is not the name of the tensorflow
module but an alias in the tutorials.
您做错了,因为tf
不是tensorflow
模块的名称,而是教程中的别名。
import tensorflow as tf
Thus try this:
因此试试这个:
from tensorflow.contrib.keras.preprocessing.text import Tokenizer
From your comments it seems that the module might not be installed so you can check in the list of installed packages:
从您的评论看来,该模块可能未安装,因此您可以查看已安装的软件包列表:
conda list
If not present, install it with pip
. Follow the doc.
如果不存在,请使用pip
. 按照文档。
回答by elf
If you want to do it through Anaconda rather than pip (pip3 install --upgrade tensorflow
):
如果你想通过 Anaconda 而不是 pip ( pip3 install --upgrade tensorflow
)来做到这一点:
Create a conda environment called
tensorflow
:C:> conda create -n tensorflow python=3.5
Activate the environment:
C:> activate tensorflow
Install tensorflow into your environment:
(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.2.1-cp35-cp35m-win_amd64.whl
(CPU) or(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl
(GPU)
创建一个名为的 conda 环境
tensorflow
:C:> conda create -n tensorflow python=3.5
激活环境:
C:> activate tensorflow
将 tensorflow 安装到您的环境中:
(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.2.1-cp35-cp35m-win_amd64.whl
(CPU)或(tensorflow)C:> pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl
(GPU)