关于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

待办计划:在不确定中前行
随笔日记:祭那些逝去的时光
  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值