机器学习-微积分

损失函数

损失函数的意义

机器学习-linear regression-两大经典场景
线性回归-预测房价

分类-classification problem

使用到的数学知识

导数-derivative

函数在某一时刻的瞬时变化率 instantaneous rate of change

函数在某点的切线斜率 = 导数

最大值、最小值的点的导数=0,即函数的瞬时变化率=0

y=C 水平线的导数 斜率=0
线性方程的 斜率=变量的系数

二阶方程的导数 f=x2->2x

3阶方程导数y=x3->3x2

导数的普适公式

反函数

sinx的导数是cosx

cosx的导数是-sinx

欧拉e的导数还是e

e=2.7182818

logX的导数1/y

函数可导

复杂函数求导
乘以标量

总和法则

乘法规则

链式法则

平方损失优化

使用log函数->求导->极值点的值

为什么使用log求导

多元模型
f(x,y)=x2+y2

偏导数

梯度

函数在(2,3)点上的梯度[4,6]

通过梯度求最小值

在多维空间中求极值,在各个变量的偏导均为0的点是极值点,极值点作为候选再选择最大最小

 

线性回归-给出一堆点->求离他们最近的直线

导数下降

learning rate 学习率

解决梯度下降带来的最小值错觉

多路同时求值

梯度下降

梯度陷阱

感知器-perceptron
linear regression-使用均方误差损失函数

classification-使用对数损失函数

神经网络

反向传播

从最后的Layer开始求导

牛顿法

二阶导数

曲率-curvature

hessian martix 海森矩阵

二元函数的二阶导数

对多元方程,通过特征值判断局面的凹凸
特征值全部>0,凹面

特征值全部<0,凸面

多元变量的牛顿法

"""
learning store for machine learning
"""
import time
import numpy as np


def time_cost(function):
    """
    用于统计执行耗时的装饰器
    """
    def wrapper(*args, **kwargs):
        start = time.time()
        res = function(*args, **kwargs)
        end = time.time()
        print(f"{function.__name__} cost {end - start:.8f} s")
        return res
    return wrapper


@time_cost
def cal_delta(hessian_matrix, gradient_vector):
    """
    cal next step
    """
    hessian_inv = np.linalg.inv(hessian_matrix)
    vector = gradient_vector.reshape(-1, 1)
    result = np.dot(hessian_inv, vector)


    return result


def cal_next_step(cur_step_vector, hessian_matrix, gradient_vector):
    """
    使用海森矩阵牛顿法推理下一步的点位
    """
    delta_vector = cal_delta(hessian_matrix, gradient_vector)
    return (cur_step_vector - delta_vector).reshape(1,-1).tolist()[0]




if __name__ == "__main__":
    hessian = np.array([[198.4, -2.6], [-2.6, 157.6]])
    gradient = np.array([277.6, 213.6])
    cur_point = np.array([[4],[4]])
    next_point = cal_next_step(cur_point, hessian, gradient)
    print(next_point)

posted @ 2024-03-20 14:50  theBoyBack  阅读(6)  评论(0编辑  收藏  举报