这是我用来学习加法函数(y=x1 + x2)的SVR模型:
%reset -f#Libsfrom sklearn import svm;#PROGRAMME ENTRY POINT==========================================================#Data, addition#Exp[I] = sum(Inp[I])Inp = [[1,2],[3,4],[5,6],[7,8],[9,0]];Exp = [ 3, 7, 11, 15, 9 ];#TrainModel = svm.SVR(kernel="poly",degree=3);Model.fit(Inp,Exp);#Inferprint("Input values are those in the train data:");print(f"1 + 2 = {Model.predict([[1,2]])[0]:.6f}");print("\nInput values are those in the train data:");print(f"5 + 6 = {Model.predict([[5,6]])[0]:.6f}");print("\nInput values are those NOT in the train data, but in range:");print(f"5 + 5 = {Model.predict([[5,5]])[0]:.6f}");print("\nInput values are those NOT in the train data, and OUT of range:");print(f"9 + 1 = {Model.predict([[9,1]])[0]:.6f}");#EOF
但结果与预期不符:
Input values are those in the train data:1 + 2 = 6.007171Input values are those in the train data:5 + 6 = 9.595818Input values are those NOT in the train data, but in range:5 + 5 = 8.533934Input values are those NOT in the train data, and OUT of range:9 + 1 = 9.170507
sklearn的SVR是否有可能泛化加法函数?在上面的代码中应该做哪些更改才能让SVR学习到x1+x2?
回答:
三次多项式核函数的方差过大,无法正确预测如此简单的函数,尤其是在如此小的数据集上。这是基于偏差/方差权衡的。你的模型在方差上损失了很多,而在偏差上几乎没有收益(你的函数过于复杂)。即使是低阶多项式和径向基函数也是如此。
降低模型的方差可以解决这个问题。只需使用线性核函数即可。
Model = svm.SVR(kernel="linear")
使用线性核函数的SVM结果如下:
Input values are those in the train data:1 + 2 = 3.100000Input values are those in the train data:5 + 6 = 10.966667Input values are those NOT in the train data, but in range:5 + 5 = 9.983333Input values are those NOT in the train data, and OUT of range:9 + 1 = 9.983333