Wednesday, 5 December 2018

testDNN analysis 9

% v2h: to transform from visible (input) variables to hidden (output) variables
%
% H = h2v(dnn, V)
%
%
%Output parameters:
% H: hidden (output) variables, where # of row is number of data and # of col is # of hidden (output) nodes
%
%
%Input parameters:
% dnn: the Deep Neural Network model (dbn, rbm)
% V: visible (input) variables, where # of row is number of data and # of col is # of visible (input) nodes
%
%
%Example:
% datanum = 1024;
% outputnum = 16;
% inputnum = 4;
%
% inputdata = rand(datanum, outputnum);
%
% dnn = randRBM( inputnum, outputnum );
% outputdata = v2h( dnn, input );
%
%
%Version: 20130727


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Deep Neural Network:                                     %
%                                                          %
% Copyright (C) 2013 Masayuki Tanaka. All rights reserved. %
%                    mtanaka@ctrl.titech.ac.jp             %
%                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function H = v2h(dnn, V) % V(1000 x 32, 250 x 32)   dnn.rbm{i}.W(32 x 16)(16 x 8) (8 x 4)

if( isequal(dnn.type, 'BBRBM') ) % BBDBN    Bernoulli-Bernoulli Restricted Bolztmann Machine
    H = sigmoid( bsxfun(@plus, V * dnn.W, dnn.b ) ); % H(250 x 16) * (32 x 16) = 250 x 16
% 250 x 512 * 512 x 128 = 250 x 128

elseif( isequal(dnn.type, 'GBRBM') ) % BBDBN Gaussian-Bernoulli Restricted Bolztmann Machine
    v = bsxfun(@rdivide, V, dnn.sig );
    H = sigmoid( bsxfun(@plus, v * dnn.W, dnn.b ) ); 

elseif( isequal(dnn.type(3:5), 'DBN') ) % BBDBN
    nrbm = numel( dnn.rbm ); % 3
    H0 = V; % 1000 x 32, 250 x 32, 250 x 512
    for i=1:nrbm
        H1 = v2h( dnn.rbm{i}, H0 ); % 1000 x 32, 250 x 32    dnn.rbm{i}.type = BBRBM dnn.rbm{i}.W(32 x 16)(16 x 8) (8 x 4)
        H0 = H1; % 1000 x 4 <- 1000x16="" 1000x8="" 250="" 250x16="" 250x8="" 4="" p="" x="">    end
    H = H1; % 1000 x 4
end