Joa*_*him 5 c++ python numpy vector pybind11
使用 Pybind11,我试图将一个 numpy 数组传递给 c++ 到 a 中std::vector,将其乘以 2,然后将其std::vector作为 numpy 数组返回给 python。
我已经完成了第一步,但第三步是在做一些奇怪的事情。为了将它传回,我使用了:py::array ret = py::cast(vect_arr);奇怪的是,我的意思是 Python 中返回的向量没有正确的维度和正确的顺序。
例如,我有数组:
[[ 0.78114362 0.06873818 1.00364053 0.93029671]
[ 1.50885413 0.38219005 0.87508337 2.01322396]
[ 2.19912915 2.47706644 1.16032292 -0.39204517]]
Run Code Online (Sandbox Code Playgroud)
代码返回:
array([[ 1.56228724e+000, 3.01770826e+000, 4.39825830e+000,
5.37804299e+161],
[ 1.86059342e+000, 4.02644793e+000, -7.84090347e-001,
1.38298992e-309],
[ 1.75016674e+000, 2.32064585e+000, 0.00000000e+000,
1.01370255e-316]])
Run Code Online (Sandbox Code Playgroud)
我已经阅读了文档,但我必须承认无法理解其中的大部分内容。因此,对于这个具体示例的任何帮助将不胜感激。提前致谢。
这是一个尝试的例子:
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
namespace py = pybind11;
py::module nn = py::module::import("iteration");
py::array nump(py::array arr){
auto arr_obj_prop = arr.request();
//initialize values
double *vals = (double*) arr_obj_prop.ptr;
unsigned int shape_1 = arr_obj_prop.shape[0];
unsigned int shape_2 = arr_obj_prop.shape[1];
std::vector<std::vector <double>> vect_arr( shape_1, std::vector<double> (shape_2));
for(unsigned int i = 0; i < shape_1; i++){
for(unsigned int j = 0; j < shape_2; j++){
vect_arr[i][j] = vals[i*shape_1 + j*shape_2] * 2;
}
}
py::array ret = py::cast(vect_arr); //py::array(vect_arr.size(), vect_arr.data());
return ret;
}
PYBIND11_MODULE(iteration_mod, m) {
m.doc() = "pybind11 module for iterating over generations";
m.def("nump", &nump,
"the function which loops over a numpy array");
}
Run Code Online (Sandbox Code Playgroud)
和python代码:
import numpy as np
import iteration_mod as i_mod
class iteration(object):
def __init__(self):
self.iterator = np.random.normal(0,1,(3,4))
def transform_to_dict(self):
self.dict = {}
for i in range(self.iterator.shape[0]):
self.dict["key_number_{}".format(i)] = self.iterator[i,:]
return self.dict
def iterate_iterator(self):
return i_mod.nump(self.iterator)
def iterate_dict(self):
return i_mod.dict(self)
a = iteration()
print(a.iterator)
print(a.iterate_iterator())
Run Code Online (Sandbox Code Playgroud)
所有这些都编译为:c++ -O3 -Wall -fopenmp -shared -std=c++11 -fPICpython3 -m pybind11 --includesiteration_mod.cpp -o iteration_mod.so
std::vector<std::vector<double>>没有 2D 内置数组的内存布局,因此py::array(vect_arr.size(), vect_arr.data());将不起作用。
看起来 py::cast 确实进行了正确的复制转换并将值从向量传播到新的 numpy 数组,但是这一行:
vect_arr[i][j] = vals[i*shape_1 + j*shape_2] * 2;
Run Code Online (Sandbox Code Playgroud)
是不正确的。它应该是:
vect_arr[i][j] = vals[i*shape_2 + j] * 2;
Run Code Online (Sandbox Code Playgroud)