c++ - 使用 Pybind11 将 Eigen::Tensor 暴露给 Python

标签 c++ eigen tensor pybind11

我正在尝试使用 pybind11 将 Eigen 张量公开给 python。我可以毫无问题地编译所有内容,并可以将其成功导入 python。但是,无法将数据转换为 python 类型。我尝试了两种方法。一种是直接公开数据,第二种是使用映射。两者都在 python 环境中失败。

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/numpy.h>
#include <pybind11/eigen.h>
#include <unsupported/Eigen/CXX11/Tensor>

class myclass{

    myclass(){
        m_data = new float[m_dim1*m_dim2*m_dim3]; // Contiguous data that represents a three dimensional array
        for(int i = 0; i<m_dim1*m_dim2*m_dim3; i++)
            m_data[i] = i;

        m_tensor = Eigen::TensorMap<Eigen::Tensor<float, 3>>(m_data, m_dim1, m_dim2, m_dim3);
    }

    Eigen::TensorMap<Eigen::Tensor<float, 3>>& getDataUsingMapping() { Eigen::TensorMap<Eigen::Tensor<float, 3>> temp(m_data, m_dim1, m_dim2, m_dim3);  return temp; }
    Eigen::Tensor<float, 3>& getDataWithoutUsingMapping() { return m_tensor};


private:
    Eigen::Tensor<float, 3> m_tensor;
    // In fact, m_data, m_dim1, m_dim2, m_dim3 all are
    // read from a data file but for this example let's 
    // assume some values.
    float* m_data; 
    int m_dim1 = 2, m_dim2 = 5, m_dim3 = 10;
}


PYBIND11_MODULE(example, m) {
    py::class_<myclass>(m, "myclass")
        .def(py::init<>())
        .def("getDataUsingMapping", &myClass::getDataUsingMapping, py::return_value_policy::reference)
        .def("getDataWithoutUsingMapping", &myClass::getDataWithoutUsingMapping, py::return_value_policy::reference);
}

我希望能够在 python 中处理这个 3D 数组及其维度信息 (m_dim1, m_dim2, m_dim3)

这是我尝试在 python 中获取数据后得到的错误消息。

>>> import example
>>> d = example()
>>>
>>> DataInPython = d.getDataUsingMapping()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Unable to convert function return value to a Python type! The signature was
        (self: example) -> Eigen::TensorMap<Eigen::Tensor<float,3,0,__int64>,0,Eigen::MakePointer>
>>>
>>>
>>> DataInPython = d.getDataWithoutUsingMapping()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Unable to convert function return value to a Python type! The signature was
        (self: example) -> Eigen::Tensor<std::complex<float>,3,0,__int64>

Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,
<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic
conversions are optional and require extra headers to be included
when compiling your pybind11 module.

我尝试包含所有 pybdin11 包含文件,但没有解决问题。有人可以帮助我吗?

最佳答案

C++ 代码无法编译并且 python 代码不可能像发布的那样运行,但在修复这些并进行逻辑更改之后,结论仍然是 pybind11 不支持来自“unsupported/Eigen/CXX11/Tensor”的 TensorMap "因为该类不提供与其他 Eigen 映射类相同的接口(interface)。

我原以为映射器施法器的特化会自动工作,但这样做是明确的:

template<>
struct py::detail::type_caster<Eigen::TensorMap<Eigen::Tensor<float, 3>>, void>
    : py::detail::eigen_map_caster<Eigen::TensorMap<Eigen::Tensor<float, 3>>> {};

pybind11::detail::EigenProps 的实例化失败,b/c TensorMap 不提供其具有 cols/rows/stride 的维度。因此,SFINAE 阻止了脚轮的自动生成。

除了使用名为“unsupported”的目录中的 header 之外,没有其他选择吗?如果没有,最好的办法是将 TensorMap 的内容复制到一个 numpy 数组,并在 getDataUsingMapping 的自定义中返回它:有几个示例说明如何在 SO 上执行此操作,包括复制和不复制。 (除非您愿意展平张量,否则 EigenProps 的特化将不起作用,但您可以使用它作为示例为 TensorMap 编写新的通用类型施法器。)

关于c++ - 使用 Pybind11 将 Eigen::Tensor 暴露给 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60344631/

相关文章:

python - 通过索引列表选择pytorch张量元素

python - 如何从 tensorflow 中的向量构造成对差的平方?

c++ - 我无法使用 premake5 添加额外的库

c++ - 逐行读取文本文件会使 vector 为空

c++ - 具有子矩阵类的特征引用

c++ - 为什么选择 Eigen 用于 TensorFlow?

python - 卡住图到 Tflite 转换错误 -> ValueError - 为输入数组 'wav data' 提供输入形状

c++ - recv() 与 errno=107 :(transport endpoint connected)

c++ - 嵌套模板需要显式构造吗?

c++ - 如何使用 Eigen 获取特征值的原始顺序?