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