DBN (深度信念网络) MATLAB DeepLearnToolbox 源码学习


 DeepLearnToolbox  源码  下载地址 https://github.com/rasmusbergpalm/DeepLearnToolbox


解压后可得到

  DBN (深度信念网络) MATLAB DeepLearnToolbox 源码学习_第1张图片

data里面有数据集,tests里面有测试代码例子。如下

% function test_example_DBN
load mnist_uint8;  % 该数据集为集为手写数字数据集该数据集为集为手写数字数据集数据集改mnist   28*28 的图片。放在data文件夹中。
train_x = double(train_x) / 255;   % train_x为784个像素值, 这里是在做数据归一化,(0-1)
test_x  = double(test_x) / 255;
train_y = double(train_y);
test_y  = double(test_y);

%%  ex1 train a 100 hidden unit RBM and visualize its weights
% rng(0);
dbn.sizes = [  100  ];  %设置网络隐藏单元数为100
opts.numepochs =   1;       %设置训练迭代次数。
opts.batchsize = 100;     %批次大小
opts.momentum  =   0;    %动量
opts.alpha     =   1;     % 学习率
dbn = dbnsetup(dbn, train_x, opts);  %初始化RBM的参数
dbn = dbntrain(dbn, train_x, opts);   %开始训练
figure; visualize(dbn.rbm{1}.W');   %  Visualize the RBM weights

%%  ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NN
% rng(0);
%train dbn
dbn.sizes = [100 100];
opts.numepochs =   10;
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,并用已经训练好的DBN的权值参数去初始化相应结构的NN网络
nn.activation_function = 'sigm';        %设置激活函数为 sigm

%train nn
opts.numepochs =  1;                   % 设置训练参数
opts.batchsize = 10;                           
nn = nntrain(nn, train_x, train_y, opts);   %训练网络
[er, bad] = nntest(nn, test_x, test_y);     %测试网络错误率

assert(er < 0.10, 'Too big error');

 







你可能感兴趣的:(Deep,Learing)