关于tensorflow2.0缺少模块的解决办法AttributeError:module tensorflow no attribute ***

装完tensorflow2.0,想跑一些github上的项目,发现会有很多报错,很常见的是:

AttributeError:module tensorflow no attribute ***。

这是因为github上的很多项目是基于tensorflow1.*的,跟最新的tensorflow2.0有不兼容的地方。比如我碰到过:

AttributeError:module tensorflow no attribute app

AttributeError:module tensorflow no attribute contrib

等等。

大部分是因为模块的位置变了,比如AttributeError:module tensorflow no attribute app,

而AttributeError:module tensorflow no attribute contrib是因为tensorflow2.0直接弃用了tf.contrib。

如果直接弃用了,那就没办法,要么自己改代码,要么重装低版本的tensorflow

如果只是模块位置变了,语法不一样了,那就很方便,可以用tensorflow2.0提供的tf_upgrade_v2工具来把低版本的代码直接升级为tensorflow2.0版的,这个工具是在安装tensorflow2.0的时候自动安装的,用法如下:

可以升级单个文件:

tf_upgrade_v2 --infile foo.py --outfile foo-upgraded.py

(tf_upgrade_v2 --infile 1.*版.py文件 --outfile 生成的2.0版.py文件)

也可以把整个项目升级:

# upgrade the .py files and copy all the other files to the outtree
tf_upgrade_v2 --intree coolcode --outtree coolcode-upgraded

# just upgrade the .py files
tf_upgrade_v2 --intree coolcode --outtree coolcode-upgraded --copyotherfiles False

如果碰到不能升级的,会有日志记录,比如那些被弃用的模块就无法升级 。

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/compatibility

关于tensorflow2.0缺少模块的解决办法AttributeError:module tensorflow no attribute ***_第1张图片 多谢打赏 关于tensorflow2.0缺少模块的解决办法AttributeError:module tensorflow no attribute ***_第2张图片 微信交流

 

你可能感兴趣的:(TensorFlow)