引言:

最近开始学习“机器学习”,早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程。今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子加深学生的印象。

视频链接(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的更多相关文章

  1. 李宏毅老师机器学习课程笔记_ML Lecture 3-1: Gradient Descent

    引言: 这个系列的笔记是台大李宏毅老师机器学习的课程笔记 视频链接(bilibili):李宏毅机器学习(2017) 另外已经有有心的同学做了速记并更新在github上:李宏毅机器学习笔记(LeeML- ...

  2. 李宏毅老师机器学习课程笔记_ML Lecture 2: Where does the error come from?

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  3. 李宏毅老师机器学习课程笔记_ML Lecture 1: 回归案例研究

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  4. 李宏毅老师机器学习课程笔记_ML Lecture 0-2: Why we need to learn machine learning?

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  5. 李宏毅老师机器学习课程笔记_ML Lecture 0-1: Introduction of Machine Learning

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  6. Andrew 机器学习课程笔记

    Andrew 机器学习课程笔记 完成 Andrew 的课程结束至今已有一段时间,课程介绍深入浅出,很好的解释了模型的基本原理以及应用.在我看来这是个很好的入门视频,他老人家现在又出了一门 deep l ...

  7. Andrew Ng机器学习课程笔记(四)之神经网络

    Andrew Ng机器学习课程笔记(四)之神经网络 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7365730.html 前言 ...

  8. 【读书笔记与思考】Andrew 机器学习课程笔记

    Andrew 机器学习课程笔记 完成 Andrew 的课程结束至今已有一段时间,课程介绍深入浅出,很好的解释了模型的基本原理以及应用.在我看来这是个很好的入门视频,他老人家现在又出了一门 deep l ...

  9. Andrew Ng机器学习课程笔记(五)之应用机器学习的建议

    Andrew Ng机器学习课程笔记(五)之 应用机器学习的建议 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7368472.h ...

随机推荐

  1. 【Java】macOS下编译JDK8

    安装mercurial brew install mercurial 下载源码 1234 hg clone http://hg.openjdk.java.net/jdk8/jdk8 java-sour ...

  2. GoLand 设置与配置

    1. 将 tab 改为 4个空格 2. GoLand 取消 import 自动导入

  3. oa办公系统快速开发工具,助力企业优化升级

    随着互联网的快速发展.信息化 IT 技术的不断进步.移动互联新技术的兴起,不管是大的集团企业还是中小型企业,纸质化的办公模式已不能满足现有需求,构建oa平台,为员工提供高效的办公环境尤其重要. 我们先 ...

  4. Linux +Docker +Nginx 部署代理转发初探

    很多开发人员仅仅只会码代码,如果让开发人员部署一些深入点的东西,或者做负载均衡等,很多开发人员估计就懵逼了,作为一个专业的开发还需要懂一些基本的运维知识:虽然说在互联网行业中,中小企业都有一个牛逼的运 ...

  5. beego的安装以及bee的安装和使用

    beego的安装以及bee的安装和使用 一.beego的安装 1.beego是什么 beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API.Web 及后端服务等各种应用, ...

  6. js判断PC端还是移动端的代码小坑

    我在写官网的时候做了pc和移动端两个,在通过网上查找了这样的代码,看着完全没问题,等放进去页面中后,PC端页面一直刷新,根本停不下来,找了类似js还是同样的问题.通过不断尝试后才发现,问题就是多了一行 ...

  7. 超详细,多图文介绍redis集群方式并搭建redis伪集群

    超详细,多图文介绍redis集群方式并搭建redis伪集群 超多图文,对新手友好度极好.敲命令的过程中,难免会敲错,但为了截好一张合适的图,一旦出现一点问题,为了好的演示效果,就要从头开始敲.且看且珍 ...

  8. excel排序技术记录

    问题: 给了我一个excel,要求以奖项和编码同时进行排序(奖项优先),但是单元格大小不一样,有数列都是合并了单元格的,同时编码的格式还不一样,有些是SMM-2-07,有些是2-07,所以根本无法进行 ...

  9. 微信小程序之登录连接django,以及用户的信息授权认证

    小结: 1 如何自定义组件 - 组件和页面一样,也是由四个文件组成,所以我们自定义组件的时候,模拟pages文件夹,把所有的所有的组件都放在一个文件夹中,每个组件又由一个文件夹包裹,方便管理,在对应目 ...

  10. Redis系列一 - 入门篇

    问:项目中为何要选用Redis? 答:传统的关系型数据库(如MySQL)已经不适用所有的场景了,比如美云销抢单活动的库存扣减,APP首页的访问流量高峰等等,都容易把数据库打崩,所以引入了缓存中间件,目 ...