ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

李宏毅机器学习P3-4学习笔记

2021-10-15 22:04:01  阅读:188  来源: 互联网

标签:P3 plt 李宏毅 data 学习 lr np grad history


P3回归模型

主要针对回归的定义、回归模型的三个构建步骤及优化模型的三个步骤进行说明,在优化模型中,构建一元N次线性模型和增加特征值的方法都有可能带来过拟合的问题,对过拟合的规律进行了说明。本节含有较多公式推理,未做叙述。P3内容见图片。

P4回归案例的计算

 代码链接:回归代码演示

import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl

# matplotlib没有中文字体,动态解决
plt.rcParams['font.sans-serif'] = ['Simhei']  # 显示中文
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
x_data = [338., 333., 328., 207., 226., 25., 179., 60., 208., 606.]
y_data = [640., 633., 619., 393., 428., 27., 193., 66., 226., 1591.]
x_d = np.asarray(x_data)
y_d = np.asarray(y_data)
x = np.arange(-200, -100, 1)
y = np.arange(-5, 5, 0.1)
Z = np.zeros((len(x), len(y)))
X, Y = np.meshgrid(x, y) # 生成网格点坐标矩阵
# loss
for i in range(len(x)):
    for j in range(len(y)):
        b = x[i]
        w = y[i]
        Z[j][i] = 0 # # meshgrid吐出结果:y为行,x为列
        for n in range(len(x_data)):
            Z[j][i] += (y_data[n] - b - w * x_data[n]) ** 2
        Z[j][i] /= len(x_data)
# 计算梯度微分的函数getGrad()
def getGrad(b,w):
    # initial b_grad and w_grad
    b_grad = 0.0
    w_grad = 0.0
    for i in range(10):
        b_grad += (-2.0) * (y_data[i] - (b + w * x_data[i]))
        w_grad += (-2.0 * x_data[i]) * (y_data[i] - (b + w * x_data[i]))
    return (b_grad,w_grad)
# 开始训练
# y_data = b + w * x_data
b = -120 # initial b
w = -4 # initial w
lr = 0.0000001 # learning rate
iteration = 100000 # 这里直接规定了迭代次数,而不是一直运行到b_grad和w_grad都为0(事实证明这样做不太可行)

# store initial values for plotting,我们想要最终把数据描绘在图上,因此存储过程数据
b_history = [b]
w_history = [w]

# iterations
for i in range(iteration):
    
    # get new b_grad and w_grad
    b_grad,w_grad=getGrad(b,w)
    
    # update b and w
    b -= lr * b_grad
    w -= lr * w_grad
    
    #store parameters for plotting
    b_history.append(b)
    w_history.append(w)

# plot the figure
plt.contourf(x, y, Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))
plt.plot([-188.4],[2.67],'x',ms=12, markeredgewidth=3, color='orange')
plt.plot(b_history, w_history,'o-',ms=3,lw=1.5,color='black')
plt.xlim(-200, -100)
plt.ylim(-5,5)
plt.xlabel(r'$b$',fontsize=16)
plt.ylabel(r'$w$',fontsize=16)
plt.show()


          

给b和w特制化两种learning rate:

# y_data = b + w * x_data
b = -120 # initial b
w = -4 # initial w
lr = 1 # learning rate 放大10倍
lr_b = 0 # b learning rate
lr_w = 0 # b w learning rate
iteration = 100000 # 这里直接规定了迭代次数,而不是一直运行到b_grad和w_grad都为0(事实证明这样做不太可行)

# store initial values for plotting,我们想要最终把数据描绘在图上,因此存储过程数据
b_history = [b]
w_history = [w]

# iterations
for i in range(iteration):
    
    # get new b_grad and w_grad
    b_grad,w_grad=getGrad(b,w)
    
    # update b and w
    lr_b = lr_b + b_grad ** 2
    lr_w = lr_w + w_grad ** 2
    b -= lr / np.sqrt(lr_b) * b_grad
    w -= lr / np.sqrt(lr_w) * w_grad
    
    #store parameters for plotting
    b_history.append(b)
    w_history.append(w)

# plot the figure
plt.contourf(x, y, Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))
plt.plot([-188.4],[2.67],'x',ms=12, markeredgewidth=3, color='orange')
plt.plot(b_history, w_history,'o-',ms=3,lw=1.5,color='black')
plt.xlim(-200, -100)
plt.ylim(-5,5)
plt.xlabel(r'$b$',fontsize=16)
plt.ylabel(r'$w$',fontsize=16)
plt.show()

标签:P3,plt,李宏毅,data,学习,lr,np,grad,history
来源: https://blog.csdn.net/kkkdemon/article/details/120731148

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有