【解决方法】module ‘tensorflow‘ has no attribute ‘XXX’

Tensorflow1.X迁移到2.0教程


单脚本转换

先进入到你要转换脚本(eg:yolov3.py)的目录,然后:

tf_upgrade_v2 --infile yolov3.py --outfile yolov3.py

文件夹批量转换

先进入你要转换的文件夹根目录(eg:yolov3/),然后可以转换文件下所有py文件:

tf_upgrade_v2 --intree yolov3/ --outtree yolov3/ --copyotherfiles False

注意事项

1.转换好之后可能会报类似于“tf.placeholder() is not compatible with eager execution”这样的错,只需要在正常import tensorflow后面加上这一句:

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

2.tf2.0版本弃用了类似于tf.flags这样的库,可能要重新装absl.flags或切换至 tensorflow/addons 中的软件包来成功迁移代码。

在执行使用了TensorFlow的代码时报错"AttributeError: module 'tensorflow' has no attribute 'GraphKeys'"问题通常是因为在代码中使用了tf1的函数,而当前环境是tf2。解决这个问题的方法有以下几种: 1. 使用tf.compat.v1替代tf来导入TensorFlow的函数和属性。例如,将"import tensorflow as tf"替换为"import tensorflow.compat.v1 as tf",然后在代码的开头添加"tf.compat.v1.disable_v2_behavior()"来禁用tf2的行为。 2. 使用pip install命令安装tf1版本,并在代码中使用tf1的函数。你可以通过运行"pip install tensorflow==1.0"来安装tf1版本,这样就可以在代码中使用tf1的函数和属性。 3. 检查代码中是否有其他导入错误或版本不匹配的问题。有时候,引发"AttributeError: module 'tensorflow' has no attribute 'GraphKeys'"错误的根本原因不是tf1和tf2之间的不匹配,而是其他代码或库的问题。请确保所有导入的库和函数都正确,并且版本兼容。 请根据你的具体环境和需求选择适合的解决方案来解决这个问题。<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_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [AttributeError: moduletensorflowhas no attributeXXX‘的问题如何解决](https://blog.csdn.net/weixin_43214528/article/details/114294154)[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_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [AttributeError: moduletensorflowhas no attribute ‘GraphKeys](https://blog.csdn.net/qq_39004379/article/details/119559357)[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_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值