wolffd@0
|
1 function [W visB hidB] = training_rbm(conf,W,data_file)
|
wolffd@0
|
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
3 % Training RBM %
|
wolffd@0
|
4 % conf: training setting %
|
wolffd@0
|
5 % W: weights of connections %
|
wolffd@0
|
6 % -*-sontran2012-*- %
|
wolffd@0
|
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
8 %% load data
|
wolffd@0
|
9 vars = whos('-file', data_file);
|
wolffd@0
|
10 A = load(data_file,vars(1).name);
|
wolffd@0
|
11 data = A.(vars(1).name);
|
wolffd@0
|
12 assert(~isempty(data),'[KBRBM] Data is empty');
|
wolffd@0
|
13 %% initialization
|
wolffd@0
|
14 visNum = size(data,2);
|
wolffd@0
|
15 hidNum = conf.hidNum;
|
wolffd@0
|
16 sNum = conf.sNum;
|
wolffd@0
|
17 lr = conf.params(1);
|
wolffd@0
|
18 N = 10; % Number of epoch training with lr_1
|
wolffd@0
|
19 W = 0.1*randn(visNum - size(W,1),size(W,2));
|
wolffd@0
|
20 W = 0.1*randn(size(W,1),hidNum-size(W,2));
|
wolffd@0
|
21
|
wolffd@0
|
22 DW = zeros(size(W));
|
wolffd@0
|
23 visB = zeros(1,visNum);
|
wolffd@0
|
24 DVB = zeros(1,visNum);
|
wolffd@0
|
25 hidB = zeros(1,hidNum);
|
wolffd@0
|
26 DHB = zeros(1,hidNum);
|
wolffd@0
|
27 visP = zeros(sNum,visNum);
|
wolffd@0
|
28 visN = zeros(sNum,visNum);
|
wolffd@0
|
29 visNs = zeros(sNum,visNum);
|
wolffd@0
|
30 hidP = zeros(sNum,hidNum);
|
wolffd@0
|
31 hidPs = zeros(sNum,hidNum);
|
wolffd@0
|
32 hidN = zeros(sNum,hidNum);
|
wolffd@0
|
33 hidNs = zeros(sNum,hidNum);
|
wolffd@0
|
34 %% Reconstruction error & evaluation error & early stopping
|
wolffd@0
|
35 mse = 0;
|
wolffd@0
|
36 omse = 0;
|
wolffd@0
|
37 inc_count = 0;
|
wolffd@0
|
38 MAX_INC = 3000; % If the error increase MAX_INC times continuously, then stop training
|
wolffd@0
|
39 %% Average best settings
|
wolffd@0
|
40 n_best = 1;
|
wolffd@0
|
41 aW = size(W);
|
wolffd@0
|
42 aVB = size(visB);
|
wolffd@0
|
43 aHB = size(hidB);
|
wolffd@0
|
44 %% Plotting
|
wolffd@0
|
45 h = plot(nan);
|
wolffd@0
|
46 %% ==================== Start training =========================== %%
|
wolffd@0
|
47 for i=1:conf.eNum
|
wolffd@0
|
48 if i== N+1
|
wolffd@0
|
49 lr = conf.params(2);
|
wolffd@0
|
50 end
|
wolffd@0
|
51 omse = mse;
|
wolffd@0
|
52 mse = 0;
|
wolffd@0
|
53 for j=1:conf.bNum
|
wolffd@0
|
54 visP = data((j-1)*conf.sNum+1:j*conf.sNum,:);
|
wolffd@0
|
55 %up
|
wolffd@0
|
56 hidP = logistic(visP*W + repmat(hidB,sNum,1));
|
wolffd@0
|
57 hidPs = 1*(hidP >rand(sNum,hidNum));
|
wolffd@0
|
58 hidNs = hidPs;
|
wolffd@0
|
59 for k=1:conf.gNum
|
wolffd@0
|
60 % down
|
wolffd@0
|
61 visN = logistic(hidNs*W' + repmat(visB,sNum,1));
|
wolffd@0
|
62 visNs = 1*(visN>rand(sNum,visNum));
|
wolffd@0
|
63 if j==5 && k==1, observe_reconstruction(visN,sNum,i,28,28); end
|
wolffd@0
|
64 % up
|
wolffd@0
|
65 hidN = logistic(visNs*W + repmat(hidB,sNum,1));
|
wolffd@0
|
66 hidNs = 1*(hidN>rand(sNum,hidNum));
|
wolffd@0
|
67 end
|
wolffd@0
|
68 % Compute MSE for reconstruction
|
wolffd@0
|
69 rdiff = (visP - visN);
|
wolffd@0
|
70 mse = mse + sum(sum(rdiff.*rdiff))/(sNum*visNum);
|
wolffd@0
|
71 % Update W,visB,hidB
|
wolffd@0
|
72 diff = (visP'*hidP - visNs'*hidN)/sNum;
|
wolffd@0
|
73 DW = lr*(diff - conf.params(4)*W) + conf.params(3)*DW;
|
wolffd@0
|
74 W = W + DW;
|
wolffd@0
|
75 % W = W.*mW;
|
wolffd@0
|
76 DVB = lr*sum(visP - visN,1)/sNum + conf.params(3)*DVB;
|
wolffd@0
|
77 visB = visB + DVB;
|
wolffd@0
|
78 DHB = lr*sum(hidP - hidN,1)/sNum + conf.params(3)*DHB;
|
wolffd@0
|
79 hidB = hidB + DHB;
|
wolffd@0
|
80 end
|
wolffd@0
|
81 %%
|
wolffd@0
|
82 mse_plot(i) = mse;
|
wolffd@0
|
83 axis([0 (conf.eNum+1) 0 2]);
|
wolffd@0
|
84 set(h,'YData',mse_plot);
|
wolffd@0
|
85 drawnow;
|
wolffd@0
|
86 % plot(mse_plot,'XDataSource','real(mse_plot)','YDataSource','imag(mse_plot)')
|
wolffd@0
|
87 % linkdata on;
|
wolffd@0
|
88
|
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 |