在tensorflow2运行代码出现此问题。
module ‘tensorflow’ has no attribute 'XXX’一般是tensorflow2和tensorflow1 不兼容问题。
问题解决办法:在tf后面加.compat.v1
事例1(module ‘tensorflow’ has no attribute ‘GraphDef’):
tensorflow1下:
tf.GraphDef()
tensorflow2下:
tf.compat.v1.GraphDef()
事例2(module ‘tensorflow’ has no attribute ‘Session’):
tensorflow1下:
tf.Session(graph=detection_graph)
tensorflow2下:
tf.compat.v1.Session(graph=detection_graph)
当在TensorFlow 2中运行代码遇到`module‘tensorflow’hasnoattribute‘XXX’`错误时,通常是由于TensorFlow 1和2之间的API差异导致的。解决方法是在引用不兼容的API时加上`tf.compat.v1`前缀,例如`tf.GraphDef()`改为`tf.compat.v1.GraphDef()`,`tf.Session()`改为`tf.compat.v1.Session()`。确保迁移代码时适配TensorFlow 2的API。

3675

被折叠的 条评论
为什么被折叠?



