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
|
from numpy import * import numpy as np
def compute_error_for_line_given_points(b, m, points): totalError = 0 for i in range(0, len(points)): x = points[i, 0] y = points[i, 1] totalError += (y - (m * x + b)) ** 2 return totalError / float(len(points))
def update_mini_batch(b, m, mini_batch, learningRate): N = float(len(points)) b_gradient,m_gradient = compute_cost_gradient_of_all(mini_batch,b,m) new_b = b - (learningRate/N) * b_gradient new_m = m - (learningRate/N) * m_gradient return [new_b, new_m]
def compute_cost_gradient_of_all(points,b,m): b_gradient = 0 m_gradient = 0 for i in range(0, len(points)): delta_b,delta_m = compute_cost_gradient_of_one(points[i],b,m) b_gradient += delta_b m_gradient += delta_m return [b_gradient,m_gradient]
def compute_cost_gradient_of_one(point,b,m): x = point[0] y = point[1] b_gradient = -2 * (y - ((m * x) + b)) m_gradient = -2 * x * (y - ((m * x) + b)) return [b_gradient,m_gradient]
def gradient_descent_runner(points, starting_b, starting_m, learning_rate, epoches): b = starting_b m = starting_m for i in range(epoches): b, m = update_mini_batch(b, m, array(points), learning_rate) return [b, m]
def gradient_descent_runner_with_mini_batch(points, starting_b, starting_m, learning_rate, epoches,mini_batch_size): b = starting_b m = starting_m n = len(points) num_batches = n/mini_batch_size for i in range(epoches):
np.random.shuffle(points)
for k in xrange(0,num_batches): mini_batch = points[k*mini_batch_size : (k+1)*mini_batch_size] b, m = update_mini_batch(b, m, mini_batch, learning_rate) return [b, m]
def run(): points = genfromtxt("data.csv", delimiter=",") learning_rate = 0.0001 initial_b = 0 initial_m = 0 epoches = 1000 print "Starting gradient descent at b = {0}, m = {1}, error = {2}".format(initial_b, initial_m, compute_error_for_line_given_points(initial_b, initial_m, points)) print "Running..." [b, m] = gradient_descent_runner(points, initial_b, initial_m, learning_rate, epoches) print "After {0} iterations b = {1}, m = {2}, error = {3}".format(epoches, b, m, compute_error_for_line_given_points(b, m, points))
def run2(): points = genfromtxt("data.csv", delimiter=",") learning_rate = 0.0001 initial_b = 0 initial_m = 0 epoches = 1000 mini_batch_size = 10 print "Starting gradient descent at b = {0}, m = {1}, error = {2}".format(initial_b, initial_m, compute_error_for_line_given_points(initial_b, initial_m, points)) print "Running..." [b, m] = gradient_descent_runner_with_mini_batch(points, initial_b, initial_m, learning_rate, epoches,mini_batch_size) print "After {0} iterations b = {1}, m = {2}, error = {3}".format(epoches, b, m, compute_error_for_line_given_points(b, m, points))
if __name__ == '__main__': run()
|