1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| import numpy as np import matplotlib.pyplot as plt %matplotlib inline
def generate_data_1(size): x = np.random.rand(size) y = np.sin(2 * np.pi * x) + np.random.normal(0,0.3,size) return x, y
def generate_data_2(size): x = np.linspace(0,1,size) y = np.sin(2 * np.pi * x) + np.random.normal(0,0.3,size) return x, y
size = 30 x_train, y_train = generate_data_2(size) x_func = np.linspace(0,1,100) y_func = np.sin(2 * np.pi * x_func) plt.plot(x_train,y_train,'.b') plt.plot(x_func,y_func,'-g') plt.show()
def polynomial_X(x, exp): if(exp == 0): return np.ones(x.size) X = np.vstack((np.ones(x.size),x)) for i in range(2, exp+1): X = np.vstack((X, np.power(x, i))) return X.T
def train_analytic(x_train, exp): X = polynomial_X(x_train, exp) if(exp == 0): theta = 1/(X.T.dot(X)) * X.T.dot(y_train) else: theta = np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(y_train) return theta
def predict_analytic(x_train, x_test, exp): theta = train_analytic(x_train, exp) return polynomial_X(x_test, exp).dot(theta)
j = 1 for i in [0, 1, 3, 9]: y_predict = predict_analytic(x_train, x_func, i) plt.subplot(2,2,j) j += 1 plt.plot(x_func, y_predict, '-r') plt.plot(x_train,y_train,'.b') plt.plot(x_func,y_func,'-g') plt.show()
def calculate_erms(y, t): return np.sqrt(np.mean(np.square(y-t)))
x = np.array([0,1,2,3,4,5,6,7,8,9]) training_erms = np.zeros(10) test_erms = np.zeros(10)
theta = [] for i in range(10): theta.append(train_analytic(x_train, i)) y_train_predict = polynomial_X(x_train, i).dot(theta[i]) training_erms[i] = calculate_erms(y_train_predict, y_train) y_test_predict = predict_analytic(x_train, x_func, i) test_erms[i] = calculate_erms(y_test_predict, y_func+np.random.normal(0,0.3,len(y_func)))
plt.plot(x, training_erms, 'o-b', label="Training") plt.plot(x, test_erms, 'o-r', label="Test") plt.legend() plt.xlabel("degree") plt.ylabel("Erms") plt.show()
def train_analytic_with_regularization(x_train, exp, lamb): X = polynomial_X(x_train, exp) if(exp == 0): theta = 1/(X.T.dot(X)+lamb) * X.T.dot(y_train) else: theta = np.linalg.pinv(X.T.dot(X)+lamb*np.eye(X.shape[1])).dot(X.T).dot(y_train) return theta
def predict_analytic_with_regularization(x_train, x_test, exp, lamb): theta = train_analytic_with_regularization(x_train, exp, lamb) return polynomial_X(x_test, exp).dot(theta)
j = 1 lamb = 0.001 for i in [0, 1, 3, 9]: y_predict = predict_analytic_with_regularization(x_train,x_func,i,lamb) plt.subplot(2,2,j) j += 1 plt.plot(x_func,y_predict, '-r') plt.plot(x_train,y_train,'.b') plt.plot(x_func,y_func,'-g') plt.show()
x = np.linspace(-40, 0) training_erms = np.zeros_like(x) test_erms = np.zeros_like(x)
X = polynomial_X(x_train, 9) theta = [] for i,index in zip(x, range(x.size)): theta.append(train_analytic_with_regularization(x_train, 9, np.exp(i))) y_train_predict = X.dot(theta[index]) training_erms[index] = calculate_erms(y_train_predict, y_train) y_test_predict = predict_analytic_with_regularization(x_train, x_func, 9, np.exp(i)) test_erms[index] = calculate_erms(y_test_predict, y_func+np.random.normal(0,0.3,len(y_func)))
plt.plot(x, training_erms, 'o-b', label="Training") plt.plot(x, test_erms, 'o-r', label="Test") plt.legend() plt.xlabel("lnλ") plt.ylabel("Erms") plt.show()
def gradient_descent(X, y, alpha, iterations): if(np.size(X.shape) == 1): theta = 0 else: theta = np.zeros(np.size(X, 1)) for i in range(iterations): theta = theta - alpha * X.T.dot(X.dot(theta) - y) return theta
def gradient_descent_predict(X, y_train, X_test, exp, alpha, iterations): theta = gradient_descent(X, y_train, alpha, iterations) if(exp == 9): print("M=9时,theta=", theta) return X_test.dot(theta)
j = 1 iterations = 1000000
for i in [0, 1, 3, 9]: plt.subplot(2,2,j) j += 1 X = polynomial_X(x_train, i) X_test = polynomial_X(x_func, i) y_predict = gradient_descent_predict(X, y_train, X_test, i, 0.01, iterations) plt.plot(x_func,y_predict,'-r') plt.plot(x_train,y_train,'.b') plt.plot(x_func,y_func,'-g') plt.show()
def conjugate_gradient(X, y): esp = 1e-10 A = X.T.dot(X) b = X.T.dot(y) if (np.size(X.shape) == 1): theta = 0 r = b - A*theta p = r r2 = r*r alpha = r2 / (p * A * p) theta = theta + alpha * p else: theta = np.zeros(np.size(X, 1)) r = b - A.dot(theta) p = r r2 = r.T.dot(r) err = 1 while err > esp: alpha = r2 / (p.T.dot(A).dot(p)) theta = theta + alpha * p r = r - alpha * A.dot(p) r2_new = r.T.dot(r) err = np.sqrt(r2_new) beta = r2_new / r2 p = r + beta * p r2 = r2_new return theta
def conjugate_gradient_predict(X_train, y_train, X_test): theta = conjugate_gradient(X_train, y_train) return X_test.dot(theta)
j = 1 for i in [0, 1, 3, 9]: plt.subplot(2,2,j) j += 1 y_predict = conjugate_gradient_predict(polynomial_X(x_train, i), y_train, polynomial_X(x_func, i)) plt.plot(x_func,y_predict,'-r') plt.plot(x_train,y_train,'.b') plt.plot(x_func,y_func,'-g') plt.show()
|