Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将无编译函数与scipy最小化一起使用_Python_Optimization_Theano_Minimize - Fatal编程技术网

Python 将无编译函数与scipy最小化一起使用

Python 将无编译函数与scipy最小化一起使用,python,optimization,theano,minimize,Python,Optimization,Theano,Minimize,我现在正在写一个脚本来计算一些函数,这些函数应该适合一些中子散射数据。 我有一个带有能量值的数组X,我把它和其他参数一起传递给函数,以得到拟合曲线。 我使用一个简单的平方距离来最小化,我想使用theano并同时计算包含所有数据集的大数组(测量的所有散射角的参数值相同,现在我只是在每个角度上测试最小化例程) 使用标准库直接定义的表达式可以正常工作,但使用theano,scipy的minimize中的“x”向量的参数从未更新,它们只是保持与初始猜测相同,就像成本函数不依赖于它一样 代码如下: def

我现在正在写一个脚本来计算一些函数,这些函数应该适合一些中子散射数据。 我有一个带有能量值的数组X,我把它和其他参数一起传递给函数,以得到拟合曲线。 我使用一个简单的平方距离来最小化,我想使用theano并同时计算包含所有数据集的大数组(测量的所有散射角的参数值相同,现在我只是在每个角度上测试最小化例程)

使用标准库直接定义的表达式可以正常工作,但使用theano,scipy的minimize中的“x”向量的参数从未更新,它们只是保持与初始猜测相同,就像成本函数不依赖于它一样

代码如下:

def resFunc_pseudo_voigt():
''' Resolution function for the backscattering spectrometer using a pseudo-voigt profile.

    Returns: The compiled theano function

    Inputs: normF   - scale factor which can be used for normalization
            S       - contribution factor for the lorentzian
            lorW    - lorentzian width
            gauW    - gaussian width    
            shift   - shift from 0 of the voigt profile's maximum
            bkgd    - background term

    Output: The compiled theano function result '''


X = T.vector()
normF = T.scalar()
S = T.scalar()
lorW = T.scalar()
gauW = T.scalar()
shift = T.scalar()
bkgd = T.scalar()


results, updates = theano.scan(lambda x_i: (normF * (S * lorW / (lorW**2 + (x_i - shift)**2) / np.pi 
                                         + (1-S) * T.exp(-(x_i-shift)**2 / 2*gauW**2) / gauW*T.sqrt(2*np.pi)
                                         + bkgd)), 
                                         sequences=X)

f_out = theano.function(inputs=[X, normF, S, lorW, gauW, shift, bkgd],
                    outputs=results, updates=updates, allow_input_downcast=True)


return f_out
以及使用:

def res_cost(self, x, datas):

    cost = np.sum((datas.intensities - self.resFunc(datas.energies, *x))**2 / datas.errors**2)

    return cost


def resFit(self):

    for i, resFile in enumerate(self.dataList):
        resList = []
        for j, datas in enumerate(resFile):
            resList.append(optimize.minimize(lambda x: self.res_cost(x, datas),
                                             datas.intensities
                                             [50, 0.4, 0.5, 0.4, 0, 0.05],
                                             bounds=[(0., 1000.), (0., 1.), (0., 10.), (0., 10.),
                                                     (-5., 5.), (0., 0.8)]))

            print('\n> Final result for qVal = %s: ' % datas.qVal, flush=True)
            print('Fit normF : ' + str(resList[j].x[0]), flush=True)
            print('Fit S     : ' + str(resList[j].x[1]), flush=True)
            print('Fit lorW  : ' + str(resList[j].x[2]), flush=True)
            print('Fit gauW  : ' + str(resList[j].x[3]), flush=True)
            print('Fit shift : ' + str(resList[j].x[4]), flush=True)
            print('Fit bkgd  : ' + str(resList[j].x[5]), flush=True)

        self.resFitList.append(resList)
有人有主意吗

谢谢