tensorflow‘ has no attribute ‘variable_scope‘ tf.placeholder() is not compatible tensorflow.compat.v

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'

解决方案:使用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)

image-20231205212705577

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

D:\Project\PycharmProjects\PRIMAL\od_mstar3\mstar_type_defs.hpp(8): fatal error C1083: 无法打开包括文件: “boost/graph/graph_traits.hpp”: No such file or directory

该项目要编译一个python的C++扩展模块,然后在CPP文件中,调用boost库中的一些文件。

当然这还涉及一些东西,python setup.py的知识,这一次仅用到构建C++扩展。

setup(ext_modules = cythonize(Extension(
           "cpp_mstar",
           sources=["cython_od_mstar.pyx"],
           extra_compile_args=["-std=c++11"]
      )))

该项目中,源码是👆的,在项目中的一个CPP文件中,包含了<boost/graph/graph_traits.hpp>头文件,并且我也安装了boost库,库中也有这个头文件,还是找不到。猜测可能是路径错误,即,没有向编译文件中说明boost库所在位置。

参考了python文档:4. 构建C/C++扩展 — Python 3.11.7 說明文件fatal error C1083: 无法打开包括文件: “boost/shared_ptr.hpp”: N o such file or directory_有包含文件,但是还是出现fatal error: boost/archive/text_oarchi-CSDN博客

发现官方文档上,扩展库的构建中,包括include_dirs等参数

module1 = Extension('demo',
                    define_macros = [('MAJOR_VERSION', '1'),
                                     ('MINOR_VERSION', '0')],
                    include_dirs = ['/usr/local/include'],
                    libraries = ['tcl83'],
                    library_dirs = ['/usr/local/lib'],
                    sources = ['demo.c'])

表明需要写明扩展。

setup(ext_modules = cythonize(Extension(
           "cpp_mstar",
           include_dirs = [r'D:\Tool\boost_1_81_0'],
           sources=["cython_od_mstar.pyx"],
           extra_compile_args=["-std=c++11"]
      )))

所以在上述代码中增加了include_dirs = [r'D:\Tool\boost_1_81_0'],,指明需要包含的库的路径。问题得到解决。

windows下编译这些项目,即使把这些需要的库地址写入到系统环境变量中,在编译的时候也是没有办法找到的,但是在Linux下编译,是可以的。

error C2065: “uint”: 未声明的标识符

windows下会报的错误,我在Ubuntu中编译这个项目,说实话还是容易的。

#include <wtypes.h>
#include <stdint.h>
#define ulong ULONG 
#define uint UINT 

windows下boost安装

有exe安装,编译安装。

不适用PCL点云库,建议直接exe安装,主要是简单。

网站下载exe文件。Boost C++ Libraries - Browse /boost-binaries at SourceForge.net,我下载的是1.83.0的exe文件;下载好后,放置到自己工作空间的文件夹下,然后双击exe文件开始安装。

配置环境变量:(不要忘)

配置 BOOST_INCLUDEDIR 和 BOOST_LIBRARYDIR和 PATH 中添加 ,

BOOST_INCLUDEDIR="D:\Tool\boost_1_81_0"  # 自己boost的安装目录
BOOST_LIBRARYDIR=D:\Tool\boost_1_81_0\lib64-msvc-14.3

编译的方式可以参考:Windows VS2019编译Boost库学习记录-CSDN博客

编译方式,文件的下载地址为:Boost Downloads

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUNX-T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值