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
|
import numpy as np import matplotlib.pyplot as plt import datetime rng = np.random.RandomState(1) x = 10 * rng.rand(500) y = 3 * x + 2 + rng.randn(500)
def batch_gradient_descent(x, y, learn_rate, epoches): """ :param x: 输入的x :param y: 输入的y :param learn_rate: 学习率 :param epoches: 迭代次数 :return: """ start_time = datetime.datetime.now() theta = np.array([0.0, 0.0]) for i in range(epoches): loss = [0.0, 0.0] m = len(y) for j in range(m): loss[0] = loss[0] + (theta[0] * x[j, 0] + theta[1] * x[j, 1] - y[j]) * x[j, 0] / m loss[1] = loss[1] + (theta[0] * x[j, 0] + theta[1] * x[j, 1] - y[j]) / m theta[0] = theta[0] - learn_rate * loss[0] theta[1] = theta[1] - learn_rate * loss[1] end_time = datetime.datetime.now() return end_time - start_time, theta
def stochastic_gradient_descent_false(x, y, learn_rate, epoches, stochastic_rate): """ :param x: 输入的x :param y: 输入的y :param learn_rate: 学习率 :param epoches: 迭代次数 :return: """ start_time = datetime.datetime.now() shufflle_data = np.column_stack((y, x)) np.random.shuffle(shufflle_data) stochastic_count = int(len(y) * stochastic_rate) y = shufflle_data[:stochastic_count, 0] x = shufflle_data[:stochastic_count, 1:3] end_time = datetime.datetime.now() return end_time - start_time, batch_gradient_descent(x, y, learn_rate, epoches)
def stochastic_gradient_descent_true(x, y, learn_rate, epoches, stochastic_rate): """ :param x: 输入的x :param y: 输入的y :param learn_rate: 学习率 :param epoches: 迭代次数 :return: """ start_time = datetime.datetime.now() theta = np.array([0.0, 0.0]) for i in range(epoches): shufflle_data = np.column_stack((y, x)) np.random.shuffle(shufflle_data) stochastic_count = int(len(y) * stochastic_rate) y = shufflle_data[:stochastic_count, 0] x = shufflle_data[:stochastic_count, 1:3] loss = [0.0, 0.0] m = len(y) for j in range(m): loss[0] = loss[0] + (theta[0] * x[j, 0] + theta[1] * x[j, 1] - y[j]) * x[j, 0] / m loss[1] = loss[1] + (theta[0] * x[j, 0] + theta[1] * x[j, 1] - y[j]) / m theta[0] = theta[0] - learn_rate * loss[0] theta[1] = theta[1] - learn_rate * loss[1] end_time = datetime.datetime.now() return end_time - start_time, theta
def mini_batch_gradient_descent(x, y, learn_rate, epoches, mini_length): """ :param x: 输入的x :param y: 输入的y :param learn_rate: 学习率 :param epoches: 迭代次数 :param mini_length: mini batch length :return: """ start_time = datetime.datetime.now() theta = np.array([0.0, 0.0]) shufflle_data = np.column_stack((y, x)) np.random.shuffle(shufflle_data) y = shufflle_data[:, 0] x = shufflle_data[:, 1:3] for i in range(epoches): loss = [0.0, 0.0] for j in range(0, len(y), mini_length): loss[0] = loss[0] + (theta[0] * x[j, 0] + theta[1] * x[j, 1] - y[j]) * x[j, 0] / mini_length loss[1] = loss[1] + (theta[0] * x[j, 0] + theta[1] * x[j, 1] - y[j]) / mini_length theta[0] = theta[0] - learn_rate * loss[0] theta[1] = theta[1] - learn_rate * loss[1] end_time = datetime.datetime.now() return end_time - start_time, theta def contro_func(func, **kwargs): """ :param func: 函数 :param kwargs: func 中需要的参数 :return: """ x = kwargs.get('x', None) y = kwargs.get('y', None) learn_rate = kwargs.get('learn_rate', None) epoches = kwargs.get('epoches', None) stochastic_rate = kwargs.get('stochastic_rate', None) mini_length = kwargs.get('mini_length', None) if stochastic_rate is not None: return func(x, y, learn_rate, epoches, stochastic_rate) if mini_length is not None: return func(x, y, learn_rate, epoches, mini_length) return func(x, y, learn_rate, epoches) def show_trend(): rng = np.random.RandomState(1) x = 10 * rng.rand(500) x = np.array([x, np.ones(500)]).T y = 3 * x + 2 + rng.randn(500) learn_rate = 0.01 stochastic_rate = 0.4 mini_length = 10 for j in [batch_gradient_descent, stochastic_gradient_descent_false, stochastic_gradient_descent_true, mini_batch_gradient_descent]: tmp = [] for epoches in [1, 10, 100, 1000, 10000, 100000]: tmp.append(contro_func(i, x=x, y=y, learn_rate=learn_rate, stochastic_rate=stochastic_rate, mini_length=mini_length, epoches=epoches)) if __name__ == '__main__':
|