def main(): m = 3 n = 15 test_size = 40 k = int(1e6) # 产生数据 ori_x, ori_y = generate_data(n) test_x, test_y = generate_data(test_size) # 确定梯度下降初始化内容,不带正则项 # w0 = best_answer_without_correct(ori_x, ori_y, m) w0 = np.zeros((m, 1)) x = vandermonde(ori_x, m) _x = vandermonde(test_x, m) A = x.T @ x t = np.mat(ori_y).T b = x.T @ t # 梯度下降求解w w = grad_down(w0, A, b, k, x, t) # 画图 # 计算E_RMS e_rms = get_RMS(get_Ew(_x, w, np.mat(test_y).T), test_size) print('without correct in test data:e_rms=', e_rms) match_plot((ori_x, ori_y), m, n, w, 'grad_down') # 梯度下降求解w,带正则项 la = np.exp(-9) A = x.T @ x + la * np.mat(np.eye(A.shape[0], A.shape[1])) # w0 = np.mat(best_answer_with_correct(ori_x, ori_y, m, la)).T w = grad_down(w0, A, b, k, x, t) # 计算E_RMS e_rms = get_RMS(get_Ew(_x, w, np.mat(test_y).T), test_size) print('with correct in test data:e_rms=', e_rms) # 画图 match_plot((ori_x, ori_y), m, n, w, 'grad_down with correct')
def main(): m = 14 n = 50 test_size = 40 # 产生数据 ori_x, ori_y = generate_data(n) test_x, test_y = generate_data(test_size) # 确定梯度下降初始化内容,不带正则项 w0 = np.zeros((m, 1)) x = vandermonde(ori_x, m) A = x.T @ x b = x.T @ np.mat(ori_y).T # 共轭梯度下降 w = conjugate_down(w0, A, b) # 画图 match_plot((ori_x, ori_y), m, n, w) # 计算E_RMS _x = vandermonde(test_x, m) e_rms = get_RMS(get_Ew(_x, w, np.mat(test_y).T), test_size) print('without correct in test data:e_rms=', e_rms) # 带正则项 la = np.exp(-9) A = x.T @ x + la * np.mat(np.eye(A.shape[0], A.shape[1])) w = conjugate_down(w0, A, b) # 计算E_RMS e_rms = get_RMS(get_Ew(_x, w, np.mat(test_y).T), test_size) print('with correct in test data:e_rms=', e_rms) # 画图 match_plot((ori_x, ori_y), m, n, w, line_name='conjugate_down_with_correct') # 确定最好的lambda la_list = np.array([np.exp(i) for i in range(-15, 0)]) e_rms_train = [] e_rms_test = [] for _la in la_list: A = x.T @ x + _la * np.mat(np.eye(A.shape[0], A.shape[1])) w = conjugate_down(w0, A, b) # 计算E_RMS e_rms_te = get_RMS(get_Ew(_x, w, np.mat(test_y).T), test_size) e_rms_test.append(float(e_rms_te)) e_rms_tr = get_RMS(get_Ew(x, w, np.mat(ori_y).T), n) e_rms_train.append(float(e_rms_tr)) # 画图 e_rms ---- lambda # plt.xlabel('lambda($e^{-x}$)') s = 'train_size=' + str(n) + ',test_size=' + str(test_size) plt.title(s) plt.xlabel('lambda') plt.ylabel('$E_{RMS}$') plt.xscale('log') plt.xticks([np.exp(-i) for i in range(5, 15, 2)], ['$e^{' + str(-i) + '}$' for i in range(5, 15, 2)]) plt.plot(la_list, e_rms_train, color='r', label='train') plt.plot(la_list, e_rms_test, color='b', label='test') plt.legend() plt.show()
def grad_down(w0, A, b, k, x, t): w = np.mat(w0) A = np.mat(A) b = np.mat(b) step_length = 0.001 # 步长 r = np.mat(b - A * w) # 梯度反方向 value1 = get_Ew(x, w, t) # 第二个 value0 = value1 precision = 1e-5 for _ in range(k): w = w + step_length * r value0 = value1 # 前一个 value1 = get_Ew(x, w, t) # 当前的 # 如果二次函数取值变大,则减小步长 if value1 - value0 > 0: step_length = 0.5 * step_length # 跳出条件 当loss足够小时 if value1 < precision: print(_) break r = np.mat(b - A * w) # 梯度反方向 return w
match_plot(ori, m, n, w, "solution with correct") # 确定最好的lambda # 通过画图来寻找近似的E(w)最低的点 绘制 train_RMS-lambda 和 test_RMS-lambda 图像 la_list = [] train_RMS = [] test_RMS = [] for _ in np.arange(-15, 0): la_list.append(np.exp(_)) for lal in la_list: _w = np.array(best_answer_with_correct(ori[0], ori[1], m, lal)) _w = np.reshape(_w, (m, 1)) _x = vandermonde(ori[0], m) _t = np.array(ori[1]).reshape(ori[1].shape[0], 1) train_RMS.append(get_RMS(float(get_Ew(_x, _w, _t)), n)) _x = vandermonde(test_data[0], m) _t = np.array(test_data[1]).reshape(test_data[1].shape[0], 1) test_RMS.append(get_RMS(float(get_Ew(_x, _w, _t)), test_size)) # RMS-lambda图像 plt.xscale('log') plt.xlabel('lambda') plt.ylabel('$E_{RMS}$') plt.xticks([np.exp(-i) for i in range(5, 15, 2)], ['$e^{' + str(-i) + '}$' for i in range(5, 15, 2)]) plt.plot(la_list, train_RMS, color='r', label='train') plt.plot(la_list, test_RMS, color='b', label='test') plt.legend() plt.show()
# 求解 w = best_answer_without_correct(ori[0], ori[1], m) # 拟合曲线 match_plot(ori, m, n, w, 'solution') # 绘制loss-m图像,解释过拟合现象 m-w-E(w) title = 'train_size=' + str(n) + ',test_size=' + str(test_size) plt.title(title) plt.xlabel('m') plt.ylabel('$E_{RMS}$') plt.xlim(0, 11) m_list = np.arange(0, 12) RMS_test = [] RMS_train = [] test_data = generate_data(test_size) test = np.array(test_data[1]).reshape(test_data[1].shape[0], 1) train = np.array(ori[1]).reshape(ori[1].shape[0], 1) for _m in m_list: _w = best_answer_without_correct(ori[0], ori[1], _m + 1) _w = np.reshape(_w, (_m + 1, 1)) _x = x = vandermonde(test_data[0], _m + 1) RMS_test.append(get_RMS(float(get_Ew(_x, _w, test)), test_size)) _x = x = vandermonde(ori[0], _m + 1) RMS_train.append(get_RMS(float(get_Ew(_x, _w, train)), n)) plt.plot(m_list, RMS_test, color='b', label='test set', marker='o') plt.plot(m_list, RMS_train, color='r', label='train set', marker='o') plt.legend() plt.show()