我正在写一个脚本来计算一些函数,这些函数应该适合一些中子散射数据。 我有一个带有能量值的数组X,我把它和其他参数一起传递给函数,得到拟合曲线。 我使用一个简单的平方距离来最小化,我想使用一个no并同时计算包含所有数据集的大数组(所有测量的散射角的参数值相同,现在我只是在每个角度上测试最小化例程)。你知道吗
使用标准库直接定义表达式可以很好地工作,但是使用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)
有人有主意吗?你知道吗
谢谢
目前没有回答
相关问题 更多 >
编程相关推荐