【DL-TensorFlow遇错】TensorFlow中遇错合集

TensorFlow中遇错合集

  • [一、`AttributeError: module 'tensorflow' has no attribute 'placeholder'`](#一、AttributeError: module 'tensorflow' has no attribute 'placeholder')
  • [二、`RuntimeError: tf.placeholder() is not compatible with eager execution.`](#二、RuntimeError: tf.placeholder() is not compatible with eager execution.)

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

错误原因

  • tensorflow版本问题:当前tensorflow版本为2.X,而tensorflow 2.0版本去掉了placeholder。tensorflow 1.*版本才有placeholder。

解决方案

  • "向后兼容"。这种做法可以在新版本的TensorFlow中仍然使用旧的API,确保旧代码的兼容性。

具体实现

  • 修改import tensorflow as tfimport tensorflow._api.v2.compat.v1 as tf

二、RuntimeError: tf.placeholder() is not compatible with eager execution.

错误原因

  • tf.placeholder()意味着被提供给会话,该会话在运行时从feed dict接收值并执行所需的操作。(也就是默认为急切运行,我们爆出这个错误说明不需要急切运行

解决方案

方式一:

  • tf.placeholder()被调用前添加tf.compat.v1.disable_eager_execution()

方式二:

bash 复制代码
import tensorflow._api.v2.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
相关推荐
巫山老妖10 小时前
置身AI内
人工智能
IT_陈寒12 小时前
JavaScript项目实战经验分享
前端·人工智能·后端
vanuan13 小时前
两个AI智能体第一次对话-A2A双Agent协作实战
人工智能
Warson_L14 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅14 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅14 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L14 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅15 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
kfaino15 小时前
码农的AI翻身(四)你好,我叫 Attention
人工智能·后端
Warson_L15 小时前
python的类&继承
python