def plot_convergence_synthetic(svm, lr): for i in range(4): a = data[i] b = Label[i] n, m = np.shape(a) a = np.vstack((a, np.ones((1, m)))) b = b.reshape(-1, 1) x = np.zeros((3, 1)) a1 = a b1 = b if svm: x_bt, norm_bt, _, acc_bt, duration_bt, diteration_bt = SVM.BackTracking( a, b, x, args.l, args.d, False, a1, b1) f_bt = open('svm_acc_bt_' + str(i) + '.txt', mode='a+') f_bt.write( np.str(acc_bt) + ' ' + str(duration_bt) + ' ' + str(diteration_bt)) x_AGM, norm_AGM, _, acc_AGM, duration_AGM, diteration_AGM = SVM.AGM( a, b, x, args.l, args.d, False, a1, b1) f_AGM = open('svm_acc_AGM_' + str(i) + '.txt', mode='a+') f_AGM.write( np.str(acc_AGM) + ' ' + str(duration_AGM) + ' ' + str(diteration_AGM)) x_BFGS, norm_BFGS, _, acc_BFGS, duration_BFGS, diteration_BFGS = SVM.G_BFGS( a, b, x, args.l, args.d, False, a1, b1) f_BFGS = open('svm_acc_BFGS_' + str(i) + '.txt', mode='a+') f_BFGS.write( np.str(acc_BFGS) + ' ' + str(duration_BFGS) + ' ' + str(diteration_BFGS)) Draw.gradient_plot(len(norm_bt), norm_bt, 'backtracking') Draw.gradient_plot(len(norm_AGM), norm_AGM, 'AGM') Draw.gradient_plot(len(norm_BFGS), norm_BFGS, 'BFGS') plt.savefig('SVM_convergence_' + str(i)) # plt.show() plt.close() if lr: x_AGM, norm_AGM, _, acc_AGM, duration_AGM, diteration_AGM = LR.AGM( a, b, x, args.l, False, a1, b1) f_AGM = open('lr_acc_AGM_' + str(i) + '.txt', mode='a+') f_AGM.write( np.str(acc_AGM) + ' ' + str(duration_AGM) + ' ' + str(diteration_AGM)) x_L_BFGS, norm_L_BFGS, _, acc_L_BFGS, duration_L_BFGS, diteration_L_BFGS = LR.L_BFGS( a, b, x, args.m, args.l, False, a1, b1) f_L_BFGS = open('lr_acc_LBFGS_' + str(i) + '.txt', mode='a+') f_L_BFGS.write( np.str(acc_L_BFGS) + ' ' + str(duration_L_BFGS) + ' ' + str(diteration_L_BFGS)) Draw.gradient_plot(len(norm_AGM), norm_AGM, 'AGM') Draw.gradient_plot(len(norm_L_BFGS), norm_L_BFGS, 'L_BFGS') plt.savefig('lr_convergence_' + str(i)) # plt.show() plt.close()
def plot_division(svm, lr): for i in range(4): a = data[i] c1 = C1[i] c2 = C2[i] b = Label[i] n, m = np.shape(a) a = np.vstack((a, np.ones((1, m)))) b = b.reshape(-1, 1) x = np.zeros((3, 1)) a1 = a b1 = b if svm: path_BT = 'svm_BT_a' + str(i) path_AGM = 'svm_AGM_a' + str(i) path_BFGS = 'svm_BFGS_a' + str(i) x_bt, norm_bt, _, acc_bt, duration_bt, diteration_bt = SVM.BackTracking( a, b, x, args.l, args.d, False, a1, b1) x_AGM, norm_AGM, _, acc_AGM, duration_AGM, diteration_AGM = SVM.AGM( a, b, x, args.l, args.d, False, a1, b1) x_BFGS, norm_BFGS, _, acc_BFGS, duration_BFGS, diteration_BFGS = SVM.G_BFGS( a, b, x, args.l, args.d, False, a1, b1) Draw.plot_division(c1, c2, x_AGM, path_BT, 'svm_BT') Draw.plot_division(c1, c2, x_AGM, path_AGM, 'svm_AGM') Draw.plot_division(c1, c2, x_BFGS, path_BFGS, 'svm_BFGS') if lr: path_AGM = 'lr_AGM_a' + str(i) path_LBFGS = 'lr_LBFGS_a' + str(i) x_AGM, norm_AGM, _, acc_AGM, duration_AGM, diteration_AGM = LR.AGM( a, b, x, args.l, False, a1, b1) x_L_BFGS, norm_L_BFGS, _, acc_L_BFGS, duration_L_BFGS, diteration_L_BFGS = LR.L_BFGS( a, b, x, args.m, args.l, False, a1, b1) Draw.plot_division(c1, c2, x_AGM, path_AGM, 'AGM') Draw.plot_division(c1, c2, x_L_BFGS, path_LBFGS, 'LBFGS')