defbatchGradientDescent(x, y, theta, alpha, m, maxIteration): for i inrange(maxIteration): hypothesis = np.dot(x, theta) loss = hypothesis - y gradient = np.dot(x.transpose(), loss) / m theta = theta - alpha * gradient # 对所有样本求和 return theta
2. SGD
1 2 3 4 5 6 7 8 9 10 11 12 13
defStochasticGradientDescent(x, y, theta, alpha, m, maxIteration): data = [] for i inrange(10): data.append(i) # 这里随便挑选一个进行更新点进行即可(不用想BGD一样全部考虑)
for i inrange(maxIteration): hypothesis = np.dot(x, theta) loss = hypothesis - y # 这里还是有十个样本 index = random.sample(data, 1)[0] # 随机抽取一个样本,得到它的下标 gradient = loss[index] * x[index] # 只取一个点进行更新计算 theta = theta - alpha * gradient.T return theta