def Get_svm_model(parameter, X, y): svm = SVM() loss_histroy = svm.Train( X, y, parameter[1], 1, parameter[0], 200, 1500, True) VisualizeLoss(loss_histroy) input("Enter any key to predict...") return svm
def AutoGetPara(X_train, y_train, X_val, y_val): learning_rate = [1e-8, 1e-7] # 学习率 regularization_strengths = [5e4, 1e5] # 正则化强度 best_parameter = None best_val = -1 # 最优评分值 delta = 1 # SVM损失函数中的delta参数 batch_num = 200 # 小批量训练 num_iter = 1500 # 迭代次数 for i in learning_rate: for j in regularization_strengths: svm = SVM() svm.Train(X_train, y_train, j, delta, i, batch_num, num_iter, True) # 得到了权重W y_pred = svm.Predict(X_val) # 利用权重W得到评分值 acc_val = np.mean(y_val == y_pred) if best_val < acc_val: best_val = acc_val best_parameter = (i, j) print('OK! Have been identified parameter! Best validation accuracy achieved during cross-validation: %f' % best_val) return best_parameter