有关梯度函数的文章:
[机器学习] ML重要概念:梯度(Gradient)与梯度下降法(Gradient Descent)
http://blog.csdn.net/walilk/article/details/50978864
深入浅出–梯度下降法及其实现
https://www.jianshu.com/p/c7e642877b0e
梯度下降法的原理与实现
https://blog.csdn.net/dpengwang/article/details/86028041
- 梯度下降算法
#梯度下降
import numpy as np
row = 4
x0 = np.zeros((row,1))
x1 = np.arange(0, row+0).reshape(row, 1)
x2 = np.arange(2, row+2).reshape(row,1)
x3 = np.arange(1, row+1).reshape(row,1)
#x 20行3列
x = np.hstack((x1,x2,x3)) #np.vstack()在竖直方向上堆叠;np.hstack()在水平方向上平铺
#y 20行1列
#y = 1*x1 + 2*x2 + 3*x3 + np.random.random(row).reshape(row, 1) ##np.random.random()生成随机浮点数
y = 1*x1 + 2*x2 + 3*x3 + np.array([2,1,3,1]).reshape(row,1)
print(x)
print(y)
#定义代价函数(误差)
def error_function(theta, x, y): #theta是权重
diff = np.dot(x, theta) - y ##diff是误差值
return (1./2*row)*np.dot(np.transpose(diff), diff) #返回的是误差函数的值
#求梯度的函数
def gradient_function(theta, x, y):
diff = np.dot(x, theta) - y
return (1./row)*np.dot(np.transpose(x), diff) #返回的是梯度函数的值,用来更新权重
#利用梯度下降法不断迭代参数
def gradient_descent(x, y, alpha):
theta = np.array([1, 1, 1]).reshape(3, 1) #初始化权重,权重的行数为X的列数(一个权重对应一个维度)
gradient = gradient_function(theta, x, y)
while not np.all(np.absolut