在C++中嵌入Python:在Python脚本中导入模块在一个函数调用中工作,但不是一个函数

2024-09-30 10:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个程序,使用C++作为数据管理后端,可以调用用户创建的Python脚本来完成各种任务。我遇到了一个与在python脚本中导入模块有关的问题。我将值从C++传递到Python,并从SkEvin对这些值执行高斯过程回归,并简单地将优化模型值或GPR不确定性返回到C++。我将这两种情况(模型优化和模型验证)作为Python脚本中的两个独立函数,因为它们将从C++的不同地方调用。在

当我运行第一个函数(模型优化)时,一切都很好,并且我得到了优化的超参数来返回到C++而没有发生意外。但是在第二个函数调用期间,脚本失败,因为它无法从sklearn导入GPR模块(与前一个函数成功导入的模块相同)。我不太熟悉将Python嵌入到C++中,所以很可能我只是丢失了一些东西,或者不完全理解规则。无法从C++端提供一个可以自己运行的代码,所以我会尽我所能提供尽可能多的嵌入代码。下面的python脚本是完整的。如果你需要更多的信息请告诉我,我很乐意提供。谢谢你的帮助。在

<强> C++:主

//other stuff

Py_Initialize();

//do more other stuff (embedding happens here)

Py_Finalize();

//do even more other stuff

<强> C++:模型优化

^{pr2}$

<强> C++:模型验证<强/> /P>

PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");

pName = PyString_FromString(file.c_str());

pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL) {
    pFunc = PyObject_GetAttrString(pModule, function.c_str());

    pArgs = PyTuple_New(size);

    PyTuple_SetItem(pArgs, 0, PyFloat_FromDouble(testFP[0].size()));
    PyTuple_SetItem(pArgs, 1, PyFloat_FromDouble(testFP.size()));
    PyTuple_SetItem(pArgs, 2, PyFloat_FromDouble(trainingFP.size()));
    PyTuple_SetItem(pArgs, 3, PyFloat_FromDouble(sigma));
    PyTuple_SetItem(pArgs, 4, PyFloat_FromDouble(sigmaN));
    PyTuple_SetItem(pArgs, 5, PyFloat_FromDouble(sigmaF));

    k = 6;

    for(i = 0; i < testFP.size(); i++){
        for(j = 0; j < testFP[i].size(); j++){
            PyTuple_SetItem(pArgs, k, PyFloat_FromDouble(testFP[i][j]));
            k++;
        }           
    }
    for(i = 0; i < trainingFP.size(); i++){
        for(j = 0; j < trainingFP[i].size(); j++){
            PyTuple_SetItem(pArgs, k, PyFloat_FromDouble(trainingFP[i][j]));
            k++;
        }           
    }
    for(i = 0; i < trainingFP.size(); i++){
        PyTuple_SetItem(pArgs, k, PyFloat_FromDouble(trainingForces[i]));
        k++;
    }
    Py_INCREF(pValue);
    pValue = PyObject_CallObject(pFunc, pArgs);
    Py_DECREF(pArgs);

 }else {
    PyErr_Print();
    fprintf(stderr, "Failed to load \"%s\"\n", function.c_str());
}

Py_XDECREF(pFunc);
Py_DECREF(pModule);

for(i = 0; i < testFP.size(); i++)
    prediction[i] = PyFloat_AsDouble(PyList_GetItem(pValue, i));

Py_DECREF(pValue);

Python

def GPR(*X):

from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, WhiteKernel
import re

#initialize local variables
counter = 0
sigma_l_initial = 1
sigma_n_initial = 1
sigma_f_initial = 2
innerFPSize = int(X[0])
ntrain = int(X[1])

optimized_hyperparameters = []
forces = []
fp = [] 
sigma_l_bounds = [.01,100]
sigma_n_bounds = [.001,.1]

fp.append([])
#pass values from c++ conversion tuple to local lists
for x in X:
    if counter > 1 and counter < 2 + innerFPSize * ntrain:
        fp[len(fp) - 1].append(x)
    elif counter >= 2 + innerFPSize * ntrain:
        forces.append(x)

    counter += 1

    if len(fp[len(fp) -1]) == innerFPSize:
        if len(fp) < ntrain:
            fp.append([])

#GPR routine
krbf = sigma_f_initial*RBF(length_scale=sigma_l_initial,length_scale_bounds=(sigma_l_bounds[0],sigma_l_bounds[1]))
noise_kernel = WhiteKernel(noise_level=sigma_n_initial,noise_level_bounds=(sigma_n_bounds[0],sigma_n_bounds[1]))

gp = GaussianProcessRegressor(kernel=krbf + noise_kernel,normalize_y=True,n_restarts_optimizer=25)
gp.fit(fp, forces)

#get optimized hyperparameters
rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", str(gp.kernel_))
optimized_hyperparameters.append(float(rr[-1]))
optimized_hyperparameters.append(float(rr[-2]))
optimized_hyperparameters.append(float(rr[0]))

return optimized_hyperparameters





def GPR_unc(*X):

try:
    from sklearn.gaussian_process import GaussianProcessRegressor
    from sklearn.gaussian_process.kernels import RBF, WhiteKernel
except:
    print 'THIS REFUSES TO WORK'

#initialize variables
uncertainty = []
testFP = []
trainingFP = []
trainingForces = []

innerFPSize = int(X[0])
testSize = int(X[1])
ntrain = int(X[2])
sigma = float(X[3])
sigma_n = float(X[4])
sigma_f = float(X[5])

counter = 0

setTrainFP = setTrainForces = False
setTestFP = True

testFP.append([])
trainingFP.append([])

#parse data from C++ arrays
for x in X:

    try:
        if counter > 5 and setTestFP == True:
            testFP[len(testFP) - 1].append(x)
        elif setTrainFP == True:
            trainingFP[len(trainingFP) - 1].append(x)
        elif setTrainForces == True:
            trainingForces.append(x)

        if counter > 5 and setTestFP == True:
            if len(testFP[len(testFP) -1]) == innerFPSize:
                if len(testFP) + 1 <= testSize:
                    testFP.append([])
                else:
                    setTestFP = False
                    setTrainFP = True                                   
        elif setTrainFP == True:
            if len(trainingFP[len(trainingFP) -1]) == innerFPSize:
                if(len(trainingFP)) + 1 <= ntrain:
                    trainingFP.append([])
                else:
                    setTrainFP = False
                    setTrainForces = True
        counter += 1
    except:
        print 'ERROR'

#perform static "optimization" of gpr kernel to get gpr object
krbf = sigma_f**2*RBF(length_scale=sigma,length_scale_bounds=(sigma,sigma))
noise_kernel = WhiteKernel(noise_level=sigma_n,noise_level_bounds=(sigma_n,sigma_n))
gp = GaussianProcessRegressor(kernel=krbf + noise_kernel,normalize_y=True, optimizer=None)
gp.fit(trainingFP, trainingForces)

#get uncertanties on test set
val,std=gp.predict(testFP,return_std=True)

#ensure that the uncertainty is loaded into a float list in order to be sent back to c++
for x in std:
    uncertainty.append(float(x))
for x in std:
    uncertainty.append(float(x) * float(x))

return uncertainty

尝试从GPR_unc函数(python代码的第二个函数)导入模块时,python脚本失败。在


Tags: trueforsizeleniffloatsigmabounds
1条回答
网友
1楼 · 发布于 2024-09-30 10:41:09

在模型验证功能期间,python元组:

pArgs = PyTuple_New(size);

传递给它的大小与附加到它的大小相比不正确。崩溃的结果是一个简单的“写入超过数组末尾而不调整大小”。在

相关问题 更多 >

    热门问题