lihang-code
lihang-code copied to clipboard
最小二乘法的代码是不是有问题
def residuals_func_regularization(p, x, y): ret = fit_func(p, x) - y ret = np.append(ret, np.sqrt(0.5*regularization*np.square(p))) # L2范数作为正则化项 return ret
是不是应该改成
def residual_func_regularization(p, x, y): ret = fit_function(p, x) - y ret = np.append(ret / np.sqrt(p.size), np.sqrt(0.5 * regularization * np.square(p))) return ret
L2 范数 前面的是不是要处以N
这个可以不用除
除不影响结果,但是公式里面是有的吧
是啊
这里为啥要用append,不太理解
这里为啥要用append,不太理解
ret = np.append(ret, np.sqrt(0.5 * regularization * np.square(p))) 通过回调、开方的操做,将正则化项当成数据项,从而可以调用,scipy.optimize.leastsq
这里为啥要用append,不太理解
可以看leastsq库里提供的参数的介绍:
f : callable
The model function, f(x, ...). It must take the independent
variable as the first argument and the parameters to fit as
separate remaining arguments.
the parameters to fit as separate remaining arguments,将用来调整的参数的数据(这里指的是正则项),作为其他参数传入