PRIMAL项目,module ‘tensorflow‘ has no attribute ‘variable_scope‘,tf.placeholder() is not compatible

PRIMAL项目,module ‘tensorflow’ has no attribute ‘variable_scope’,tf.placeholder() is not compatible with eager execution

文章目录

  • PRIMAL项目,module 'tensorflow' has no attribute 'variable_scope',tf.placeholder() is not compatible with eager execution

整个项目是基于tensorflow 1.x版本的。众所周知,tf 1.0 和 2.0 的兼容有点差。

AttributeError: module ‘tensorflow’ has no attribute ‘variable_scope’

Traceback (most recent call last):
  File "primal_testing.py", line 133, in <module>
    primal=PRIMAL('model_primal',10)
  File "primal_testing.py", line 25, in __init__
    self.network=ACNet("global",5,None,False,grid_size,"global")
  File "/root/WorkSpace/PRIMAL/ACNet.py", line 23, in __init__
    with tf.variable_scope(str(scope)+'/qvalues'):
AttributeError: module 'tensorflow' has no attribute 'variable_scope'

解决方案:使用compat接口

tf.compat.v1.variable_scope

类似的错误

AttributeError: module 'tensorflow' has no attribute 'variable_scope'
AttributeError: module 'tensorflow' has no attribute 'Session'
AttributeError: module 'tensorflow' has no attribute 'ConfigProto'
AttributeError: module 'tensorflow' has no attribute 'GPUOptions'
AttributeError: module 'tensorflow' has no attribute 'placeholder'

tf.placeholder() is not compatible with eager execution.

参考文章:TensorFlow报错:tf.placeholder() is not compatible with eager execution.,内容详细。

Traceback (most recent call last):
  File "primal_testing.py", line 133, in <module>
    primal=PRIMAL('model_primal',10)
  File "primal_testing.py", line 25, in __init__
    self.network=ACNet("global",5,None,False,grid_size,"global")
  File "/root/WorkSpace/PRIMAL/ACNet.py", line 25, in __init__
    self.inputs = tf.placeholder(shape=[None,4,GRID_SIZE,GRID_SIZE], dtype=tf.float32)
  File "/root/miniconda3/envs/myconda/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 3268, in placeholder
    raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.

解决方法:

在调用tf.placeholder()方法之前使用该语句。

tf.compat.v1.disable_eager_execution()

tf.compat.v1.disable_eager_execution()
self.inputs = tf.placeholder(shape=[None,4,GRID_SIZE,GRID_SIZE], dtype=tf.float32)

AttributeError: module ‘tensorflow.compat.v1’ has no attribute ‘contrib’

这个问题最难解决:

原因是tensorflow1.x的contrib包被整合进了2.0的其他包里。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

最基本的解决方法。

如果解决不了,就参考官方文档TensorFlow API Versions | TensorFlow v2.14.0 (google.cn)。

PRIMAL项目,module ‘tensorflow‘ has no attribute ‘variable_scope‘,tf.placeholder() is not compatible_第1张图片

查看自己当前的环境的tensorflow版本的api,然后查看项目的tensorflow版本api,两个对照以后进行修改。

你可能感兴趣的:(算法,tensorflow,人工智能)