当前位置:   article > 正文

tensorflow‘ has no attribute ‘variable_scope‘ tf.placeholder() is not compatible tensorflow.compat.v_module 'tensorflow' has no attribute 'variable

module 'tensorflow' has no attribute 'variable

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

整个项目是基于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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

解决方案:使用compat接口

tf.compat.v1.variable_scope
  • 1

类似的错误

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'
  • 1
  • 2
  • 3
  • 4
  • 5

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

解决方法:

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

tf.compat.v1.disable_eager_execution()
  • 1

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

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

这个问题最难解决:

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

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
  • 1
  • 2

最基本的解决方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/569664
推荐阅读
相关标签