python做算法 不会c++如何将代码封装给别人调用_在C++中嵌入Python:在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);

Pythondef 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脚本失败。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值