Python 在PyTorch C++;扩展名,如何访问张量中的单个元素并将其转换为标准c++;数据类型? 我正在编写一个PyTrk的C++扩展,其中我需要用索引访问张量元素,并且我还需要把元素转换成标准C++类型。下面是一个简短的例子。假设我有一个二维张量a,我需要访问a[I][j],并将其转换为float #包括 浮点get(torch::张量a,int i,int j){ 返回[i][j]; }

Python 在PyTorch C++;扩展名,如何访问张量中的单个元素并将其转换为标准c++;数据类型? 我正在编写一个PyTrk的C++扩展,其中我需要用索引访问张量元素,并且我还需要把元素转换成标准C++类型。下面是一个简短的例子。假设我有一个二维张量a,我需要访问a[I][j],并将其转换为float #包括 浮点get(torch::张量a,int i,int j){ 返回[i][j]; },python,c++,pytorch,libtorch,Python,C++,Pytorch,Libtorch,上述内容被放入名为tensortest.cpp的文件中。在另一个文件setup.py中,我写入 从设置工具导入设置,扩展 从torch.utils导入cpp_扩展 安装程序(name='tensortest', ext_modules=[cpp_extension.CppExtension('tensortest_cpp',['tensortest.cpp']), cmdclass={'build\u ext':cpp\u extension.BuildExtension}) 当我运行pyth

上述内容被放入名为
tensortest.cpp
的文件中。在另一个文件
setup.py
中,我写入

从设置工具导入设置,扩展
从torch.utils导入cpp_扩展
安装程序(name='tensortest',
ext_modules=[cpp_extension.CppExtension('tensortest_cpp',['tensortest.cpp']),
cmdclass={'build\u ext':cpp\u extension.BuildExtension})
当我运行
python setup.py install
时,编译器会报告以下错误

running install
running bdist_egg
running egg_info
creating tensortest.egg-info
writing tensortest.egg-info/PKG-INFO
writing dependency_links to tensortest.egg-info/dependency_links.txt
writing top-level names to tensortest.egg-info/top_level.txt
writing manifest file 'tensortest.egg-info/SOURCES.txt'
/home/trisst/.local/lib/python3.8/site-packages/torch/utils/cpp_extension.py:335: UserWarning: Attempted to use ninja as the BuildExtension backend but we could not find ninja.. Falling back to using the slow distutils backend.
  warnings.warn(msg.format('we could not find ninja.'))
reading manifest file 'tensortest.egg-info/SOURCES.txt'
writing manifest file 'tensortest.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
building 'tensortest_cpp' extension
creating build
creating build/temp.linux-x86_64-3.8
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/user/.local/lib/python3.8/site-packages/torch/include -I/home/user/.local/lib/python3.8/site-packages/torch/include/torch/csrc/api/include -I/home/user/.local/lib/python3.8/site-packages/torch/include/TH -I/home/user/.local/lib/python3.8/site-packages/torch/include/THC -I/usr/include/python3.8 -c tensortest.cpp -o build/temp.linux-x86_64-3.8/tensortest.o -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=tensortest_cpp -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++14
In file included from /home/user/.local/lib/python3.8/site-packages/torch/include/ATen/Parallel.h:149,
                 from /home/user/.local/lib/python3.8/site-packages/torch/include/torch/csrc/api/include/torch/utils.h:3,
                 from /home/user/.local/lib/python3.8/site-packages/torch/include/torch/csrc/api/include/torch/nn/cloneable.h:5,
                 from /home/user/.local/lib/python3.8/site-packages/torch/include/torch/csrc/api/include/torch/nn.h:3,
                 from /home/user/.local/lib/python3.8/site-packages/torch/include/torch/csrc/api/include/torch/all.h:7,
                 from /home/user/.local/lib/python3.8/site-packages/torch/include/torch/extension.h:4,
                 from tensortest.cpp:1:
/home/user/.local/lib/python3.8/site-packages/torch/include/ATen/ParallelOpenMP.h:84: warning: ignoring #pragma omp parallel [-Wunknown-pragmas]
   84 | #pragma omp parallel for if ((end - begin) >= grain_size)
      | 
tensortest.cpp: In function ‘float get(at::Tensor, int, int)’:
tensortest.cpp:4:15: error: cannot convert ‘at::Tensor’ to ‘float’ in return
    4 |  return a[i][j];
      |               ^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
我能做什么?

编辑

#include <torch/extension.h>

float get(torch::Tensor a, int i, int j) 
{
    return a[i][j].item<float>(); 
}
#包括
浮点get(torch::张量a,int i,int j)
{
返回一个[i][j].item();
}
.item()
也应该足够了。无论如何,请加上一些解释,然后接受它。我知道这不多,但这是一个很好的做法。