李宏毅老师机器学习课程笔记_ML Lecture 1: ML Lecture 1: Regression - Demo
引言:
最近开始学习“机器学习”,早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程。今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子加深学生的印象。
视频链接(bilibili):李宏毅机器学习(2017)
另外已经有有心的同学做了速记并更新在github上:李宏毅机器学习笔记(LeeML-Notes)
所以,接下来我的笔记只记录一些我自己的总结和听课当时的困惑,如果有能够帮我解答的朋友也请多多指教。
学习机器学习,先从demo侠做起吧,这个demo是完全复现的李老师demo
import numpy as np
import matplotlib.pyplot as plt
x_data = [ 338., 333., 328., 207., 226., 25., 179., 60., 208., 606. ]
y_data = [ 640., 633., 619., 393., 428., 27., 193., 66., 226., 1591. ]
# y_data = b + w * x_data
x = np.arange(-200, -100, 1) # bias
y = np.arange(-5, 5, 0.1) # weight
Z = np.zeros((len(x), len(y)))
X, Y = np.meshgrid(x, y)
for i in range(len(x)):
for j in range((len(y))):
b = x[i]
w = y[j]
Z[j][i] = 0
for n in range(len(x_data)):
Z[j][i] = Z[j][i] +(y_data[n] - b - w*x_data[n])**2
Z[j][i] = Z[j][i]/len(x_data)
b = -129 # intialize b
w = -4 # intialize w
lr = 0.0000001 # learning rate
iteration = 100000
# Store intial values for plotting
b_history = [b]
w_history = [w]
# Iteration
for i in range(iteration):
b_grad = 0.0
w_grad = 0.0
for n in range(len(x_data)):
b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0
w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n]
# Update parameters
b = b - lr * b_grad
w = w - lr * w_grad
# Store the parameters for plotting
b_history.append(b)
w_history.append(w)
# plot the figure
plt.contour(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,lr = 0.000001(调大10倍),得到结果如图2。
#### change the lr to 0.000001
b = -129 # intialize b
w = -4 # intialize w
lr = 0.000001 # learning rate
iteration = 100000
# Store intial values for plotting
b_history = [b]
w_history = [w]
# Iteration
for i in range(iteration):
b_grad = 0.0
w_grad = 0.0
for n in range(len(x_data)):
b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0
w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n]
# Update parameters
b = b - lr * b_grad
w = w - lr * w_grad
# Store the parameters for plotting
b_history.append(b)
w_history.append(w)
# plot the figure
plt.contour(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()

我们再调大learning rate,lr = 0.00001(调大10倍),得到结果如图3。
#### change the lr to 0.00001
b = -129 # intialize b
w = -4 # intialize w
lr = 0.00001 # learning rate
iteration = 100000
# Store intial values for plotting
b_history = [b]
w_history = [w]
# Iteration
for i in range(iteration):
b_grad = 0.0
w_grad = 0.0
for n in range(len(x_data)):
b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0
w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n]
# Update parameters
b = b - lr * b_grad
w = w - lr * w_grad
# Store the parameters for plotting
b_history.append(b)
w_history.append(w)
# plot the figure
plt.contour(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()

一开始设置学习率为0.0000001,经过10万次迭代,发现离最优解还挺远,说明学习率太小,然后将学习率调整为0.000001,扩大了10倍,但是这个时候我们发现,学习率有发生了震荡,但是比之前的结果好了一点,更加接近我们的最优解。然后我们又将学习率增大了十倍,发现最终结果已经超出了整个图纸,完全震荡了,找不到最优解了。
解决办法是:客制化b、w不同的学习率,这种方法称之为AdaGrad
#### using adagrad to solve this problem
b = -129 # intialize b
w = -4 # intialize w
lr = 1 # learning rate
iteration = 100000
b_lr = 0.0
w_lr = 0.0
# Store intial values for plotting
b_history = [b]
w_history = [w]
# Iteration
for i in range(iteration):
b_grad = 0.0
w_grad = 0.0
for n in range(len(x_data)):
b_grad = b_grad - 2.0*(y_data[n] - b - w*x_data[n])*1.0
w_grad = w_grad - 2.0*(y_data[n] - b - w*x_data[n])*x_data[n]
b_lr = b_lr + b_grad**2
w_lr = w_lr + w_grad**2
# Update parameters
b = b - lr/np.sqrt(b_lr) * b_grad
w = w - lr/np.sqrt(w_lr) * w_grad
# Store the parameters for plotting
b_history.append(b)
w_history.append(w)
# plot the figure
plt.contour(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()
最后的结果如图4:

李宏毅老师机器学习课程笔记_ML Lecture 1: ML Lecture 1: Regression - Demo的更多相关文章
- 李宏毅老师机器学习课程笔记_ML Lecture 3-1: Gradient Descent
引言: 这个系列的笔记是台大李宏毅老师机器学习的课程笔记 视频链接(bilibili):李宏毅机器学习(2017) 另外已经有有心的同学做了速记并更新在github上:李宏毅机器学习笔记(LeeML- ...
- 李宏毅老师机器学习课程笔记_ML Lecture 2: Where does the error come from?
引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...
- 李宏毅老师机器学习课程笔记_ML Lecture 1: 回归案例研究
引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...
- 李宏毅老师机器学习课程笔记_ML Lecture 0-2: Why we need to learn machine learning?
引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...
- 李宏毅老师机器学习课程笔记_ML Lecture 0-1: Introduction of Machine Learning
引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...
- Andrew 机器学习课程笔记
Andrew 机器学习课程笔记 完成 Andrew 的课程结束至今已有一段时间,课程介绍深入浅出,很好的解释了模型的基本原理以及应用.在我看来这是个很好的入门视频,他老人家现在又出了一门 deep l ...
- Andrew Ng机器学习课程笔记(四)之神经网络
Andrew Ng机器学习课程笔记(四)之神经网络 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7365730.html 前言 ...
- 【读书笔记与思考】Andrew 机器学习课程笔记
Andrew 机器学习课程笔记 完成 Andrew 的课程结束至今已有一段时间,课程介绍深入浅出,很好的解释了模型的基本原理以及应用.在我看来这是个很好的入门视频,他老人家现在又出了一门 deep l ...
- Andrew Ng机器学习课程笔记(五)之应用机器学习的建议
Andrew Ng机器学习课程笔记(五)之 应用机器学习的建议 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7368472.h ...
随机推荐
- SolrJ 的运用
SolrJ 是操作 Solr 的 Java 客户端,它提供了增加.修改.删除.查询 Solr 索引的 Java 接口.SolrJ 针对 Solr 提供了 REST 的 Http 接口进行了封装, So ...
- 系统分析与设计lesson6
| 分类 作业 | 1.用例建模 a. 阅读 Asg_RH 文档,绘制用例图. 按 Task1 要求,请使用工具 UMLet,截图格式务必是 png 并控制尺寸 b. 选择你熟悉的定旅馆在线服务系统 ...
- 开发过程中关于JSON的那些事
在使用过程中,对JSON了解的还不够,特地整理一下,用于个人学习和知识参考. 1.IBM的json入门指南 json官网 2.javaweb中发送接收解析问题 3.Java解析json,以及js ...
- 【教程向】配置属于自己的vim
新建Vim配置文件 Linux mkdir -/.vimrc 配置 常用设置 配置 功能 set number 设置行号 set systax on 高亮 colorscheme{主题} 设置颜色主题 ...
- python爬虫-纠正MD5错误认知
m = md5(".encode()) print(m.hexdigest()) # 25d55ad283aa400af464c76d713c07ad m = md5(".enco ...
- datatable某列不排序、和自定义搜索、给数据里面加属性
datatable中如果不想对前几列进行排序,使用以下代码: $('#informationList').DataTable({ //对0,1,2列不排序 "columnDefs" ...
- 前端分享之cookie的使用及单点登录
cookie是什么 cookie的英文意思是饼干.在计算机术语中指服务端存放在客户端的一段数据.这段数据在客户端每次进行http请求时会自动加在http请求报文中的header上:服务端在响应时,可以 ...
- Flask架构管理及特点(重要)
正文 程序包结构 ——————————————————————————————————flask文件夹结构 其中:app为程序包,Flask程序保存在这个包中migrations文件夹包含数据库迁移脚 ...
- spring boot Shiro JWT整合
一个api要支持H5, PC和APP三个前端,如果使用session的话对app不是很友好,而且session有跨域攻击的问题,所以选择了JWT 1.导入依赖包 <dependency> ...
- 计算机网络原理实验_使用网络协议分析仪Wireshark
一.实验名称 使用网络协议分析仪Wireshark 二.实验目的: 1. 掌握安装和配置网络协议分析仪Wireshark的方法: 2. 熟悉使用Wireshark工具分析网络协议的基本方法,加深对协 ...