解决AttributeError: module ‘tensorflow._api.v2.train’ has no attribute ‘NewCheckpointReader’


问题描述:

TensorFlow版本是2.8.0,执行如下代码:

reader = tf.train.NewCheckpointReader(filename)

报错如下:

AttributeError: module 'tensorflow._api.v2.train' has no attribute 'NewCheckpointReader'

原因分析:

TensorFlow 2.0以上的版本已经移除 tf.train.NewCheckpointReader 了。


解决方案:

tf.compat.v1.train.NewCheckpointReader 替换 tf.train.NewCheckpointReader 即可。

# reader = tf.train.NewCheckpointReader(filename)
reader = tf.compat.v1.train.NewCheckpointReader(filename)

tf.compat.v1.train.NewCheckpointReader

如果代码多处因版本问题出现报错,可改换低版本的TensorFlow,用pip安装即可。

pip uninstall tensorflow
pip install tensorflow==1.14
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐