AttributeError: module ‘tensorflow‘ has no attribute ‘placeholder‘解决办法

在这里插入图片描述

1.报错代码

self.inputs_base_structure_left = tf.placeholder(dtype=tf.float32, shape=[None, 2048, 2], name="inputs_left")  # initial a inputs to siamese_network

2. 报错原因

AttributeError: module 'tensorflow' has no attribute 'placeholder' 这个错误发生的原因是因为在较新版本的 TensorFlow 中,tf.placeholder 已经被弃用,取而代之的是使用 tf.compat.v1.placeholder 或者直接使用 TensorFlow 2.x 中的新特性,比如 tf.functiontf.Tensor 来定义输入。

TensorFlow 2.x 相较于 1.x 版本有着显著的变化,其中最重要的是引入了即时执行(Eager Execution)和函数式编程模型,这使得 TensorFlow 的使用变得更加直观和Pythonic。在 TensorFlow 2.x 中,许多 TensorFlow 1.x 的概念(比如图计算、会话等)都被简化或重新设计了。

3. 解决办法

  1. 使用 TensorFlow 1.x 兼容模式
    在 TensorFlow 2.x 中,你可以通过启用 TensorFlow 1.x 兼容模式来使用 tf.placeholder。在代码的开始处添加以下行:

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

    然后,你可以使用 tf.compat.v1.placeholder 来创建占位符:

    x = tf.compat.v1.placeholder(tf.float32, shape=[None, 784])
    y = tf.compat.v1.placeholder(tf.float32, shape=[None, 10])
    

    注意,在 TensorFlow 2.x 中使用兼容模式可能会限制你使用 TensorFlow 2.x 的新特性。

  2. 使用 TensorFlow 1.x 版本:(我的方法
    如果你不打算将代码迁移到 TensorFlow 2.x,你可以考虑安装并使用 TensorFlow 1.x 版本。但请注意,TensorFlow 1.x 不再得到官方的积极支持,且存在安全隐患。

我开始使用tf.compat.v1.placeholder后来又出现了其他问题,我直接弃用了这种方法,然后安装了tensorflow1.15版本,直接完美运行代码

  • 25
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Tensorflow版本的升级过程中,有些方法的名称发生了变化,导致在使用旧版本的代码时出现了AttributeError: module 'tensorflow' has no attribute 'placeholder'错误。 解决这个问题的方法是根据使用的Tensorflow版本来选择正确的方法。如果你正在使用Tensorflow 2.x及以上版本,那么placeholder方法已经被移除了,应该使用tf.Variable来代替。 如果你的代码是在Tensorflow 1.x版本中编写的,而你正在使用Tensorflow 2.x及以上版本,则需要修改你的代码,将placeholder替换为tf.Variable,以兼容新版本的Tensorflow。 另外一种解决办法是降低Tensorflow的版本至1.x版本,这样你的代码就能够正常运行了。 总之,要解决AttributeError: module 'tensorflow' has no attribute 'placeholder'错误,只需要根据你使用的Tensorflow版本来选择正确的方法即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [程序报错:AttributeError: module 'tensorflow' has no attribute 'xxx' 解决办法](https://blog.csdn.net/qq_41320433/article/details/104198059)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值