美文网首页
8. 梯度下降实例(三维)

8. 梯度下降实例(三维)

作者: 十里江城 | 来源:发表于2019-11-11 18:28 被阅读0次

三维梯度下降的例子

步骤:

  • 创建三维数据
  • 损失函数计算方式
  • 循环法计算损失函数值
  • 绘制损失函数值的三维图
  • 偏导法使梯度下降
  • 停止条件之外梯度逐渐下降
  • 绘制损失函数下降趋势的散点图
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# 0 np.meshgrid()函数创建网格图
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
# 生成坐标矩阵
X, Y = np.meshgrid(x, y)
Z = X ** 2 + Y ** 2
fig = plt.figure()
# 获得三维的ax
ax = fig.gca(projection = '3d')
# 显示三维网格图
plt.show()


# 1 创建三维数据
theta0s = np.linspace(-2, 2, 100)
theta1s = np.linspace(-2, 2, 100)
# 创建未初始化数组,内容可以是内存上存在的任何数
COST = np.empty(shape = [100, 100])
TOS, TIS = np.meshgrid(theta0s, theta1s)

# 2 损失函数计算方式
def cost(theta0, theta1, x, y):
    J = 0
    m = len(x)
    for i in range(m):
        h = theta1 * x[i] + theta0
        J += (h - y[i]) ** 2
    J /= (2 * m)
    return J


# 3 低效的for循环法计算损失函数值
# 嵌套的for循环很低效,使用以下的求导操作进行绘图
for i in range(100):
    for j in range(100):
        COST[i, j] = cost(TOS[0, i], TIS[j, 0], pga.distance, pga.accuracy)

# 4 绘制损失函数值的三维图
fig2 = plt.figure()
# 获取对象ax
ax = fig2.gca(projection = '3d')
ax.plot_surface(X = TOS, Y = TIS, Z = COST)
plt.show()
 
    

# 5 偏导法使梯度下降
# theta1的偏导    
def partialCostTheta1(theta0, theta1, x, y):
    h = theta0 + theta1 * x
    diff = (h - y) * x
    partial = diff.sum() / (x.shape[0]) # 样本数x.shape[0]
    return partial
partial = partialCostTheta1(0, 5, pga.distance, pga.accuracy)
# print('theta1的偏导 partial:', partial)

# theta0的偏导
def partialCostTheta0(theta0, theta1, x, y):
    h = theta0 + theta1 * x
    diff = h - y
    partial = diff.sum() / (x.shape[0])
    return partial
partial0 = partialCostTheta0(1, 1, pga.distance, pga.accuracy)

# 6 梯度逐渐下降,满足停止条件时候结束
def gradientDescent(x, y, alpha = 0.1, theta0 = 0, theta1 = 0):
    maxEpoch = 1000
    counter = 0
    c = cost(theta1, theta0, pga.distance, pga.accuracy)
    costs = [c]
    # 收敛值
    convergence_thres = 0.000001
    cprev = c + 10
    theta0s = [theta0]
    theta1s = [theta1]
    
    # 不满足停止条件(未达到收敛条件或未达到最大迭代次数)时
    while ((cprev - c) > convergence_thres) and (counter < maxEpoch):
        cprev = c
        update0 = alpha * partialCostTheta0(theta0, theta1, x, y)
        update1 = alpha * partialCostTheta1(theta0, theta1, x, y)
        
        theta0 -= update0
        theta1 -= update1
        
        theta0s.append(theta0)
        theta1s.append(theta1)
        
        c = cost(theta0, theta1, pga.distance, pga.accuracy)
        
        costs.append(c)
        counter += 1
    return {'theta0': theta0, 'theta1': theta1, 'costs': costs}

obj = gradientDescent(pga.distance, pga.accuracy)
print('Theta0 = ', obj['theta0'])
print('Theta1 = ', obj['theta1'])
descend = gradientDescent(pga.distance, pga.accuracy, alpha = .01)


# 7 绘制损失函数下降趋势的散点图
plt.scatter(range(len(descend['costs'])), descend['costs'])
plt.xlabel('apoch')
plt.ylabel('costs')

plt.show()

结果如下:


三维坐标网格图 损失函数三维图和损失函数下降的散点图

相关文章

  • 8. 梯度下降实例(三维)

    三维梯度下降的例子 步骤: 创建三维数据 损失函数计算方式 循环法计算损失函数值 绘制损失函数值的三维图 偏导法使...

  • 深入浅出--梯度下降法及其实现

    梯度下降的场景假设梯度梯度下降算法的数学解释梯度下降算法的实例梯度下降算法的实现Further reading 本...

  • 统计学习方法-感知机-python

    感知机算法 算法描述: 梯度下降 将损失函数梯度下降过程定义为函数 以下代码命名为perceptron.py 实例...

  • (三)线性回归--梯度下降

    一、梯度下降 二、代码的实现 (一.梯度下降) 导包 构建数据 梯度下降 使用梯度下降,可视化 (二。梯度下降矩阵...

  • 神经网络优化2

    梯度下降 梯度下降法 批梯度下降法(Batch Gradient Descent,BGD)是最常用的梯度下降形式,...

  • 机器学习-常用优化方法

    一阶方法:梯度下降、随机梯度下降、mini 随机梯度下降降法。 随机梯度下降不但速度上比原始梯度下降要快,局部最优...

  • ML-梯度下降代码-线性回归为例

    梯度下降代码线性回归为例 bgd 批量梯度下降 sbd 随机梯度下降 mbfd 小批量随机梯度下降

  • 2020-08-19--梯度下降法01

    梯度下降法简介 多元线性回归中的梯度下降法 随机梯度下降法 梯度下降法 的调试 1.梯度下降法简介 不是一个机器学...

  • gluon.Trainer()

    #定义优化算法 #Trainer实例,学习率=0.03小批量随机梯度下降(sgd)为优化算法 #迭代net实例 #...

  • 机器学习笔记(六)—— 梯度下降

    梯度下降 批量梯度下降(Batch Gradient Descent,BGD) 批量梯度下降法是最原始的形式,它是...

网友评论

      本文标题:8. 梯度下降实例(三维)

      本文链接:https://www.haomeiwen.com/subject/ttfxictx.html