通过 Pybind11 将 Eigen csr_matrix 导出到 python,而不转换为 scipy.sparse.csc/csr 矩阵

发布于 01-17 18:06 字数 1352 浏览 3 评论 0原文

我想将特征CSR稀疏矩阵导出到Python,以执行基准测试。我很清楚scipy.sparse csr矩阵正在遵循csr矩阵的规范格式,而eigen具有不同的表示,这是我想要玩的。
但是,当导出特征CSR矩阵时,Pybind11将其导出为Scipy.sparse类型,我实际上想避免使用。

我尝试了以下操作:

    typedef Eigen::SparseMatrix< double >  TpSpMatrix_csr;

    py::class_<TpSpMatrix_csr> csr_matrix_eigen(m, "csr_matrix_eigen");

    csr_matrix_eigen
    .def(py::init<>()
    , "Default constructor"
    )

    .def(py::init<TpSpMatrix_csr>()
    , "Init with csr matrix"
    , py::arg("A")
     )
    ;

    m.def("csr_test", []( TpSpMatrix_csr& A) { std::cout << "csr!"<< std::endl; }
        , "csr test"
        , py::arg("A").noconvert()
        );

将其导入ipython后,我将获得以下内容:

In [2]: A = csr_matrix_eigen()

In [3]: csr_test(A)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-77568ef56cf5> in <module>
----> 1 csr_test(A)

TypeError: csr_test(): incompatible function arguments. The following argument types are supported:
    1. (A: scipy.sparse.csc_matrix[numpy.float64]) -> None

Invoked with: <eigen_test.csr_matrix_eigen object at 0x7f9e78265af0>

与封装在另一个类中的特征稀疏矩阵相比,是否有更简单的方法来实现此目标?

简而言之,我想在没有scipy的情况下揭露特征稀疏的矩阵。

I would like to export the eigen csr sparse matrix to python in order to perform benchmarks. I'm well aware that the scipy.sparse csr matrix is following the canonical format of a csr matrix, while eigen has a different representation, and that's specifically what I would like to play with.
However, when exporting the eigen csr matrix, pybind11 exports it as a scipy.sparse type, which I actually wanted to avoid.

I tried the following:

    typedef Eigen::SparseMatrix< double >  TpSpMatrix_csr;

    py::class_<TpSpMatrix_csr> csr_matrix_eigen(m, "csr_matrix_eigen");

    csr_matrix_eigen
    .def(py::init<>()
    , "Default constructor"
    )

    .def(py::init<TpSpMatrix_csr>()
    , "Init with csr matrix"
    , py::arg("A")
     )
    ;

    m.def("csr_test", []( TpSpMatrix_csr& A) { std::cout << "csr!"<< std::endl; }
        , "csr test"
        , py::arg("A").noconvert()
        );

After importing it into ipython I get the following:

In [2]: A = csr_matrix_eigen()

In [3]: csr_test(A)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-77568ef56cf5> in <module>
----> 1 csr_test(A)

TypeError: csr_test(): incompatible function arguments. The following argument types are supported:
    1. (A: scipy.sparse.csc_matrix[numpy.float64]) -> None

Invoked with: <eigen_test.csr_matrix_eigen object at 0x7f9e78265af0>

Is there a simpler way to achieve this compared to encapsulating the eigen sparse matrix in yet another class?
In short, I would like to expose the eigen sparse matrix without scipy.sparse being involved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音2025-01-24 18:06:59

好的,发布问题似乎有所帮助。尽管我已经在这个问题上挣扎了一段时间,但只有在发布问题后,我找到了一个解决方案,即,我必须

PYBIND11_MAKE_OPAQUE(TpSpMatrix_csr);

在Pybind接口文件的顶部包含一个。
现在,我可以根据需要调用我的测试功能。
我希望这可以对其他人的其他人有所帮助。

o.k., posting questions seems to help. Although I struggled with this problem for some time now, only after posting the question I found a solution, namely, I have to include a

PYBIND11_MAKE_OPAQUE(TpSpMatrix_csr);

at the top of my pybind interface file.
I can now call my test function as desired.
I hope that this may help someone else in a similar pronlem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文