SGD/BGD/MBGD使用python简单实现
算法具体可以参照其他的博客:
随机梯度下降:
# coding=utf-8
'''
随机梯度下降
'''
import numpy as np # 构造训练数据
x = np.arange(0., 10., 0.2)
m = len(x)
x0 = np.full(m, 1.0)
input_data = np.vstack([x0, x]).T # 将偏置b作为权向量的第一个分量
target_data = 3 * x + 8 + np.random.randn(m) max_iter = 10000 # 最大迭代次数
epsilon = 1e-5 # 初始化权值
w = np.random.randn(2)
# w = np.zeros(2) alpha = 0.001 # 步长
diff = 0.
error = np.zeros(2)
count = 0 # 循环次数 print '随机梯度下降算法'.center(60, '=') while count < max_iter:
count += 1
for j in range(m):
diff = np.dot(w, input_data[j]) - target_data[j] # 训练集代入,计算误差值
# 这里的随机性表现在:一个样本更新一次参数!
w = w - alpha * diff * input_data[j] if np.linalg.norm(w - error) < epsilon: # 直接通过np.linalg包求两个向量的范数
break
else:
error = w
print 'loop count = %d' % count, '\tw:[%f, %f]' % (w[0], w[1])
# coding=utf-8
"""
批量梯度下降
"""
import numpy as np # 构造训练数据
x = np.arange(0., 10., 0.2)
m = len(x)
x0 = np.full(m, 1.0)
input_data = np.vstack([x0, x]).T # 将偏置b作为权向量的第一个分量
target_data = 3 * x + 8 + np.random.randn(m) # 停止条件
max_iter = 10000
epsilon = 1e-5 # 初始化权值
w = np.random.randn(2)
# w = np.zeros(2) alpha = 0.001 # 步长
diff = 0.
error = np.zeros(2)
count = 0 # 循环次数 while count < max_iter:
count += 1 sum_m = np.zeros(2) for i in range(m):
dif = (np.dot(w, input_data[i]) - target_data[i]) * input_data[i]
sum_m = sum_m + dif
'''
for j in range(m):
diff = np.dot(w, input_data[j]) - target_data[j] # 训练集代入,计算误差值
w = w - alpha * diff * input_data[j]
'''
w = w - alpha * sum_m if np.linalg.norm(w - error) < epsilon:
break
else:
error = w
print 'loop count = %d' % count, '\tw:[%f, %f]' % (w[0], w[1])
小批量梯度下降:
# coding=utf-8
"""
小批量梯度下降
"""
import numpy as np
import random # 构造训练数据
x = np.arange(0., 10., 0.2)
m = len(x)
x0 = np.full(m, 1.0)
input_data = np.vstack([x0, x]).T # 将偏置b作为权向量的第一个分量
target_data = 3 * x + 8 + np.random.randn(m) # 两种终止条件
max_iter = 10000
epsilon = 1e-5 # 初始化权值
np.random.seed(0)
w = np.random.randn(2)
# w = np.zeros(2) alpha = 0.001 # 步长
diff = 0.
error = np.zeros(2)
count = 0 # 循环次数 while count < max_iter:
count += 1 sum_m = np.zeros(2)
index = random.sample(range(m), int(np.ceil(m * 0.2)))
sample_data = input_data[index]
sample_target = target_data[index] for i in range(len(sample_data)):
dif = (np.dot(w, input_data[i]) - target_data[i]) * input_data[i]
sum_m = sum_m + dif w = w - alpha * sum_m if np.linalg.norm(w - error) < epsilon:
break
else:
error = w
print 'loop count = %d' % count, '\tw:[%f, %f]' % (w[0], w[1])
通过迭代,结果会收敛到8和3:
loop count = w:[8.025972, 2.982300]
参考:http://www.cnblogs.com/pinard/p/5970503.html
SGD/BGD/MBGD使用python简单实现的更多相关文章
- 深度学习——优化器算法Optimizer详解(BGD、SGD、MBGD、Momentum、NAG、Adagrad、Adadelta、RMSprop、Adam)
在机器学习.深度学习中使用的优化算法除了常见的梯度下降,还有 Adadelta,Adagrad,RMSProp 等几种优化器,都是什么呢,又该怎么选择呢? 在 Sebastian Ruder 的这篇论 ...
- 【深度学习】深入理解优化器Optimizer算法(BGD、SGD、MBGD、Momentum、NAG、Adagrad、Adadelta、RMSprop、Adam)
在机器学习.深度学习中使用的优化算法除了常见的梯度下降,还有 Adadelta,Adagrad,RMSProp 等几种优化器,都是什么呢,又该怎么选择呢? 在 Sebastian Ruder 的这篇论 ...
- Python简单爬虫入门三
我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...
- Python简单爬虫入门二
接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...
- 亲身试用python简单小爬虫
前几天基友分享了一个贴吧网页,有很多漂亮的图片,想到前段时间学习的python简单爬虫,刚好可以实践一下. 以下是网上很容易搜到的一种方法: #coding=utf-8 import urllib i ...
- GJM : Python简单爬虫入门(二) [转载]
感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...
- Selenium + PhantomJS + python 简单实现爬虫的功能
Selenium 一.简介 selenium是一个用于Web应用自动化程序测试的工具,测试直接运行在浏览器中,就像真正的用户在操作一样 selenium2支持通过驱动真实浏览器(FirfoxDrive ...
- 【美妙的Python之中的一个】Python简单介绍及环境搭建
美妙的Python之Python简单介绍及安装 简而言之: Python 是能你无限惊喜的语言,与众不同. 1.Python: ...
- python 简单图像识别--验证码
python 简单图像识别--验证码 记录下,准备工作安装过程很是麻烦. 首先库:pytesseract,image,tesseract,PIL windows安装PIL,直接exe进行安装更方便( ...
随机推荐
- PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- B - Given Length and Sum of Digits... CodeForces - 489C (贪心)
You have a positive integer m and a non-negative integer s. Your task is to find the smallest and th ...
- [RoarCTF 2019]Easy Calc-协议层攻击之HTTP请求走私
0X01:什么是HTTP请求走私 HTTP请求走私属于协议层攻击,是服务器漏洞的一种. HTTP请求走私是一种干扰网站处理从一个或多个用户接收的HTTP请求序列的方式的技术.使攻击者可以绕过安全控制, ...
- 用Pandas Dataframe来抓取重构金融股票的各种业务&数据形态
4. 如果计算各项股票指标时,或者处理业务流程时,上一篇的直观认知数据结构,怎样帮助开发者去好好操作,又同时避免计算错误的坑. 首先从上篇的数据结据,可以看出/设计出多少种业务和股票指标. A. 恒生 ...
- 即时函数(Immediate Functions)
1.即时函数的声明方法 即时函数(Immediate Functions)是一种特殊的JavaScript语法,可以使函数在定义后立即执行: (function () { alert('wat ...
- web项目servlet&jsp包失效问题
今天偶然遇到这样的一个问题,故做个总结. javaee开发只用到serlet和jsp两个包.而sun提供的jdk只是javase部分的包,对于se部分只提供了规范,而包由容器给出. 由于自己在新建好一 ...
- 苹果浏览器移动端click事件延迟300ms的原因以及解决办法
这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题 —— 当时的网站都是为大屏幕设备所设计的.于是苹果的工程师们做了一些约定,应对 iPhone 这种小屏幕浏览桌面端站点 ...
- CodeForces 382B 数学推导
这个题目题意简单,但是TLE得哭哭的... 输入 a b w x c五个数,最终要使得c<=a, 每一秒可以进行一个操作,如果b>=x,则 b=b-x,同时 c--;如果b<x,则a ...
- python函数中的参数(关键字参数,默认参数,位置参数,不定长参数)
默认参数:定义函数的时候给定变量一个默认值. def num(age=1): 位置参数:调用函数的时候根据定义函数时的形参位置和实参位置进行引用. 关键字参数:如果定义的函数中含有关键字参数,调用函数 ...