Python3入门机器学习 - 梯度下降法

简介: 梯度下降是迭代法的一种,可以用于求解最小二乘问题(线性和非线性都可以)。在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法。

梯度下降是迭代法的一种,可以用于求解最小二乘问题(线性和非线性都可以)。在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法。在求解损失函数的最小值时,可以通过梯度下降法来一步步的迭代求解,得到最小化的损失函数和模型参数值。

模拟实现梯度下降法


def DJ(theta):      //计算损失函数J的斜率
    return 2*(theta-2.5)
def J(theta):        //损失函数J,使用梯度下降法 求该函数极小值
    return (theta-2.5)**2+1
theta = 0.0
eta = 0.1
epsilon = 1e-8
theta_history = [theta]

while True:
    gradient = DJ(theta)
    last_theta = theta
    theta = theta - eta*gradient
    theta_history.append(theta)
    if(abs(J(theta) - J(last_theta))<epsilon):
        break

pyplot.plot(plot_x,plot_y)
pyplot.plot(np.array(theta_history),J(np.array(theta_history)),color='r',marker='+')
img_acdd1700a150b18d152f8c8c784c1a89.png
梯度下降法应用于线性回归算法
    def fit_gd(self,X_train,y_train,eta=0.01,n_iters=1e6):
        def J(theta,X_b,y):
            try:
                return np.sum((y-X_b.dot(theta))**2)/len(y)
            except:
                return float("inf")
        def dJ(theta,X_b,y):
            # res = np.empty()
            # res[0] = np.sum(X_b.dot(theta)-y)
            # for i in range(1,len(theta)):
            #     res[i] = (X_b.dot(theta)-y).dot(X_b[:,i])
            # return res * 2 / len(X_b)
            return X_b.T.dot(X_b.dot(theta)-y)*2./len(X_b)
        def gradient_descent(X_b,y,initial_theta,eta,n_iters=1e6,epsilon=1e-8):
            theta = initial_theta
            cur_iter = 0
            while cur_iter<n_iters:
                gradient = dJ(theta,X_b,y)
                last_theta = theta
                theta = theta - eta * gradient
                if (abs(J(theta,X_b,y) - J(last_theta,X_b,y)) < epsilon):
                    break
                cur_iter+=1
            return theta
        X_b = np.hstack([np.ones((len(X_train),1)),X_train])
        initial_theta = np.zeros(X_b.shape[1])
        self._theta = gradient_descent(X_b,y_train,initial_theta,eta,n_iters)
        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]
        return self


随机梯度下降法


随机梯度下降法是在矩阵X_b中任选一行进行梯度下降,基于这种思想,每次下降具有很大的随机性,甚至损失函数有可能变大,但根据经验,发现这种方法也可以较好的计算出最佳的损失函数值。

img_6780521ac5905045a3337dc2faa658da.png
随机梯度下降法的超参数(模拟退火)

由于随机梯度下降法的不确定性,因此eta值需要根据每次递归的过程递减,图示即为常用的eta值递减方案。

def dJ_sgd(theta,X_b_i,y_i):
    return X_b_i.T.dot(X_b_i.dot(theta)-y_i)*2.

def sgd(X_b,y, initial_theta,n_iters):
    t0 = 5.0
    t1 = 50.0
    
    def learning_theta(t):
        return t0/(t1+t)
    
    theta = initial_theta
    for cur_iter in range(n_iters):
        rand_i = np.random.randint(len(X_b))
        gradient = dJ_sgd(theta,X_b[rand_i],y[rand_i])
        theta = theta-learning_theta(cur_iter) * gradient
    return theta
使用sklearn中的随机梯度下降法
from sklearn.linear_model import SGDRegressor

sgd = SGDRegressor(n_iter=1000)
sgd.fit(X_train_standard,y_train)
sgd.score(X_test_standard,y_test)


梯度下降法的DEBUG


一般来说,梯度下降法需要对损失函数进行数学推导出他的导函数,但我们如何得知推导过程是否正确,或者说导函数是否正确呢,我们可以使用以下方法进行验证

def dJ_debug(theta,X_b,y,epslion=0.01):
    res = np.empty(len(theta))
    for i in range(len(theta)):
        theta_1 = theta.copy()
        theta_1[i] += epslion
        theta_2 = theta.copy()
        theta_2[i] -= epslion
        res[i] = (J(theta_1,X_b,y)-J(theta_2,X_b,y)/(2*epslion))
    return res
img_b4be7e79c3fc76e278edd4e97d455abf.png
使用两个蓝点的斜率来替代红点的斜率,验证斜率是否正确
目录
相关文章
|
2月前
|
机器学习/深度学习 传感器 运维
使用机器学习技术进行时间序列缺失数据填充:基础方法与入门案例
本文探讨了时间序列分析中数据缺失的问题,并通过实际案例展示了如何利用机器学习技术进行缺失值补充。文章构建了一个模拟的能源生产数据集,采用线性回归和决策树回归两种方法进行缺失值补充,并从统计特征、自相关性、趋势和季节性等多个维度进行了详细评估。结果显示,决策树方法在处理复杂非线性模式和保持数据局部特征方面表现更佳,而线性回归方法则适用于简单的线性趋势数据。文章最后总结了两种方法的优劣,并给出了实际应用建议。
128 7
使用机器学习技术进行时间序列缺失数据填充:基础方法与入门案例
|
3月前
|
机器学习/深度学习 数据采集
机器学习入门——使用Scikit-Learn构建分类器
机器学习入门——使用Scikit-Learn构建分类器
|
3月前
|
机器学习/深度学习 数据采集 数据可视化
Python数据科学实战:从Pandas到机器学习
Python数据科学实战:从Pandas到机器学习
|
3月前
|
机器学习/深度学习 数据可视化 数据处理
掌握Python数据科学基础——从数据处理到机器学习
掌握Python数据科学基础——从数据处理到机器学习
69 0
|
3月前
|
机器学习/深度学习 数据采集 人工智能
机器学习入门:Python与scikit-learn实战
机器学习入门:Python与scikit-learn实战
102 0
|
3月前
|
机器学习/深度学习 数据采集 数据挖掘
Python在数据科学中的应用:从数据处理到模型训练
Python在数据科学中的应用:从数据处理到模型训练
|
3月前
|
机器学习/深度学习 算法 Python
机器学习入门:理解并实现K-近邻算法
机器学习入门:理解并实现K-近邻算法
56 0
|
9月前
|
机器学习/深度学习 存储 搜索推荐
利用机器学习算法改善电商推荐系统的效率
电商行业日益竞争激烈,提升用户体验成为关键。本文将探讨如何利用机器学习算法优化电商推荐系统,通过分析用户行为数据和商品信息,实现个性化推荐,从而提高推荐效率和准确性。
278 14
|
9月前
|
机器学习/深度学习 算法 数据可视化
实现机器学习算法时,特征选择是非常重要的一步,你有哪些推荐的方法?
实现机器学习算法时,特征选择是非常重要的一步,你有哪些推荐的方法?
166 1
|
9月前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)

热门文章

最新文章