ML-程序练习-Dragon
回归问题
前期
假设已有某样例,参数为w=1.477, b=0.089,即为\(y=1.477x+0.089\)
过程分析
数据采样
首先我们需要模拟一些带有真实样本观测误差的数据(因为真实情况是真实模型我们已经知道了),所以我们在这里给模型添加误差自变量\(\epsilon\),其采样自均值为0,标准差为0.01的高斯分布:
\]
通过随机采样100次获得训练数据集
data = []
for i in range(100):
x=np.random.uniform(-10., 10.)
eps=np.random.normal(0., 0.01)
y=1.477*x+0.089+eps
data.append([x,y])
data=np.array(data)
MSE
计算每个点的预测值与真实值之间差的平方并累加,获得均方误差损失值
def mse(b, w, points): # 误差计算
totalError=0
for i in range(0, len(points)):#循环迭代
x=points[i, 0]
y=points[i, 1]
totalError+=(y-(w*x+b))**2
return totalError/float(len(points))# 求均方差
最后的误差和除以样本总数得到平均误差
梯度计算
这里首先需要推导一下梯度的表达式
\]
所以可以得到
\]
同理,可以推导得到
\]
所以,我们只需要计算出上述两个值,平均以后即可得到偏导数
def stepdown_gradient(b_current, w_current, points, lr):
b_gradient=0
w_gradient=0
M=float(len(points))#样本总数
for i in range(0, len(points)):
x=points[i, 0]
y=points[i, 1]
b_gradient+=(2/M)*((w_current * x + b_current) - y)
w_gradient+=(2/M)*x*((w_current*x + b_current)-y)
new_b=b_current-(lr*b_gradient)
new_w=w_current-(lr*w_gradient)
return [new_b,new_w]
梯度更新
我们可以根据计算出的误差函数在w和b处的梯度后,根据\(x^`=x-\eta\triangledown f\)更新w和b的值。对数据集的所有样本训练一次称为一个Epoch,共循环迭代num_iterations个Epoch
def gradient_descent(points, starting_b, starting_w, lr, num_iterations):
b=starting_b
w=starting_w
for step in range(num_iterations):
b, w=stepdown_gradient(b, w, np.array(points), lr)
loss=mse(b, w, points)
if step%50==0:
print(f"iteration:{step}, loss:{loss}, w:{w}, b:{b}")
return [b, w]
完整程序
def mse(b, w, points): # 误差计算
totalError=0
for i in range(0, len(points)):#循环迭代
x=points[i, 0]
y=points[i, 1]
totalError+=(y-(w*x+b))**2
return totalError/float(len(points))# 求均方差
def stepdown_gradient(b_current, w_current, points, lr):
b_gradient=0
w_gradient=0
M=float(len(points))#样本总数
for i in range(0, len(points)):
x=points[i, 0]
y=points[i, 1]
b_gradient+=(2/M)*((w_current * x + b_current) - y)
w_gradient+=(2/M)*x*((w_current*x + b_current)-y)
new_b=b_current-(lr*b_gradient)
new_w=w_current-(lr*w_gradient)
return [new_b,new_w]
def gradient_descent(points, starting_b, starting_w, lr, num_iterations):
b=starting_b
w=starting_w
for step in range(num_iterations):
b, w=stepdown_gradient(b, w, np.array(points), lr)
loss=mse(b, w, points)
if step%50==0:
print(f"iteration:{step}, loss:{loss}, w:{w}, b:{b}")
return [b, w]
data = []
for i in range(100):
x=np.random.uniform(-10., 10.)
eps=np.random.normal(0., 0.01)
y=1.477*x+0.089+eps
data.append([x,y])
data=np.array(data)
lr=0.01
initial_b=0
initial_w=0
num_iterations=1000
[b, w]=gradient_descent(data, initial_b, initial_w, lr, num_iterations)
loss=mse(b, w, data)
print(f'Fina loss:{loss}, w:{w}, b:{b}')
运行结果:
iteration:0, loss:6.162441874953508, w:1.0617677882731775, b:-0.014516689518537094
iteration:50, loss:0.0017523594804364892, w:1.4762089816223927, b:0.04897703734558919
iteration:100, loss:0.00033386053656463924, w:1.4766149652009066, b:0.0747027092487452
iteration:150, loss:0.00014236473287524616, w:1.4767641324874572, b:0.08415488632085935
iteration:200, loss:0.00011651300912947552, w:1.476818939825868, b:0.08762782384952462
iteration:250, loss:0.0001130230547401269, w:1.476839077246164, b:0.08890385740094789
iteration:300, loss:0.0001125519147202384, w:1.476846476176817, b:0.08937270016328336
iteration:350, loss:0.00011248831133360896, w:1.4768491947064997, b:0.08954496329503976
iteration:400, loss:0.00011247972494608194, w:1.4768501935540335, b:0.08960825655441457
iteration:450, loss:0.00011247856579317109, w:1.476850560552563, b:0.08963151188851563
iteration:500, loss:0.00011247840930879765, w:1.476850695395886, b:0.08964005640920385
iteration:550, loss:0.00011247838818357833, w:1.4768507449402855, b:0.08964319585383505
iteration:600, loss:0.00011247838533169705, w:1.4768507631439864, b:0.0896443493547688
iteration:650, loss:0.00011247838494669628, w:1.476850769832426, b:0.0896447731763553
iteration:700, loss:0.00011247838489472176, w:1.4768507722899058, b:0.08964492889771788
iteration:750, loss:0.00011247838488770622, w:1.4768507731928378, b:0.08964498611316783
iteration:800, loss:0.00011247838488675786, w:1.4768507735245948, b:0.0896450071353812
iteration:850, loss:0.00011247838488663068, w:1.4768507736464898, b:0.08964501485940428
iteration:900, loss:0.00011247838488661222, w:1.4768507736912766, b:0.08964501769738008
iteration:950, loss:0.00011247838488661079, w:1.476850773707732, b:0.08964501874011474
Fina loss:0.00011247838488660875, w:1.4768507737137073, b:0.08964501911873728
所以,当迭代100次之后,w和b的值就已经比较接近真实模型了。
ML-程序练习-Dragon的更多相关文章
- TensorFlow 2.0 新特性
安装 TensorFlow 2.0 Alpha 本文仅仅介绍 Windows 的安装方式: pip install tensorflow==2.0.0-alpha0 # cpu 版本 pip inst ...
- RaxML使用
1.下载 https://github.com/stamatak/standard-RAxML 2.How many Threads shall I use? 重要的是要知道,RAxML PThrea ...
- TensorFlow 2.0高效开发指南
Effective TensorFlow 2.0 为使TensorFLow用户更高效,TensorFlow 2.0中进行了多出更改.TensorFlow 2.0删除了篇冗余API,使API更加一致(统 ...
- 利用ML&AI判定未知恶意程序——里面提到ssl恶意加密流检测使用N个payload CNN + 字节分布包长等特征综合判定
利用ML&AI判定未知恶意程序 导语:0x01.前言 在上一篇ML&AI如何在云态势感知产品中落地中介绍了,为什么我们要预测未知恶意程序,传统的安全产品已经无法满足现有的安全态势.那么 ...
- 如何在应用程序中使用ML.NET?
https://www.cnblogs.com/shanyou/p/9190701.html ML.NET以NuGet包的形式提供,可以轻松安装到新的或现有的.NET应用程序中. 该框架采用了用于其他 ...
- 2017年"程序媛和工程狮"绝对不能忽视的编程语言、框架和工具
2017年"程序媛和工程狮"绝对不能忽视的编程语言.框架和工具 在过去的一年里,软件开发行业继续大踏步地向前迈进.回顾 2016 年,我们看到了更多新兴的流行语言.框架和工具, ...
- 用VC2010以上版本编译可以在低版本XP和2003的运行程序的方法
2013-09-17 作者:佚名 来源:本站整理 浏览:2001 评论:1 一直以来倍受此事困拢,vc2010以上版本编译出的exe或dll总是会引用kernel32.dll的En ...
- 【系统篇】从int 3探索Windows应用程序调试原理
探索调试器下断点的原理 在Windows上做开发的程序猿们都知道,x86架构处理器有一条特殊的指令——int 3,也就是机器码0xCC,用于调试所用,当程序执行到int 3的时候会中断到调试器,如果程 ...
- python成长之路-----day1-----作业(登录程序和三级菜单)
作业: 作业1:用户登录 1)程序说明: a.用户输入密码验证成功然后打印欢迎信息 b.如果密码错误,用户登录失败,提示用户,密码错误 c.用户输入密码错误3次,则用户锁定 d.当用户多次输入不存在的 ...
- 浅谈VB.Net 程序的编译和动态编译
---恢复内容开始--- 一般,我们都是通过Visual Studio(下面简称vs)来编写和编译vb.net应用程序的,但是,不少的人并不知道vs是通过何种方式编译程序的.今天,我们就来探讨一下编译 ...
随机推荐
- react+antd pro实现【列表可实时行内编辑】的弹窗表单组件
纯列表版效果展示: ① 初始无值,展示为唤醒按钮+文案外链 ②点击按钮唤醒弹窗(简易版示意图) ③配置后 可编辑表格组件文档: https://procomponents.ant.design/com ...
- redis字段使用说明
Set(集合)增删改查: #删除当前选择数据库中的所有key127.0.0.1:6379> flushdbOK#生成set集合,添加4个数据127.0.0.1:6379> sadd set ...
- opencv对鱼眼图像畸变矫正
import numpy as np ''' #T_cam_imu body_T_cam0: !!opencv-matrix rows: 4 cols: 4 dt: d data: [0.003489 ...
- module ‘pip‘ has no attribute ‘pep425tags‘的解决方案
可行方案: E:\pyth\Anaconda\envs>python -m pip debug --verboseWARNING: This command is only meant for ...
- ffmpeg安装教程
1 下载所需要的软件 mkdir /usr/local/soft cd /usr/local/soft wget https://www.ffmpeg.org/releases/ffmpeg-snap ...
- 项目:表格打印(字符图网格进阶、rjust、列表中最长的字符串长度)
项目要求:编写一个名为 printTable()的函数,它接受字符串的列表的列表,将它显示在组织良好的表格中,每列右对齐. tableData = [['apples', 'oranges', 'ch ...
- python2 selenium
参考blog: https://www.cnblogs.com/xiaozhiblog/p/5378723.html http://www.cnblogs.com/fnng/ 一.项目结构介绍 下面逐 ...
- 任意的形如 z = F(x,y)的曲面生成与显示---基于OpenGL Core Profile
运行结果: (圆锥面) (抛物面) (马鞍面) 其中的做法是:从顶部看上去就是一个平面网格.每个点的 z.x的位置都是程序细分出来的(指定起始.结束.步长).比较固定.但高度 y 的计算使用 用户 ...
- MacOS ssh config 配置
Host 别名 #password 注释,保存密码 HostName IP User 服务器账号#root Port 端口 IdentityFile ~/.ssh/id_rsa #指定密钥 Remot ...
- laravel groupBy 分页
$model=DB::table('tablebname') ->where(function($query) use ($res){ $query->where('xx','xx'); ...