下载地址:DeepLearningToolBox
参考博客原文:https://blog.csdn.net/u010025211/article/details/50582693
DBN 是由多层 RBM 组成的一个神经网络,它既可以被看作一个生成模型,也可以当作判别模型,其训练过程是:使用非监督贪婪逐层方法去预训练获得权值。
训练过程:
图中的绿色部分就是在最顶层 RBM 中参与训练的标签。注意调优 (FINE-TUNING) 过程是一个判别模型
调优过程 (Fine-Tuning) :
生成模型使用 Contrastive Wake-Sleep 算法进行调优,其算法过程是:
test_example_DBN
%% ex1 train a 100 hidden unit RBM and visualize its weights
rand('state',0)
dbn.sizes = [100];
opts.numepochs = 1;
opts.batchsize = 100;
opts.momentum = 0;
opts.alpha = 1;
dbn = dbnsetup(dbn, train_x, opts);
dbn = dbntrain(dbn, train_x, opts);
figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights
第一个例子是训练含有100个隐层单元的RBM,然后可视化权重。方法和之前将的训练RBM来降维是类似的。
可视化权重结果:
%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NN
rand('state',0)
%train dbn
dbn.sizes = [100 100];
opts.numepochs = 1;
opts.batchsize = 100;
opts.momentum = 0;
opts.alpha = 1;
dbn = dbnsetup(dbn, train_x, opts);
dbn = dbntrain(dbn, train_x, opts);
%unfold dbn to nn
nn = dbnunfoldtonn(dbn, 10);
nn.activation_function = 'sigm';
%train nn
opts.numepochs = 1;
opts.batchsize = 100;
nn = nntrain(nn, train_x, train_y, opts);
[er, bad] = nntest(nn, test_x, test_y);
assert(er < 0.10, 'Too big error');
直接分层初始化每一层的rbm(受限波尔兹曼机(Restricted Boltzmann Machines, RBM)), 同样,W,b,c是参数,vW,vb,vc是更新时用到的与momentum的变量
for u = 1 : numel(dbn.sizes) - 1
dbn.rbm{u}.alpha = opts.alpha;
dbn.rbm{u}.momentum = opts.momentum;
dbn.rbm{u}.W = zeros(dbn.sizes(u + 1), dbn.sizes(u));
dbn.rbm{u}.vW = zeros(dbn.sizes(u + 1), dbn.sizes(u));
dbn.rbm{u}.b = zeros(dbn.sizes(u), 1);
dbn.rbm{u}.vb = zeros(dbn.sizes(u), 1);
dbn.rbm{u}.c = zeros(dbn.sizes(u + 1), 1);
dbn.rbm{u}.vc = zeros(dbn.sizes(u + 1), 1);
end
function dbn = dbntrain(dbn, x, opts)
n = numel(dbn.rbm);
//对每一层的rbm进行训练
dbn.rbm{1} = rbmtrain(dbn.rbm{1}, x, opts);
for i = 2 : n
x = rbmup(dbn.rbm{i - 1}, x);
dbn.rbm{i} = rbmtrain(dbn.rbm{i}, x, opts);
end
end
首先映入眼帘的是对第一层进行rbmtrain(),后面每一层在train之前用了rbmup, rbmup其实就是简单的一句sigm(repmat(rbm.c’, size(x, 1), 1) + x * rbm.W’);也就是上面那张图从v到h计算一次,公式是Wx+c.
for i = 1 : opts.numepochs //迭代次数
kk = randperm(m);
err = 0;
for l = 1 : numbatches
batch = x(kk((l - 1) * opts.batchsize + 1 : l * opts.batchsize), :);
v1 = batch;
h1 = sigmrnd(repmat(rbm.c', opts.batchsize, 1) + v1 * rbm.W'); //gibbs sampling的过程
v2 = sigmrnd(repmat(rbm.b', opts.batchsize, 1) + h1 * rbm.W);
h2 = sigm(repmat(rbm.c', opts.batchsize, 1) + v2 * rbm.W');
//Contrastive Divergence 的过程
//这和《Learning Deep Architectures for AI》里面写cd-1的那段pseudo code是一样的
c1 = h1' * v1;
c2 = h2' * v2;
//关于momentum,请参看Hinton的《A Practical Guide to Training Restricted Boltzmann Machines》
//它的作用是记录下以前的更新方向,并与现在的方向结合下,跟有可能加快学习的速度
rbm.vW = rbm.momentum * rbm.vW + rbm.alpha * (c1 - c2) / opts.batchsize;
rbm.vb = rbm.momentum * rbm.vb + rbm.alpha * sum(v1 - v2)' / opts.batchsize;
rbm.vc = rbm.momentum * rbm.vc + rbm.alpha * sum(h1 - h2)' / opts.batchsize;
//更新值
rbm.W = rbm.W + rbm.vW;
rbm.b = rbm.b + rbm.vb;
rbm.c = rbm.c + rbm.vc;
err = err + sum(sum((v1 - v2) .^ 2)) / opts.batchsize;
end
end
DBN的每一层训练完成后自然还要把参数传递给一个大的NN,这就是这个函数的作用.在这里DBN就相当于预训练网络,然后将训练好的参数赋给NN结构。
function nn = dbnunfoldtonn(dbn, outputsize)
%DBNUNFOLDTONN Unfolds a DBN to a NN
% outputsize是你的目标输出label,比如在MINST就是10,DBN只负责学习feature
% 或者说初始化Weight,是一个unsupervised learning,最后的supervised还得靠NN
if(exist('outputsize','var'))
size = [dbn.sizes outputsize];
else
size = [dbn.sizes];
end
nn = nnsetup(size);
%把每一层展开后的Weight拿去初始化NN的Weight
%注意dbn.rbm{i}.c拿去初始化了bias项的值
for i = 1 : numel(dbn.rbm)
nn.W{i} = [dbn.rbm{i}.c dbn.rbm{i}.W];
end
end
最后用NN来train(fine-tune)就可以了。只要理解了多层RBM,DBN就不是问题了。