如何解决AttributeError: 模块“tensorflow”没有属性“contrib”
我正在使用在 Tensorflow1 中实现的其他代码 (Code Link)。我想将此代码运行到 Tensorflow2 但是我收到此错误:
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
AttributeError: module 'tensorflow' has no attribute 'contrib'
我使用以下说明升级了此代码:
!tf_upgrade_v2 \
--infile /research/dept8/gds/anafees/MyTest.py \
--outfile /research/dept8/gds/anafees/MyTest2.py
大部分内容都已更新,但生成的报告显示:
168:21: ERROR: Using member tf.contrib.distribute.MirroredStrategy in deprecated module tf.contrib. tf.contrib.distribute.MirroredStrategy cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0,please consider an alternative in non-contrib TensorFlow,a community-maintained repository such as tensorflow/addons,or fork the required code.
我搜索谷歌;但是,我找不到任何合适的解决方案。我不想回到 Tensorflow1。有没有替代解决方案?有人可以帮忙吗?
解决方法
很少有库在 Tensorflow 2.x 中被弃用,例如 tf.contrib
,为了使代码与 Tf 2 兼容,需要对库进行一些更改。
替换
tf.contrib.learn.datasets.load_dataset("mnist")
与
tf.keras.datasets.mnist.load_data()
并替换
tf.contrib.distribute.MirroredStrategy
与
tf.distribute.MirroredStrategy(devices=None,cross_device_ops=None)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。