1.报错的部分
Traceback (most recent call last):
File "experiment.py", line 2, in <module>
import d4rl
File "/d4rl/d4rl/__init__.py", line 39, in <module>
import d4rl.carla
File "/d4rl/d4rl/carla/__init__.py", line 1, in <module>
from .carla_env import CarlaObsDictEnv
File "/d4rl/d4rl/carla/carla_env.py", line 46, in <module>
from agents.navigation.agent import Agent, AgentState
File "/opt/conda/lib/python3.8/site-packages/agents/__init__.py", line 22, in <module>
from . import scripts
File "/opt/conda/lib/python3.8/site-packages/agents/scripts/__init__.py", line 21, in <module>
from . import train
File "/opt/conda/lib/python3.8/site-packages/agents/scripts/train.py", line 33, in <module>
from agents.scripts import configs
File "/opt/conda/lib/python3.8/site-packages/agents/scripts/configs.py", line 26, in <module>
from agents.scripts import networks
File "/opt/conda/lib/python3.8/site-packages/agents/scripts/networks.py", line 30, in <module>
tfd = tf.contrib.distributions
AttributeError: module 'tensorflow' has no attribute 'contrib'
2.问题的分析
搜索到 module 'tensorflow' has no attribute 'contrib'发现是tensorflow的兼容性的问题。
通过报错可以发现是python的agents库的问题
3.了解问题并解决
打开agents的官方网站并下载源码
下载源码之后利用IDE打开进行分析,利用报错的信息发现主要是tf.contrib的问题
4.解决方案
由于是在docker里面的,无法直接用IDE进行修改,vim打开源代码进行修改
4.1tensorflow官方的方案Automatically rewrite TF 1.x and compat.v1 API symbols | TensorFlow Core (google.cn)
Migrate to TensorFlow 2 | TensorFlow Core (google.cn)
tf_upgrade_v2 \ --intree my_project/ \ --outtree my_project_v2/ \ --reportfile report.txt
example
tf_upgrade_v2 --infile networks.py --outfile networks.py
这里会提供一个report,需要根据report进一步修改
4.2report
ERROR line 30:6: Using member tf.contrib.distributions in deprecated module tf.contrib. tf.contrib.distributions 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.
INFO line 54:7: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name.
INFO line 54:7: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope'
INFO line 57:14: Renamed 'tf.log' to 'tf.math.log'
INFO line 58:14: Renamed 'tf.log' to 'tf.math.log'
INFO line 61:8: Added keywords to args of function 'tf.reduce_sum'
INFO line 62:8: Added keywords to args of function 'tf.reduce_sum'
INFO line 63:8: Added keywords to args of function 'tf.reduce_sum'
INFO line 63:39: Added keywords to args of function 'tf.reduce_sum'
INFO line 92:24: Changing tf.contrib.layers.variance_scaling_initializer to a tf.compat.v1.keras.initializers.VarianceScaling and converting arguments.
INFO line 94:36: tf.constant_initializer requires manual check. Initializers no longer have the dtype argument in the constructor or partition_info argument in the __call__ method.
The calls have been converted to compat.v1 for safety (even though they may already have been correct).
INFO line 94:36: Renamed 'tf.constant_initializer' to 'tf.compat.v1.constant_initializer'
INFO line 97:6: Added keywords to args of function 'tf.shape'
INFO line 97:33: Added keywords to args of function 'tf.shape'
INFO line 99:7: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
ERROR line 102:10: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
ERROR line 103:11: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
WARNING line 106:25: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
INFO line 106:25: Renamed 'tf.get_variable' to 'tf.compat.v1.get_variable'
INFO line 111:9: Added keywords to args of function 'tf.shape'
INFO line 111:28: Added keywords to args of function 'tf.shape'
INFO line 112:7: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
ERROR line 115:10: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
ERROR line 116:12: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
INFO line 117:9: Renamed 'tf.check_numerics' to 'tf.debugging.check_numerics'
INFO line 118:8: Renamed 'tf.check_numerics' to 'tf.debugging.check_numerics'
INFO line 119:10: Renamed 'tf.check_numerics' to 'tf.debugging.check_numerics'
INFO line 144:24: Changing tf.contrib.layers.variance_scaling_initializer to a tf.compat.v1.keras.initializers.VarianceScaling and converting arguments.
INFO line 149:6: Added keywords to args of function 'tf.shape'
INFO line 149:33: Added keywords to args of function 'tf.shape'
INFO line 151:7: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
ERROR line 154:10: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
ERROR line 155:13: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
INFO line 157:7: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
ERROR line 160:10: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
ERROR line 161:12: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
INFO line 192:24: Changing tf.contrib.layers.variance_scaling_initializer to a tf.compat.v1.keras.initializers.VarianceScaling and converting arguments.
INFO line 194:36: tf.constant_initializer requires manual check. Initializers no longer have the dtype argument in the constructor or partition_info argument in the __call__ method.
The calls have been converted to compat.v1 for safety (even though they may already have been correct).
INFO line 194:36: Renamed 'tf.constant_initializer' to 'tf.compat.v1.constant_initializer'
WARNING line 196:9: Using member tf.contrib.rnn.GRUBlockCell in deprecated module tf.contrib.rnn. (Manual edit required) tf.contrib.rnn.* has been deprecated, and widely used cells/functions will be moved to tensorflow/addons repository. Please check it there and file Github issues if necessary.
ERROR line 196:9: Using member tf.contrib.rnn.GRUBlockCell in deprecated module tf.contrib. tf.contrib.rnn.GRUBlockCell 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.
INFO line 198:6: Added keywords to args of function 'tf.shape'
INFO line 198:33: Added keywords to args of function 'tf.shape'
INFO line 200:7: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
ERROR line 203:10: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
INFO line 204:15: Renamed 'tf.nn.dynamic_rnn' to 'tf.compat.v1.nn.dynamic_rnn'
ERROR line 205:11: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
WARNING line 208:25: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
INFO line 208:25: Renamed 'tf.get_variable' to 'tf.compat.v1.get_variable'
INFO line 213:9: Added keywords to args of function 'tf.shape'
INFO line 213:28: Added keywords to args of function 'tf.shape'
INFO line 214:7: Renamed 'tf.variable_scope' to 'tf.compat.v1.variable_scope'
ERROR line 217:10: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
ERROR line 218:12: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
INFO line 219:9: Renamed 'tf.check_numerics' to 'tf.debugging.check_numerics'
INFO line 220:8: Renamed 'tf.check_numerics' to 'tf.debugging.check_numerics'
INFO line 221:10: Renamed 'tf.check_numerics' to 'tf.debugging.check_numerics'
TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 1 files
Detected 17 issues that require attention
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
File: networks.py
--------------------------------------------------------------------------------
networks.py:30:6: ERROR: Using member tf.contrib.distributions in deprecated module tf.contrib. tf.contrib.distributions 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.
networks.py:102:10: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:103:11: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:106:25: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
networks.py:115:10: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:116:12: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:154:10: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:155:13: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:160:10: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:161:12: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:196:9: WARNING: Using member tf.contrib.rnn.GRUBlockCell in deprecated module tf.contrib.rnn. (Manual edit required) tf.contrib.rnn.* has been deprecated, and widely used cells/functions will be moved to tensorflow/addons repository. Please check it there and file Github issues if necessary.
networks.py:196:9: ERROR: Using member tf.contrib.rnn.GRUBlockCell in deprecated module tf.contrib. tf.contrib.rnn.GRUBlockCell 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.
networks.py:203:10: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:205:11: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:208:25: WARNING: tf.get_variable requires manual check. tf.get_variable returns ResourceVariables by default in 2.0, which have well-defined semantics and are stricter about shapes. You can disable this behavior by passing use_resource=False, or by calling tf.compat.v1.disable_resource_variables().
networks.py:217:10: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
networks.py:218:12: ERROR: Using member tf.contrib.layers.fully_connected in deprecated module tf.contrib. tf.contrib.layers.fully_connected 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.
4.3阅读报告之后发现了3个要修改的地方
4.3.1tf.contrib.layers
vim的整体的替换Linux vi/vim | 菜鸟教程 (runoob.com)
:1,$s/tf.contrib.layers/tf.compat.v1.layers/g
4.3.2tf.contrib.distributions
Module: tf.contrib.distributions | TensorFlow v1.15.0 (google.cn)
所以代码的修改
原始的代码
tfd = tf.contrib.distributions
修改为
import tensorflow_probability as tfp
tfd = tfp.distributions
注意一下,distributions指的是数学分布,相当于一个数学包
4.3.3 tf.contrib.rnn
利用vim修改为tf.conpat.v1.nn.rnn
5.测试运行
6.提供一些链接
(153条消息) module ‘tensorflow’ has no attribute ‘contrib’个人简要思路_编程爱撞墙的铁头娃的博客-CSDN博客https://blog.csdn.net/qq_43218533/article/details/120294987(153条消息) tf2.0不降版本也能完美解决module ‘tensorflow’ has no attribute ‘contrib’的问题_lllms的博客-CSDN博客https://blog.csdn.net/lms18811338696/article/details/108233705