Python TensorFlow,“‘模块’对象没有‘占位符’属性”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37383812/
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
TensorFlow, "'module' object has no attribute 'placeholder'"
提问by user3023715
I've been trying to use tensorflow for two days now installing and reinstalling it over and over again in python2.7 and 3.4. No matter what I do, I get this error message when trying to use tensorflow.placeholder()
我已经尝试使用 tensorflow 两天了,现在在 python2.7 和 3.4 中一遍又一遍地安装和重新安装它。无论我做什么,在尝试使用 tensorflow.placeholder() 时都会收到此错误消息
It's very boilerplate code:
这是非常样板代码:
tf_in = tf.placeholder("float", [None, A]) # Features
No matter what I do I always get the trace back:
无论我做什么,我总能得到回溯:
Traceback (most recent call last):
File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 2, in <module>
import tensorflow as tf
File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 53, in <module>
tf_in = tf.placeholder("float", [None, A]) # Features
AttributeError: 'module' object has no attribute 'placeholder'
Anyone know how I can fix this?
有谁知道我该如何解决这个问题?
采纳答案by farnaz jazayeri
It happened to me too. I had tensorflow and it was working pretty well, but when I install tensorflow-gpu along side the previous tensorflow this error arose then I did these 3 steps and it started working with no problem:
它也发生在我身上。我有 tensorflow 并且它运行得很好,但是当我在之前的 tensorflow 旁边安装 tensorflow-gpu 时,出现了这个错误,然后我执行了以下 3 个步骤,它开始正常工作:
- I removed tensorflow-gpu, tensorflow, tensorflow-base packages from Anaconda. Using.
- 我从 Anaconda 中删除了 tensorflow-gpu、tensorflow、tensorflow-base 包。使用。
conda remove tensorflow-gpu tensorflow tensorflow-base
conda remove tensorflow-gpu tensorflow tensorflow-base
- re-installed tensorflow. Using
- 重新安装张量流。使用
conda install tensorflow
conda install tensorflow
回答by u2gilles
If you have this error after an upgrade to TensorFlow 2.0, you can still use 1.X API by replacing:
如果您在升级到 TensorFlow 2.0 后出现此错误,您仍然可以通过替换以下内容来使用 1.X API:
import tensorflow as tf
by
经过
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
回答by Lifu Huang
Solution: Do not use "tensorflow" as your filename.
解决方案:不要使用“tensorflow”作为文件名。
Notice that you use tensorflow.py as your filename. And I guess you write code like:
请注意,您使用 tensorflow.py 作为文件名。我猜你写的代码如下:
import tensorflow as tf
Then you are actually importing the script file "tensorflow.py" that is under your current working directory, rather than the "real" tensorflow module from Google.
然后,您实际上是在导入当前工作目录下的脚本文件“tensorflow.py”,而不是来自 Google 的“真实”tensorflow 模块。
Here is the order in which a module will be searched when importing:
以下是导入时搜索模块的顺序:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default.
包含输入脚本的目录(或未指定文件时的当前目录)。
PYTHONPATH(目录名称列表,与 shell 变量 PATH 具有相同的语法)。
依赖于安装的默认值。
回答by Adrian Hood Sr
It appears that .placeholder() , .reset_default_graph() , and others were removed with version 2. I ran into this issue using Docker image: tensorflow/tensorflow:latest-gpu-py3
which automatically pulls the latest version. I was working in 1.13.1 and was 'upgraded to 2' automatically and started getting the error messages. I fixed this by being more specific with my image: tensorflow/tensorflow:1.13.1-gpu-py3
.
似乎 .placeholder() 、 .reset_default_graph() 和其他版本已在版本 2 中删除。我使用 Docker 镜像遇到了这个问题:tensorflow/tensorflow:latest-gpu-py3
它会自动拉取最新版本。我在 1.13.1 中工作并自动“升级到 2”并开始收到错误消息。我解决了这个问题通过更具体的与我的形象:tensorflow/tensorflow:1.13.1-gpu-py3
。
More info can be found here: https://www.tensorflow.org/alpha/guide/effective_tf2
更多信息可以在这里找到:https: //www.tensorflow.org/alpha/guide/effective_tf2
回答by Pradeep
Import the old version of tensorflow instead of the new version
导入旧版本的tensorflow而不是新版本
[https://inneka.com/ml/tf/tensorflow-module-object-has-no-attribute-placeholder/][1]
[ https://inneka.com/ml/tf/tensorflow-module-object-has-no-attribute-placeholder/][1]
import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
将 tensorflow.compat.v1 导入为 tf tf.disable_v2_behavior()
回答by mahbubcseju
I also got the same error. May be because of the version of tensorflow. After installing tensorflow 1.4.0, I got relief from the error.
我也遇到了同样的错误。可能是因为tensorflow的版本。安装 tensorflow 1.4.0 后,我从错误中解脱出来。
pip install tensorflow==1.4.0
回答by Shaina Raza
You need to use the keras model with tensorflow 2, as here
您需要将 keras 模型与 tensorflow 2 一起使用,如下所示
import tensorflow as tf
from tensorflow.python.keras.layers import Input, Embedding, Dot, Reshape, Dense
from tensorflow.python.keras.models import Model
回答by Ramesh Hawking
Avoid using the below striked out statement in tensorflow=2.0
避免在 tensorflow=2.0 中使用以下删除语句
i?m?p?o?r?t? ?t?e?n?s?o?r?f?l?o?w? ?a?s? ?t?f? ?x? ?=? ?t?f?.?p?l?a?c?e?h?o?l?d?e?r?(?s?h?a?p?e?=?[?N?o?n?e?,? ?2?]?,? ?d?t?y?p?e?=?t?f?.?f?l?o?a?t?3?2?)?
进口??t?e?n?s?o?r?f?l?o?w? ?作为??t?f? ?X??=? ?t?f?.?p?l?a?c?e?h?o?l?d?e?r?(?s?h?a?p?e?=?[?N?o?n ?e?,??2?]?,??d?t?y?p?e?=?t?f?.?f?l?o?a?t?3?2?)?
You can disable the v2 behavior by using the following code
您可以使用以下代码禁用 v2 行为
This one is perfectly working for me.
这个非常适合我。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)
回答by Nawin K Sharma
Recent version 2.0 does not support placeholder.
I uninstalled 2.0 using command: conda remove tensorflow
.
then I installed 1.15.0 using command: conda install -c conda-forge tensorflow=1.15.0
.
1.15 is latest in version 1 series. You can change as per you wish and requirement.
For seeing all version, use command: conda search tensorflow
.
It worked for Anaconda3 in Windows.
最近的 2.0 版不支持占位符。我使用命令卸载了 2.0:conda remove tensorflow
. 然后我用命令安装1.15.0: conda install -c conda-forge tensorflow=1.15.0
。1.15 是版本 1 系列的最新版本。您可以根据自己的意愿和要求进行更改。要查看所有版本,请使用命令:conda search tensorflow
。它适用于 Windows 中的 Anaconda3。
回答by Niharranjan Pradhan
If you are using TensorFlow 2.0, then some code developed for tf 1.x may code work. Either you can follow the link : https://www.tensorflow.org/guide/migrate
如果您使用的是 TensorFlow 2.0,那么一些为 tf 1.x 开发的代码可能会起作用。您可以点击链接:https: //www.tensorflow.org/guide/migrate
or you can install a previous version of tf by pip3 install tensorflow==version
或者您可以通过 pip3 install tensorflow==version 安装以前版本的 tf