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
| import numpy as np
def load_data(): data = [] for i in range(100): x = np.random.uniform(-10., 10.) eps = np.random.normal(0., 0.01) y = 1.477 * x + 0.089 + eps data.append([x, y]) data = np.array(data) return data
def mse(w, b, points): totalError = 0 for i in range(0, len(points)): x = points[i, 0] y = points[i, 1] totalError += (y - (w * x + b)) ** 2 return totalError / float(len(points))
def step_gradient(w_curr, b_curr, points, lr): b_gradient = 0 w_gradient = 0 M = float(len(points)) for i in range(0, len(points)): x = points[i, 0] y = points[i, 1] b_gradient += (2/M) * ((w_curr * x + b_curr) - y) w_gradient += (2/M) * x * ((w_curr * x + b_curr) - y) new_w = w_curr - (lr * w_gradient) new_b = b_curr - (lr * b_gradient) return [new_w, new_b]
def gradient_descent(points, starting_w, starting_b, lr, num_iter): w = starting_w b = starting_b for step in range(num_iter): w, b = step_gradient(w, b, np.array(points), lr) loss = mse(b, w, points) if step % 50 == 0: print(step, loss, w, b) return [b, w]
def main(): lr = 0.01 init_b = 0 init_w = 0 num_iterations = 1000 data = load_data()
[w, b] = gradient_descent(data, init_b, init_w, lr, num_iterations)
loss = mse(w, b, data) print('final', loss, w, b)
if __name__ == '__main__': main()
|