首页 > 解决方案 > 为什么占位符在张量流中不起作用?

问题描述

当我尝试使用 tf.placeholder() 运行一个简单的语句时,我得到以下结果。

X=tf.placeholder(tf.float32,shape=(None,height,width,channels))
Traceback (most recent call last):

  File "<ipython-input-6-6f535655ed45>", line 1, in <module>
    X=tf.placeholder(tf.float32,shape=(None,height,width,channels))

AttributeError: module 'tensorflow' has no attribute 'placeholder'

在做了一些阅读之后,这似乎与拥有 tensorflow 的第 2 版而不是早期版本有关。但我检查了一下,看来我有 1.14。

pip list
Package                            Version            
---------------------------------- -------------------
tensorboard                        1.14.0             
tensorboard-plugin-wit             1.6.0.post3        
tensorflow                         1.14.0             
tensorflow-estimator               1.14.0 

我对此很陌生,并试图按照书中的教程进行操作,不胜感激。对于上下文,我试图在 Spyder 中运行它作为 Python 3.7 的 Anaconda 发行版的一部分。

编辑:我忘了包括我尝试使用某些版本的 tf.compat.placeholder() 以及。

X = tf.compat.v1.placeholder(tf.float32,shape=(None,height,width,channels))
Traceback (most recent call last):

  File "<ipython-input-7-d9a798a5972c>", line 1, in <module>
    X = tf.compat.v1.placeholder(tf.float32,shape=(None,height,width,channels))

AttributeError: module 'tensorflow' has no attribute 'compat'

编辑 2:经过更多测试,代码将在 Jupyter 中运行,但不在 Spyder 中,所以我不确定这意味着什么。

标签: pythontensorflow

解决方案


您可以尝试X = tf.compat.v1.placeholder按照文档中的建议使用(请参见此处


推荐阅读