机器学习算法之梯度下降法

机器学习之重要迭代算法梯度下降法 讲解了机器学习中的重要算法梯度下降法,用来求解无约束最优化问题的最小值问题。 阅读详情

梯度下降法

  • 1.梯度下降法简介
  • 2.简单线性回归中梯度下降法的模拟
  • 3.多元线性回归中的梯度下降法
  • 4.随机梯度下降法
  • 5.波士顿房价预测问题

一、梯度下降法简介

  • 不是一个机器学习算法
  • 是一个基于搜索的最优化方法
  • 作用:最小化损失函数
  • 梯度上升法:最大化一个效用函数

以下是定义了一个损失函数以后,参数theta对应的损失函数J的值对应的示例图,我们需要找到一个使得损失函数值J取得最小值的对应的theta(选用二维平面,即参数只有一个)

1.η

a.η的介绍

  • η称为学习率(learnng rate)
  • η的取值影响获得最优解的速度
  • η取值不合适,甚至得不到最优解
  • η是梯度下降法的一个超参数

b.η取值太小,影响收敛学习速度

c.η太大,甚至导致不收敛

2.注意事项

并不是所有的函数都有唯一的极值点

在这里插入图片描述
解决方案:

  • 多次运行,随机化初始点
  • 梯度下降法的初始点也是一个超参数

二、简单线性回归中梯度下降法的模拟

1.绘制一个简单的损失函数

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# 简单模拟一个损失函数
plot_x = np.linspace(-1,6,144)
plot_y = (plot_x-2.5)**2-1
plt.plot(plot_x,plot_y)
[<matplotlib.lines.Line2D at 0x176fa5c2dd8>]

在这里插入图片描述

2.定义梯度下降函数

def dJ(theta):
    """损失函数的导数"""
    return 2*(theta-2.5)

def J(theta):
    """损失函数"""
    try:
        return (theta-2.5)**2-1
    except:
        return float("inf")

def gradient_descent(initial_theta,eta,n_iters=1e4,epsilon=1e-8):
    """梯度下降法封装"""
    """
    initial_theta:初始化的theta值
    eta:学习率
    n_iters:最大循环次数
    epsilon:精度
    """
    theta = initial_theta
    theta_history.append(initial_theta) # 保存theta的变化值
    i_iters = 0
    
    while i_iters < n_iters:
        """
        如果theta两次变化之间的损失函数值的变化小于我们定义的精度
        则可以说明我们已经找到了最低的损失函数值和对应的theta
        
        如果循环次数超过了我们设置的循环次数,
        则说明可能由于η设置的过大导致无止境的循环
        """
        gradient = dJ(theta)  # 导数
        last_theta = theta    # 保存上一步的theta
        theta = theta-eta*gradient # 新的theta
        theta_history.append(theta)
        
        # 判断是否小于精度
        if (abs(J(theta)-J(last_theta))<epsilon):
            break
        i_iters += 1
        
def plot_theta_history():
    plt.plot(plot_x,J(plot_x))
    plt.plot(np.array(theta_history),J(np.array(theta_history)),color="r",marker="+")
    plt.show()
    print("the size of theta_history is %d"%len(theta_history))

3.使用不同的η学习率进行测试

a.η=0.1

eta = 0.1
theta_history = []
gradient_descent(0.,eta)
plot_theta_history()

在这里插入图片描述

the size of theta_history is 46

b.η=0.01

eta = 0.01
theta_history = []
gradient_descent(0.,eta)
plot_theta_history()

在这里插入图片描述

the size of theta_history is 424

c.η=0.001

eta = 0.001
theta_history = []
gradient_descent(0.,eta)
plot_theta_history()

在这里插入图片描述

the size of theta_history is 3682

d.η=0.8

eta = 0.8
theta_history = []
gradient_descent(0.,eta)
plot_theta_history()

在这里插入图片描述

the size of theta_history is 22

e.η=1.1

eta = 1.1
theta_history = []
gradient_descent(0.,eta,n_iters=10)
plot_theta_history()

在这里插入图片描述

the size of theta_history is 11

三、多元线性回归中的梯度下降法

1.模拟数据的准备

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(666)
x = 2*np.random.random(size=100)
y = x*3.+4+np.random.normal(size=100)
X = x.reshape(-1,1)
plt.scatter(X,y)
<matplotlib.collections.PathCollection at 0x176fa87e390>

在这里插入图片描述

2.梯度下降法的实现

a.简单的直接实现

class LinearRegression:

    def __init__(self):
        """初始化Simple Linear Regression模型"""
        self.a_ = None
        self.b_ = None

    def fit(self, x_train, y_train):
        """根据训练数据集x_train,y_train训练Simple Linear Regression模型"""
        assert x_train.ndim == 1, \
            "Simple Linear Regressor can only solve single feature training data."
        assert len(x_train) == len(y_train), \
            "the size of x_train must be equal to the size of y_train"
        
        # 均值
        x_mean = np.mean(x_train)
        y_mean = np.mean(y_train)
        
        # 使用向量化点乘计算参数a和b
        self.a_ = (x_train - x_mean).dot(y_train - y_mean) / (x_train - x_mean).dot(x_train - x_mean)
        self.b_ = y_mean - self.a_ * x_mean

        return self

    def predict(self, x_predict):
        """给定待预测数据集x_predict,返回表示x_predict的结果向量"""
        assert x_predict.ndim == 1, \
            "Simple Linear Regressor can only solve single feature training data."
        assert self.a_ is not None and self.b_ is not None, \
            "must fit before predict!"

        return np.array([self._predict(x) for x in x_predict])

    def _predict(self, x_single):
        """给定单个待预测数据x_single,返回x_single的预测结果值"""
        return self.a_ * x_single + self.b_

    def __repr__(self):
        return "LinearRegression()"
    
    def fit_gd(self, X_train, y_train, eta=0.01, n_iters = 1e4):
        """根据训练数据集X_train,y_train, 使用梯度下降法训练Linear Regression 模型"""
        assert X_train.shape[0] == y_train.shape[0], \
            "the size of X_train must be equal to the size of y_train"

        def J(theta, X_b, y):
            try:
                return np.sum((y - X_b.dot(theta))**2) / len(X_b)
            except:
                return float('inf')

        def dJ(theta, X_b, y):
            res = np.empty(len(theta))
            res[0] = np.sum(X_b.dot(theta) - y)

            for i in range(1, len(theta)):
                res[i] = np.sum((X_b.dot(theta) - y).dot(X_b[:, i]))

            return res * 2 / len(X_b)

        def gradient_descent(X_b, y, initial_theta, eta, n_iters=n_iters, epsilon=1e-8):
            """
            梯度下降法封装
            X_b: X特征矩阵
            y: 结果向量
            initial_theta:初始化的theta值
            eta:学习率η
            n_iters: 最大循环次数
            epsilon: 精度
            """
            theta = initial_theta
            i_iters = 0

            while i_iters < n_iters:
                """
                如果theta两次变化之间的损失函数值的变化小于我们定义的精度
                则可以说明我们已经找到了最低的损失函数值和对应的theta
                
                如果循环次数超过了我们设置的循环次数,
                则说明可能由于η设置的过大导致无止境的循环
                """
                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

                i_iters += 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)

        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]

        return self
lin_reg = LinearRegression()
lin_reg.fit_gd(X,y)
# 查看系数
print(lin_reg.coef_)
# 查看截距
print(lin_reg.interception_)
[3.00706277]
4.021457858204859

b.向量化的处理

import numpy as np
from math import sqrt


def accuracy_score(y_true, y_predict):
    """计算y_true和y_predict之间的准确率"""
    assert len(y_true) == len(y_predict), \
        "the size of y_true must be equal to the size of y_predict"

    return np.sum(y_true == y_predict) / len(y_true)


def mean_squared_error(y_true, y_predict):
    """计算y_true和y_predict之间的MSE"""
    assert len(y_true) == len(y_predict), \
        "the size of y_true must be equal to the size of y_predict"

    return np.sum((y_true - y_predict)**2) / len(y_true)


def root_mean_squared_error(y_true, y_predict):
    """计算y_true和y_predict之间的RMSE"""

    return sqrt(mean_squared_error(y_true, y_predict))


def mean_absolute_error(y_true, y_predict):
    """计算y_true和y_predict之间的MAE"""
    assert len(y_true) == len(y_predict), \
        "the size of y_true must be equal to the size of y_predict"

    return np.sum(np.absolute(y_true - y_predict)) / len(y_true)


def r2_score(y_true, y_predict):
    """计算y_true和y_predict之间的R Square"""

    return 1 - mean_squared_error(y_true, y_predict)/np.var(y_true)
class LinearRegression:

    def __init__(self):
        """初始化Linear Regression模型"""
        self.coef_ = None
        self.intercept_ = None
        self._theta = None

    def fit_normal(self, X_train, y_train):
        """根据训练数据集X_train, y_train训练Linear Regression模型"""
        assert X_train.shape[0] == y_train.shape[0], \
            "the size of X_train must be equal to the size of y_train"

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        self._theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)

        self.intercept_ = self._theta[0]
        self.coef_ = self._theta[1:]

        return self

    def fit_gd(self, X_train, y_train, eta=0.01, n_iters=1e4):
        """根据训练数据集X_train, y_train, 使用梯度下降法训练Linear Regression模型"""
        assert X_train.shape[0] == y_train.shape[0], \
            "the size of X_train must be equal to the size of y_train"

        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):
            return X_b.T.dot(X_b.dot(theta) - y) * 2. / len(y)

        def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, 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.intercept_ = self._theta[0]
        self.coef_ = self._theta[1:]

        return self

    def predict(self, X_predict):
        """给定待预测数据集X_predict,返回表示X_predict的结果向量"""
        assert self.intercept_ is not None and self.coef_ is not None, \
            "must fit before predict!"
        assert X_predict.shape[1] == len(self.coef_), \
            "the feature number of X_predict must be equal to X_train"

        X_b = np.hstack([np.ones((len(X_predict), 1)), X_predict])
        return X_b.dot(self._theta)
    def accuracy_score(y_true, y_predict):
        """计算y_true和y_predict之间的准确率"""
        assert len(y_true) == len(y_predict), \
            "the size of y_true must be equal to the size of y_predict"

        return np.sum(y_true == y_predict) / len(y_true)


    def score(self, X_test, y_test):
        """根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""

        y_predict = self.predict(X_test)
        return r2_score(y_test, y_predict)

    def __repr__(self):
        return "LinearRegression()"

lin_reg = LinearRegression()
lin_reg.fit_gd(X,y)
print(lin_reg.coef_)
print(lin_reg.intercept_)
[3.00706277]
4.021457858204859

3.波士顿房价预测

a.准备数据集

from sklearn import datasets
from sklearn.model_selection import train_test_split
boston = datasets.load_boston()

x = boston.data
y = boston.target

x = x[y<50.0]
y = y[y<50.0]

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=666)

b.数据归一化

由于数据的规模在不同的特征上不同,所以需要对数据进行归一化

from sklearn.preprocessing import StandardScaler

standardScaler = StandardScaler()
standardScaler.fit(x_train)

x_train_standard = standardScaler.transform(x_train)
x_test_standard = standardScaler.transform(x_test)

c.调用梯度下降法

lin_reg = LinearRegression()
lin_reg.fit_gd(x_train_standard,y_train)
lin_reg.score(x_test_standard,y_test)
0.8129873310487505

然而,如果样本数非常多的情况下,那么即使使用梯度下降法也会导致速度非常慢,因为在梯度下降法中,每一个样本都需要运算,这时候就需要随机梯度下降法。

四、随机梯度下降法

1.随机梯度下降法介绍

a.批量梯度下降法

批量梯度下降法带来的一个问题是η的值需要设置的比较小,在样本数比较多的时候导致不是速度特别慢,这时候观察随机梯度下降法损失函数的求导公式,可以发现,我们对每一个Xb都做了求和操作,又在最外面除以了m,那么可以考虑将求和和除以m的两个运算约掉,采用每次使用一个随机的Xb

b.随机梯度下降法

由于我们使用的是随机梯度下降法,所以导致我们的最终结果不会像批量梯度下降法一样准确的朝着一个方向运算,而是曲线行下降,这时候我们就希望,越到下面,η值相应减小,事运算次数变多,从而精确计算结果

2.模拟数据进行测试

a.数据准备:

import numpy as np
import matplotlib.pyplot as plt

m = 100000
x = np.random.normal(size=m)
y = 4.*x+3.+np.random.normal(0,3,size=m)
X = x.reshape(-1,1)

b.批量随机梯度法:

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):
    return X_b.T.dot(X_b.dot(theta) - y) * 2. / len(y)

def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, 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
%%time
X_b = np.hstack([np.ones((len(X),1)),X])
initial_theta = np.zeros(X_b.shape[1])
eta = 0.01
theta = gradient_descent(X_b,y,initial_theta,eta)
Wall time: 1.27 s
print(theta)
[3.00590902 4.00776602]

c.随机梯度下降法:

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
    t1 = 50
    
    def learning_rate(t):
        return t0 / (t + t1)
    
    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_rate(cur_iter) * gradient
    return theta
%%time
X_b = np.hstack([np.ones((len(X), 1)), X])
initial_theta = np.zeros(X_b.shape[1])
theta = sgd(X_b, y, initial_theta, n_iters=m//3)
Wall time: 325 ms

3.对随机梯度函数进行封装

class LinearRegression():
    def fit_sgd(self, X_train, y_train, n_iters=5, t0=5, t1=50):
        """
        根据训练数据集X_train, y_train, 使用随机梯度下降法训练Linear Regression模型
        :param X_train:
        :param y_train:
        :param n_iters: 在随机梯度下降法中,n_iters代表所有的样本会被循环几次
        :param t0:
        :param t1:
        :return:
        """
        assert X_train.shape[0] == y_train.shape[0], \
            "the size of X_train must be equal to the size of y_train"
        assert n_iters >= 1

        def dJ_sgd(theta, X_b_i, y_i):
            """
            X_b,y 中的随机一个元素进行导数公式的计算
            :param theta:
            :param X_b_i:
            :param y_i:
            :return:
            """
            return X_b_i * (X_b_i.dot(theta) - y_i) * 2.

        def sgd(X_b, y, initial_theta, n_iters, t0=5, t1=50):

            def learning_rate(t):
                """
                计算学习率,t1 为了减慢变化速度,t0为了增加随机性
                :param t: 第t次循环
                :return:
                """
                return t0 / (t + t1)

            theta = initial_theta
            m = len(X_b)

            for cur_iter in range(n_iters):
                # 对X_b进行一个乱序的排序
                indexes = np.random.permutation(m)
                X_b_new = X_b[indexes]
                y_new = y[indexes]

                # 对整个数据集看一遍
                for i in range(m):
                    gradient = dJ_sgd(theta, X_b_new[i], y_new[i])
                    theta = theta - learning_rate(cur_iter * m + i) * gradient

            return theta

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        initial_theta = np.random.randn(X_b.shape[1])
        self._theta = sgd(X_b, y_train, initial_theta, n_iters, t0, t1)

        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]

        return self

五、波士顿房价预测问题

1.数据集获取

from sklearn import datasets

boston = datasets.load_boston()
x = boston.data
y = boston.target

x = x[y<50.0]
y = y[y<50.0]

2.数据集的划分

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=666)

3.数据集归一化处理

from sklearn.preprocessing import StandardScaler
standardScaler = StandardScaler()
standardScaler.fit(x_train)
x_train_standard = standardScaler.transform(x_train)
x_test_standard = standardScaler.transform(x_test)

4.调用SGD方法

from sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor(n_iter=100)
%%time
sgd_reg.fit(x_train_standard,y_train)
Wall time: 5.98 ms


D:\software\Anaconda\workplace\lib\site-packages\sklearn\linear_model\stochastic_gradient.py:152: DeprecationWarning: n_iter parameter is deprecated in 0.19 and will be removed in 0.21. Use max_iter and tol instead.
  DeprecationWarning)





SGDRegressor(alpha=0.0001, average=False, early_stopping=False, epsilon=0.1,
       eta0=0.01, fit_intercept=True, l1_ratio=0.15,
       learning_rate='invscaling', loss='squared_loss', max_iter=None,
       n_iter=100, n_iter_no_change=5, penalty='l2', power_t=0.25,
       random_state=None, shuffle=True, tol=None, validation_fraction=0.1,
       verbose=0, warm_start=False)
sgd_reg.score(x_test_standard,y_test)
0.7997484818787682
# 打印theta参数
print(sgd_reg.coef_)
# 打印截距
print(sgd_reg.intercept_)
[-0.972382    0.6864189  -0.34498804 -0.0125929  -1.26078593  2.27274023
 -0.40157318 -2.30498603  1.96081398 -1.83022108 -1.83893614  0.74687792
 -2.81437963]
[21.53228681]

总结

在这里插入图片描述
综合二者的优缺点,出现了小批量梯度下降法.

小批量梯度下降法:我们每一次不看全部样本那么多,也不是只看一次样本那么少,每次只看k个样本。

def fit_lit_sgd(self, X_train, y_train, n_iters=5, t0=5, t1=50,k=10):
        """
        根据训练数据集X_train, y_train, 使用随机梯度下降法训练Linear Regression模型
        :param X_train:
        :param y_train:
        :param n_iters: 在随机梯度下降法中,n_iters代表所有的样本会被看几圈
        :param t0:
        :param t1:
        :param k: 小批量随机下降法的超参数k
        :return:
        """
        assert X_train.shape[0] == y_train.shape[0], \
            "the size of X_train must be equal to the size of y_train"
        assert n_iters >= 1

        def dJ_sgd(theta, X_b_k, y_k):
            """
            去X_b,y 中的随机选择k个元素进行导数公式的计算
            :param theta:
            :param X_b_i:
            :param y_i:
            :return:
            """
            return np.sum((X_b_k * (X_b_k.dot(theta) - y_k) ))* 2/len(X_b_k)

        def sgd(X_b, y, initial_theta, n_iters, t0=5, t1=50):

            def learning_rate(t):
                """
                计算学习率,t1 为了减慢变化速度,t0为了增加随机性
                :param t: 第t次循环
                :return:
                """
                return t0 / (t + t1)

            theta = initial_theta
            m = len(X_b)

            for cur_iter in range(n_iters):
                # 每次看k个元素
                i =0
                while i < m:
                    X_b_new = X_b[i:i+k]
                    y_new = y[i:i+k]
                    gradient = dJ_sgd(theta, X_b_new, y_new)
                    theta = theta - learning_rate(cur_iter * m + i+k) * gradient
                    i = i+k

            return theta

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        initial_theta = np.random.randn(X_b.shape[1])
        self._theta = sgd(X_b, y_train, initial_theta, n_iters, t0, t1)

        self.interception_ = self._theta[0]
        self.coef_ = self._theta[1:]

        return self
机器学习经典算法:利用梯度下降法求解线性回归参数 梯度下降法通过迭代更新线性回归模型的参数,沿着误差函数的梯度反方向逐步最小化损失,直到收敛到最优解。 阅读详情

相关推荐

机器学习算法之----SVM

SVM代码实例 本博客重在讲解SVM原理,以及其具体实现方式。 What’s SVM? Support Vector Machine (SVM) is a supervised machine learning algorithm which can be used for both classification or regression. However, it is mostly used...

weixin_41697507的博客 1117

00_00 python机器学习_各章实例代码汇总(随学习进度更新)

第一章 001 散点图 + K邻近算法 + 模型训练 # 多维数组基础包 import numpy as np # 数据表格 import pandas as pd # 颜色方案 import mglearn # 绘图 import matplotlib.pyplot as pt # 机器学习 数据模型 from sklearn.datasets import load_iris # 机器学习 数据分离 from sklearn.model_selection import train_test_split

辛欣的博客 3299

梯度下降法:机器学习优化算法入门

梯度下降法是机器学习中用来优化模型参数的基本算法,其核心思想是通过迭代的方式逐步逼近函数的最小值点。在理解该算法之前,我们首先需要掌握优化问题的基本概念和目标函数的选择。

weixin_42518334的博客 1184

复现iwgo-svr过程出现的问题

记录学习过程

m0_46210763的博客 1335

基因组注释(Annotation)

Annotation

hgz2020的博客 6495

大模型训练显存瘦身全记录:从ZeRO-3参数分区到CPU Offload的硬件协奏曲

本文详细解析了如何通过ZeRO-3参数分区与CPU/NVMe卸载技术优化大模型训练的显存使用。结合bf16混合精度与梯度累积等策略,实现在消费级GPU上高效训练百亿参数模型,显著提升硬件利用率与训练效率。

weixin_29051245的博客 435

python做深度学习(四)

Python接口操作caffe 可以直接在一个文件夹下面写 python可以直接写层,这个后面再写 python也可以直接写solver文件,将所有的caffe 用到的文件写在一个python文件中 from  caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() 这样,就将solver封装到变量s中,可以操作s得到

Soleilhao的博客 545

戴维南和诺顿定理到底怎么选?一个实际维修案例讲透两种等效电路用法

本文通过工业电源模块维修案例,详细解析戴维南和诺顿定理在实际电路分析中的选择策略。针对电压敏感型故障、电流分配问题及功率传输异常,提供三大黄金法则,帮助工程师高效选择等效电路方法,提升故障排查效率。特别强调诺顿定理在电流分析中的优势。

weixin_31854867的博客 358

深度学习实战 Pycharm 代码调试报错集合

1 YOLO5 pip install -r requirements.txt安装在pycharm上安装失败问题解答 2 pycocotools安装报错解决 3. TimeoutError: [WinError 10060] 4.pycharm中: OSError: [WinError 1455] 页面文件太小 6.CUDA out of memory 解决办法 8.visual studio 10C:\Users\LY\Documents\WindowsPowerShell\profile.ps1,

qq_41134483的博客 7583

systemView的安装使用

systemView的安装使用

qq_33712422的博客 3978

D19: OpenClaw 安全性配置指南

在 OpenClaw 的 30 天探索旅程中,我们已经探讨了邮件自动化(D18)。今天进入安全专题,我将分享一个至关重要的话题:OpenClaw 安全性配置指南。 随着 OpenClaw 深入工作流核心,它掌握了越来越多的敏感信息: API Keys(GitHub、飞书、Notion、Jira...) 企业数据(代码库、文档、聊天记录) 个人数据(日历、邮件、任务) 系统权限(文件读写、命令执行、网络访问) 安全不是可选项,而是必选项。一次配置失误可能导致: API Key 泄露 → 账号被盗用权限

刘一说的IT专栏 203

机器学习最常用的优化算法 — 梯度下降法

1. 学习目标 每一个机器学习模型都有一个目标函数,而学习的目标,就是最小化目标函数。直观而言,当我们已经获得了一个函数,最小化该函数其实就是,在其自变量取值范围内,找到使得因变量最小的那个自变量取值点。 经典机器学习模型的目标函数都是凸函数,函数的凸性保证了其有最小值。 2. 凸函数 什么叫做凸函数?这个有一套严格的数学定义:某个向量空间的凸子集(区间)上的实值函数,如果在其定义域上的任意两点...

wohu1104的专栏 3281

Adams Car——Adams car与Simulink联合仿真

Adams与simulink悬架联合仿真

Mr.邹的博客 2127

机器学习的训练算法(优化方法)汇总——梯度下降法及其改进算法

本文转载自:https://blog.csdn.net/sqiu_11/article/details/78323676 Intro...

sdf57的博客 823

Livox激光雷达传感器在Python3中的完整驱动程序:深入剖析OpenPyLivox库的实现和应用

当我们提到激光雷达或激光扫描技术时,Livox激光雷达传感器无疑是业界的佼佼者。尽管Livox提供了基于C++的官方软件和API,但现在,有了OpenPyLivox (OPL) 库,Python开发者们也能够轻松地接入和控制这些高级的激光雷达设备。

qq_38334677的博客 1002

javafx结合Scene Builder自定义fxml实现漂亮的登录界面、自定义最小化、关闭按钮

本节主要介绍了如何结合Scene Builder工具来制作一个好看又漂亮的登录界面,以及介绍了如何在程序中调用生成界面文件fxml文件,如何获取到fxml文件中的组件等,并演示登录、最小化和关闭等功能。

c1007857613的专栏 5223

机器学习中的降维算法和梯度下降法

机器学习中有很多算法都是十分经典的,比如说降维算法以及梯度下降法,这些方法都能够帮助大家解决很多问题,因此学习机器学习一定要掌握这些算法,而且这些算法都是比较受大家欢迎的。在这篇文章中我们就给大家重点介绍一下降维算法和梯度下降法。 降维算法 首先,来说一说降维算法,降维算法是一种无监督学习算法,其主要特征是将数据从高维降低到低维层次。在这里,维度其实表示的是数据的特征量的大小,当特征量大的话,那...

CDA数据分析师 1023

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【机器学习-基础算法】梯度下降法

俗话说,没有对比就没有伤害,为了体现出Tensorflow2.0,PyTorch等深度学习框架的便捷性,这次我们使用科学计算库Numpy来实现在整个机器学习领域中最重要,也是最基础的迭代优化算法----梯度下降法。 话不多说,上菜 导包: import numpy as np from sklearn import datasets import matplotlib.pyplot as plt %matplotlib inline 计算误差 def total_error_of_calcul

gecko 314

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

机器学习 · 总览篇 VIII 三要素之算法 - 梯度下降法及其变种

机器学习最后一个要素-算法,准确来说是优化算法,特别是数值优化(Numerical Optimization)或凸优化(Convex Optimization),属于运筹学(最优化理论)的一部分。机器学习中广泛使用的凸优化方法主要分为梯度下降法和拟牛顿法,这两类方法都派生出大量的变种,本文将介绍梯度下降法及其变种。 文章首发于我的博客,转载请保留链接 ???? 一、运筹学和凸优化 本文要介绍的机器学习最后一个要素-算法,其实就是使用了运筹学、特别是凸优化的相关知识,所以本文首先分别介绍运筹学和凸优化。

OEDead的专栏 995

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

机器学习算法之_梯度下降法(Gradient Descent)

在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法。这里就对梯度下降法做一个完整的总结。 一. 梯度 在微积分里面,对多元函数的参数求∂偏导数,把求得的各个参数的偏导数以向量的形式写出来,就是梯度。比如函数f(x,y), 分别对x,y求偏导数,求得的梯度向量就是(∂f/∂x, ∂f/∂y)T,简称grad...

清平乐的技术专栏 785

机器学习算法——梯度下降法

文章目录一、梯度下降法的原理介绍(一)什么是梯度下降法(二)相关概念的介绍(三)梯度下降法的原理二、梯度下降法手工求解三、在Excel中用梯度下降法求解近似根四、线性回归问题的求解(一)最小二乘法(二)梯度下降法(三)二者对比 一、梯度下降法的原理介绍 (一)什么是梯度下降法 (二)相关概念的介绍 (三)梯度下降法的原理 二、梯度下降法手工求解 三、在Excel中用梯度下降法求解近似根 四、线性回归问题的求解 (一)最小二乘法 (二)梯度下降法 (三)二者对比 ...

qq_43678923的博客 1629

机器学习--基础算法--梯度下降法

1 什么是梯度下降法 说明: 不是一个机器学习算法 是一种基于搜索的最优化方法 作用:最小化一个损失函数 梯度上升法:最大化一个效用函数 ●η称为学习率(learning rate) ●η的取值影响获得最优解的速度 ●n取值不合适,甚至得不到最优解 ●n是梯度下降法的一个超参数 并不是所有函数都有唯一的极值点 解决方案: ●多次运行,随机化初始点 ●梯度下降法的初始点也是一个超参数 ...

Mr_Wang0120的博客 497

机器学习总结(五)优化算法之梯度下降法

在机器学习中,优化损失函数的算法对于优化损失函数非常重要,它决定了损失函数的收敛速度,是否容易收敛甚至能不能收敛,是否收敛在全局最小处。 本文主要总结一种常见的优化 损失函数的算法,即梯度下降法: 梯度下降法: 梯度下降法是求解无约束最优化问题的一种最常用,最经典的算法,有实现简单的优点。它是一种迭代算法,每一步需要求解的目标函数的梯度向量。其不仅常用于机器学习算法 ,而且也是深度学习常用的...

weixin_30568715的博客 512

机器学习金典算法(二)--梯度下降法(2)

机器学习金典算法(二)–梯度下降法 &amp;amp;amp;amp;amp;amp;amp;amp;nbsp; &amp;amp;amp;amp;amp;amp;amp;amp;nbsp; &amp;amp;amp;amp;amp;amp;amp;amp;nbsp; &amp;amp;amp;amp;amp;amp;amp;amp;nbsp;本人上篇博文梯度下降法(1)解释了梯度下降法在机器学习中位置及思想,本文将继续讨论梯度下降法,梯度下降法存在的问题及改进思路,以

Mingx9527 1787

梯度下降法-优化算法-机器学习

其每轮计算的目标函数不再是全体样本误差,而仅是单个样本误差,即每次只代入计算一个样本目标函数的梯度来更新权重,再取下一个样本重复此过程,直到损失函数值停止下降或损失函数值小于某个设定的阈值。首先我们先确定前进的方向,也就是梯度的反向,然后走一段距离的步长,也就是α,走完这个段步长,就到达了Θ1这个点!在机器学习中,梯度下降法的根本目是在迭代中寻找损失函数的最小值,并通过反向传播的方式调整各个权重的大小,知道模型拟合程度达到指定要求。2.用步长乘以损失函数的梯度,得到当前位置下降的距离。

weixin_68479946的博客 818
上一篇: 实战七:手把手教你用TensorFlow进行验证码识别(下)
下一篇: 机器学习算法之多项式回归
mind_programmonkey
mind_programmonkey 领域专家: 算法与数据结构技术领域 领域专家: 算法与数据结构技术领域
博客等级 码龄9年 4435粉丝 1111原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mind_programmonkey

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值