DBN(深度信念网络)
DBN运用CD算法逐层进行训练,得到每一层的参数Wi和ci用于初始化DBN,之后再用监督学习算法对参数进行微调。本例中采用softmax分类器(下一篇随笔中)作为监督学习算法。
RBM与上一篇随笔中一致,通过多层RBM将softmax parameter从 (10L, 784L)降低到(10L, 50L)。单独用softmax分类器也可以得到相近(或者略好)的正确率,所需的时间略长一点。
from rbm2 import RBM
from softmax import SoftMax
import os
import numpy as np
import cPickle class DBN:
def __init__(self,nlayers,ntype,vlen,hlen):
self.rbm_layers = []
self.nlayers = nlayers
self.ntype = ntype
self.vlen=vlen
self.hlen=hlen def calcRBMForward(self,x):
for rbm in self.rbm_layers:
x = rbm.forward(x.T)
return x def load_param(self,dbnpath,softmaxpath):
weights = cPickle.load(open(dbnpath,'rb'))
self.nlayers = len(weights)
for i in range(self.nlayers):
weight = weights[i]
v,h= np.shape(weight)
rbm = RBM(v,h)
rbm.w = weight
self.rbm_layers.append(rbm)
print "RBM layer%d shape:%s" %(i,str(rbm.w.shape))
self.softmax = SoftMax()
self.softmax.load_theta(softmaxpath)
print "softmax parameter: "+str(self.softmax.theta.shape) def pretrainRBM(self,trainset):
weights = []
for i in range(self.nlayers):
rbm = RBM(self.vlen,self.hlen)
if i == 0:
traindata = trainset
else:
traindata = np.array(outdata.T)
rbm.rbmBB(traindata)
outdata = np.mat(rbm.forward(traindata))
self.rbm_layers.append(rbm)
weights.append(rbm.w)
self.vlen = self.hlen
self.hlen = self.hlen/2
f= open("data/dbn.pkl",'wb')
cPickle.dump(weights,f)
f.close() def fineTune(self,trainset,labelset):
rbm_output = self.calcRBMForward(trainset)
MAXT,step,landa = 100,1,0.01
self.softmax = SoftMax(MAXT,step,landa)
self.softmax.process_train(rbm_output,labelset,self.ntype) def predict(self,x):
rbm_output = self.calcRBMForward(x)
return self.softmax.predict(rbm_output) def validate(self,testset,labelset):
testnum = len(testset)
correctnum = 0
for i in range(testnum):
x = testset[i]
testtype = self.predict(x)
orgtype = labelset[i]
if testtype == orgtype:
correctnum += 1
rate = float(correctnum)/testnum
print "correctnum = %d, sumnum = %d" %(correctnum,testnum)
print "Accuracy:%.2f" %(rate)
return rate dbn = DBN(3,10,784,200)
f = open('mnist.pkl', 'rb')
training_data, validation_data, test_data = cPickle.load(f)
training_inputs = [np.reshape(x, 784) for x in training_data[0]]
data = np.array(training_inputs[:5000]).T
training_inputs = [np.reshape(x, 784) for x in validation_data[0]]
vdata = np.array(training_inputs[:5000])
if not os.path.exists('data/softmax.pkl'): # Run twice
dbn.pretrainRBM(data)
dbn.fineTune(data.T,training_data[1][:5000])
else:
dbn.load_param("data/dbn.pkl","data/softmax.pkl")
dbn.validate(vdata,validation_data[1][:5000]) #RBM layer0 shape:(784L, 200L)
#RBM layer1 shape:(200L, 100L)
#RBM layer2 shape:(100L, 50L)
#softmax parameter: (10L, 50L)
#correctnum = 4357, sumnum = 5000
#Accuracy:0.87
DBN(深度信念网络)的更多相关文章
- 机器学习——DBN深度信念网络详解(转)
深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系列文章主要记录自己对深度神经网络的一些学习心得. 简要描述深度神经网络模型. 1. 自联想神经网络 ...
- 深度学习(二)--深度信念网络(DBN)
深度学习(二)--深度信念网络(Deep Belief Network,DBN) 一.受限玻尔兹曼机(Restricted Boltzmann Machine,RBM) 在介绍深度信念网络之前需要先了 ...
- 受限玻尔兹曼机(RBM, Restricted Boltzmann machines)和深度信念网络(DBN, Deep Belief Networks)
受限玻尔兹曼机对于当今的非监督学习有一定的启发意义. 深度信念网络(DBN, Deep Belief Networks)于2006年由Geoffery Hinton提出.
- Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3 http://blog.csdn.net/sunbow0 第二章Deep ...
- Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1 http://blog.csdn.net/sunbow0 Spark ML ...
- Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.2
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.2 http://blog.csdn.net/sunbow0 第二章Deep ...
- 理论优美的深度信念网络--Hinton北大最新演讲
什么是深度信念网络 深度信念网络是第一批成功应用深度架构训练的非卷积模型之一. 在引入深度信念网络之前,研究社区通常认为深度模型太难优化,还不如使用易于优化的浅层ML模型.2006年,Hinton等研 ...
- 八.DBN深度置信网络
BP神经网络是1968年由Rumelhart和Mcclelland为首的科学家提出的概念,是一种按照误差反向传播算法进行训练的多层前馈神经网络,是目前应用比较广泛的一种神经网络结构.BP网络神经网络由 ...
- RBM(受限玻尔兹曼机)和深层信念网络(Deep Brief Network)
目录: 一.RBM 二.Deep Brief Network 三.Deep Autoencoder 一.RBM 1.定义[无监督学习] RBM记住三个要诀:1)两层结构图,可视层和隐藏层:[没输出层] ...
随机推荐
- ACM2013.9.7
acm还是要好好做的,还有好多的东西要学呢!不能不认真对待了,该玩够了!
- malloc,vmalloc与kmalloc,kfree与vfree的区别和联系
kmalloc和vmalloc是分配的是内核的内存,malloc分配的是用户的内存kmalloc保证分配的内存在物理上是连续的,vmalloc保证的是在虚拟地址空间上的连续kmalloc能分配的大小有 ...
- java generic type
java generic type: 类型安全.类型参数化.不需要强制转换
- ResultSet结果集判断是否为空
目前亲测过能用的一个方法是: if(rs.next())//当前行有内容 { msg2 = "有这个活动!"; } else //rs对象为空表示查无此活动 { msg2 = &q ...
- HID高级攻击姿势:利用PowerShell脚本进行文件窃取
0×01 引言 又到了期中考试了,我又要去偷答案了,一直发现远程下载运行exe的方式不太好,容易报毒所以这里打算用ps脚本. 0×02 关于HID HID是Human Interface Device ...
- 黑马程序员——【Java基础】——面向对象(二)异常机制、包(Package)
---------- android培训.java培训.期待与您交流! ---------- 一.异常机制 (一)异常概述 1.异常:就是程序在运行时出现不正常情况. 2.异常类:程序在运行时,出现的 ...
- UVA11624(bfs)
题意:给一张图,有火源,有障碍物,剩下的是道路,火源在下一分钟能够让上下左右四个方向的道路也着火,告诉人的位置,问最短时间能逃出去的时间是多少: 思路:一个bfs用来求出所有的火源能蔓延到的地方,另一 ...
- mysql锁表机制及相关优化
(该文章为方便自己查阅,也希望对大家有所帮助,转载于互联网) 1. 锁机制 当前MySQL支持 ISAM, MyISAM, MEMORY (HEAP) 类型表的表级锁,BDB 表支持页级锁,InnoD ...
- cocos2dx 搭建 android 平台
Mac OS X下配置Cocos2d-x for Android(Eclipse)&IOS(Xcode)开发环境 前面一段时间只用Cocos2d-x在IOS平台下开发, 学习Cocos2d-x ...
- [转Go-简洁的并发 ]
http://www.yankay.com/go-clear-concurreny/ Posted on 2012-11-28by yankay 多核处理器越来越普及.有没有一种简单的办法,能够让我们 ...