annotate toolboxes/RBM/gen_training_rbm.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [W visB hidB] = gen_training_krbm(conf,W,mW,train_file,train_label)
wolffd@0 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 3 % Training Knowledge Based RBM for generative classification %
wolffd@0 4 % conf: training setting %
wolffd@0 5 % W: weights of connections %
wolffd@0 6 % mW: mask of connections %
wolffd@0 7 % -*-sontran2012-*- %
wolffd@0 8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 9 %% load data
wolffd@0 10 vars = whos('-file', train_file);
wolffd@0 11 A = load(train_file,vars(1).name);
wolffd@0 12 data = A.(vars(1).name);
wolffd@0 13 vars = whos('-file', train_label);
wolffd@0 14 A = load(train_label,vars(1).name);
wolffd@0 15 label = A.(vars(1).name);
wolffd@0 16 assert(~isempty(data),'[KRBM-GEN] Data is empty');
wolffd@0 17 assert(size(data,1) == size(label,1),'[KRBM-GEN] Number of data and label mismatch');
wolffd@0 18 Classes = unique(label)';
wolffd@0 19 lNum = size(Classes,2);
wolffd@0 20 data = [data discrete2softmax(label,Classes)]
wolffd@0 21 %% initialization
wolffd@0 22 visNum = size(data,2);
wolffd@0 23 hidNum = conf.hidNum;
wolffd@0 24 sNum = conf.sNum;
wolffd@0 25 lr = conf.params(1);
wolffd@0 26 N = 10; % Number of epoch training with lr_1
wolffd@0 27 W = [W;0.1*randn(visNum - size(W,1),size(W,2))];
wolffd@0 28 W = [W 0.1*randn(size(W,1),hidNum-size(W,2))];
wolffd@0 29
wolffd@0 30 DW = zeros(size(W));
wolffd@0 31 visB = zeros(1,visNum);
wolffd@0 32 DVB = zeros(1,visNum);
wolffd@0 33 hidB = zeros(1,hidNum);
wolffd@0 34 DHB = zeros(1,hidNum);
wolffd@0 35 visP = zeros(sNum,visNum);
wolffd@0 36 visN = zeros(sNum,visNum);
wolffd@0 37 visNs = zeros(sNum,visNum);
wolffd@0 38 hidP = zeros(sNum,hidNum);
wolffd@0 39 hidPs = zeros(sNum,hidNum);
wolffd@0 40 hidN = zeros(sNum,hidNum);
wolffd@0 41 hidNs = zeros(sNum,hidNum);
wolffd@0 42 %% Reconstruction error & evaluation error & early stopping
wolffd@0 43 mse = 0;
wolffd@0 44 omse = 0;
wolffd@0 45 inc_count = 0;
wolffd@0 46 MAX_INC = 3; % If the error increase MAX_INC times continuously, then stop training
wolffd@0 47 %% Average best settings
wolffd@0 48 n_best = 1;
wolffd@0 49 aW = size(W);
wolffd@0 50 aVB = size(visB);
wolffd@0 51 aHB = size(hidB);
wolffd@0 52 %% ==================== Start training =========================== %%
wolffd@0 53 for i=1:conf.eNum
wolffd@0 54 if i== N+1
wolffd@0 55 lr = conf.params(2);
wolffd@0 56 end
wolffd@0 57 omse = mse;
wolffd@0 58 mse = 0;
wolffd@0 59 for j=1:conf.bNum
wolffd@0 60 visP = data((j-1)*conf.sNum+1:j*conf.sNum,:);
wolffd@0 61 %up
wolffd@0 62 hidP = logistic(visP*W + repmat(hidB,sNum,1));
wolffd@0 63 hidPs = 1*(hidP >rand(sNum,hidNum));
wolffd@0 64 hidNs = hidPs;
wolffd@0 65 for k=1:conf.gNum
wolffd@0 66 % down
wolffd@0 67 visN = hidNs*W' + repmat(visB,sNum,1);
wolffd@0 68 visN(:,1:visNum-lNum) = logistic(visN(:,1:visNum-lNum));
wolffd@0 69 visN(:,visNum-lNum+1:visNum) = softmax_activation(visN(:,visNum-lNum+1:visNum));
wolffd@0 70 visNs = [1*(visN(:,1:visNum-lNum)>rand(sNum,visNum-lNum)) visN(:,visNum-lNum+1:visNum)];
wolffd@0 71 if j==5 && k==1, observe_reconstruction(visN(:,1:visNum-lNum),sNum,i,28,28); end
wolffd@0 72 % up
wolffd@0 73 hidN = logistic(visNs*W + repmat(hidB,sNum,1));
wolffd@0 74 hidNs = 1*(hidN>rand(sNum,hidNum));
wolffd@0 75 end
wolffd@0 76 % Compute MSE for reconstruction
wolffd@0 77 rdiff = (visP - visN);
wolffd@0 78 mse = mse + sum(sum(rdiff.*rdiff))/(sNum*visNum);
wolffd@0 79 % Update W,visB,hidB
wolffd@0 80 diff = (visP'*hidP - visNs'*hidN)/sNum;
wolffd@0 81 DW = lr*(diff - conf.params(4)*W) + conf.params(3)*DW;
wolffd@0 82 W = W + DW;
wolffd@0 83 % W = W.*mW;
wolffd@0 84 DVB = lr*sum(visP - visN,1)/sNum + conf.params(3)*DVB;
wolffd@0 85 visB = visB + DVB;
wolffd@0 86 DHB = lr*sum(hidP - hidN,1)/sNum + conf.params(3)*DHB;
wolffd@0 87 hidB = hidB + DHB;
wolffd@0 88 end
wolffd@0 89 if mse > omse
wolffd@0 90 inc_count = inc_count + 1
wolffd@0 91 else
wolffd@0 92 inc_count = 0;
wolffd@0 93 end
wolffd@0 94 if inc_count> MAX_INC, break; end;
wolffd@0 95 fprintf('Epoch %d : MSE = %f\n',i,mse);
wolffd@0 96 end
wolffd@0 97 end