python - “tensorflow/core/framework/common_shape_fns.h:没有这样的文件或目录”在tensorflow中添加自定义操作时
问题描述
我正在尝试使用此Tensorflow Doc在Google Colab的 Tensorflow 中添加自定义操作。但是,在构建时出现此错误。
2021-04-05 04:24:26.500483: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-04-05 04:24:29.436586: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
xor_op.cc:2:10: fatal error: tensorflow/core/framework/common_shape_fns.h: No such file or directory
#include "tensorflow/core/framework/common_shape_fns.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
构建命令是,
$ TF_LFLAGS=($(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_link_flags()))'))
$ TF_CFLAGS=($(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_compile_flags()))'))
$
$ g++ -std=c++14 -shared xor_op.cc -o xor_op.so -fPIC ${TF_CFLAGS[@]} ${TF_LFLAGS[@]} -O2
知道在这种情况下有什么问题吗?
解决方案
使用以下步骤在 Google Colab 中运行它。基本上,我们将在构建中删除变量(例如,TF_LFLAGS、TF_CFLAGS)的使用,并使用直接命令构建它。
- 添加.cpp 文件。
# create empty file and copy and paste code
! touch ./xor_op.cc
或者使用魔法操作,
%%writefile xor_op.cc
#And contents of the file
- 从 TensorFlow 获取编译标志。
tf.sysconfig.get_compile_flags()
# output: ['-I/usr/local/lib/python3.7/dist-packages/tensorflow/include',
# '-D_GLIBCXX_USE_CXX11_ABI=0']
- 从 TensorFlow 获取链接标志。
tf.sysconfig.get_link_flags()
# output: ['-L/usr/local/lib/python3.7/dist-packages/tensorflow',
# '-l:libtensorflow_framework.so.2']
- 构建操作。
! g++ -std=c++14 -shared \
xor_op.cc \
-o xor_op.so \
-fPIC \
-I/usr/local/lib/python3.7/dist-packages/tensorflow/include \
-D_GLIBCXX_USE_CXX11_ABI=0 \
-L/usr/local/lib/python3.7/dist-packages/tensorflow \
-l:libtensorflow_framework.so.2 \
-O2
现在这将创建预期的 .so 文件。gobrewers14在另一个问题的评论部分指出了这一点。
推荐阅读
- excel - 如何将 Excel VBA 类集合合并到接口/工厂方法中?
- typescript - 对于类型化对象,对“any”值的不安全成员访问
- swiftui - 使用非零值初始化可选的 @AppStorage 属性
- prometheus - Prometheus 如何包含缺失的实例指标
- reactjs - 在一个组件中找不到模块错误,但在其他组件中可以找到它
- amazon-cloudformation - 为 KMS 密钥添加多个 Principal 值
- unity3d - 我想做一个统一的P2P联网(考虑用镜像)游戏,没有专用服务器,类似于泰拉瑞亚和瓦尔海姆
- python - 无法弄清楚如何为 Django 中的 psycopg2.errors.NotNullViolation 正确“断言”
- gradle - Gradle 通过插件添加对另一个源集的依赖
- sass - sass 迁移器问题 - 绝对路径和 node_modules