TensorFlow,“'module' 对象没有属性 'placeholder'”

TensorFlow, "'module' object has no attribute 'placeholder'"

我已经尝试使用 tensorflow 两天了,现在在 python2.7 和 3.4 中一遍又一遍地安装和重新安装它。无论我做什么,在尝试使用 tensorflow.placeholder()

时都会收到此错误消息

这是非常样板代码:

tf_in = tf.placeholder("float", [None, A]) # Features

无论我做什么,我总能得到回溯:

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'

有人知道我该如何解决这个问题吗?

解决方案:不要使用 "tensorflow" 作为文件名。

请注意,您使用 tensorflow.py 作为文件名。我猜你会写这样的代码:

import tensorflow as tf

那么您实际上是在导入当前工作目录下的脚本文件 "tensorflow.py",而不是 Google.

中的 "real" tensorflow 模块

这是导入时搜索模块的顺序:

  1. The directory containing the input script (or the current directory when no file is specified).

  2. PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  3. The installation-dependent default.

在现有 python 安装上安装张量流时,在 Ubuntu 16LTS 上遇到同样的问题。

解决方法: 1.) 从 pip 和 pip3 中卸载 tensorflow

sudo pip uninstall tensorflow
sudo pip3 uninstall tensorflow

2.)卸载python&python3

sudo apt-get remove python-dev python3-dev python-pip python3-pip

3.)只安装一个版本的python(我用的是python3)

sudo apt-get install python3-dev python3-pip

4.)安装tensorflow到python3

sudo pip3 install --upgrade pip

对于非 GPU tensorflow,运行 此命令

sudo pip3 install --upgrade tensorflow

对于 GPU 张量流,运行 下面的命令

sudo pip3 install --upgrade tensorflow-gpu

建议不要安装GPU和tensorflow的vanilla版本

我也遇到过。我有 tensorflow,它工作得很好,但是当我安装 tensorflow-gpu 与之前的 tensorflow 一起安装时,出现了这个错误,然后我做了这 3 个步骤,它开始正常工作:

  1. 我从 Anaconda 中删除了 tensorflow-gpu、tensorflow、tensorflow-base 包。使用。

conda remove tensorflow-gpu tensorflow tensorflow-base

  1. re-installed张量流。使用

conda install tensorflow

可能是placeholder字写错了。 就我而言,我将其拼错为 placehoder 并得到如下错误: AttributeError: 'module' object has no attribute 'placehoder'

似乎 .placeholder() 、 .reset_default_graph() 和其他内容已从版本 2 中删除。我 运行 使用 Docker 图片解决了这个问题:tensorflow/tensorflow:latest-gpu-py3 自动拉取最新版本。我在 1.13.1 中工作,并且自动 'upgraded to 2' 并开始收到错误消息。我通过更具体的图像来解决这个问题:tensorflow/tensorflow:1.13.1-gpu-py3.

可在此处找到更多信息:https://www.tensorflow.org/alpha/guide/effective_tf2

如果升级到 TensorFlow 2.0 后出现此错误,您仍然可以使用 1.X API 替换为:

import tensorflow as tf

来自

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

我也遇到了同样的错误。可能是因为tensorflow的版本。 安装tensorflow 1.4.0后,我的错误得到了缓解。

pip install tensorflow==1.4.0

如果您使用的是 TensorFlow 2.0,则为 tf 1.x 开发的某些代码可能无法运行。或者你可以关注 link : https://www.tensorflow.org/guide/migrate

或者您可以通过以下方式安装以前版本的 tf pip3 安装 tensorflow==version

因为tensflow2.0版本不能使用placeholder,所以需要使用tensflow1*,或者改代码修复tensflow2.0

我之前遇到过同样的问题,尝试升级tensorflow后,我通过重新安装Tensorflow和Keras解决了。

pip uninstall tensorflow

pip uninstall keras

然后:

pip install tensorflow

pip install keras

如果你在tensorflow 2.0.0+上得到这个,很可能是因为代码与较新版本的tensorflow不兼容。

要解决此问题,运行 tf_upgrade_v2 script

tf_upgrade_v2 --infile=YOUR_SCRIPT.py --outfile=YOUR_SCRIPT.py

最近的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

导入旧版本的tensorflow而不是新版本

[https://inneka.com/ml/tf/tensorflow-module-object-has-no-attribute-placeholder/][1]

导入 tensorflow.compat.v1 作为 tf tf.disable_v2_behavior()

问题出在TensorFlow版本上;你 运行 是 2.01.5 以上的人,而 placeholder 只能与 1.4.

一起使用

所以只需卸载 TensorFlow,然后使用 1.4 版本重新安装它,一切都会正常。

您需要使用带有tensorflow 2的keras模型,如此处

import tensorflow as tf
from tensorflow.python.keras.layers import  Input, Embedding, Dot, Reshape, Dense
from tensorflow.python.keras.models import Model

避免在 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̶)̶

您可以使用以下代码禁用 v2 行为

这个非常适合我。

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)

出现错误是因为我们使用的是 tensorflow 版本 2 而命令来自版本 1。所以如果我们使用:

tf.compat.v1.summary.(method_name)

它会起作用

而不是 tf.placeholder(shape=[None, 2], dtype=tf.float32) 使用类似的东西 tf.compat.v1.placeholder(shape=[None, 2], dtype=tf.float32) 如果您不想完全禁用 v2。

试试这个:

pip install tensorflow==1.14

或者这个(如果你有 GPU):

pip install tensorflow-gpu==1.14
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior() 

有效。 我正在使用 Python 3.7 和 tensorflow 2.0.

请看一下Migrate your TensorFlow 1 code to TensorFlow 2

这些代码:

import tensorflow as tf
tf_in = tf.placeholder("float", [None, A]) # Features

需要在 TensorFlow 2 中迁移如下:

import tensorflow as tf
import tensorflow.compat.v1 as v1
tf_in = vi.placeholder("float", [None, A]) # Features