ModuleNotFoundError: No module named ‘tensorflow.compat’ 表示Python无法找到名为 tensorflow.compat 的模块。这通常发生在尝试使用TensorFlow库的兼容性模块时,但环境中没有安装或正确配置该模块。
我使用的是tensorflow 2.7.0,tensorflow.compat 模块是TensorFlow 1.x与TensorFlow 2.x之间的兼容性层。如果你的代码是为TensorFlow 1.x编写的,你可以通过以下方式在TensorFlow 2.x中启用兼容模式:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
再次运行,大功告成!