% Generic script for training an FCRBM
%
% Version 2.0
%
% By: Omid Alemi
%
% Train a Factored Conditional RBM with real style values 
%

%% L1

%initialize RAND,RANDN to a different state
rand('state',sum(100*clock))
randn('state',sum(100*clock))

tic

% Preprocess

MotionData.data =  Motion;
MotionData.skel = skel;
MotionData.FEAT_DIM = 0;
MotionData.mean = 0;
MotionData.std = 0;
MotionData.batchdata = [];
MotionData.minibatch = [];
MotionData.batchdataindex = [];
MotionData.dropRate=4;
MotionData.batchsize = 100;


fprintf(1,'Preprocessing data \n');


%Remove the empty or constant dimensions from the data
%MotionData = Prep_InvarRoot(MotionData);
MotionData = preprocess1(MotionData);
MotionData = Prep_RemoveEmpty(MotionData,0);
MotionData = Prep_AddVelocity(MotionData);



% Pass through layer 1
L1CRBM = load(L1Model);

MotionDataL1 = Prep_NormalizeCRBM(MotionData, L1CRBM.CRBMConfig);

for m=1:length(MotionDataL1.data)
   MotionDataL1.data{m} = exp2crbmfeat(MotionDataL1.data{m}, skel, L1CRBM.CRBMConfig)';
end


labeldata = [];
for jj=1:length(Labels)
  labeldata = [labeldata; Labels{jj}(L1CRBM.CRBMConfig.order+1:end,:)];
end


MotionDataL1 = Prep_MiniBatchOnly(MotionDataL1, n2);

batchdata = MotionDataL1.batchdata;
minibatch = MotionDataL1.minibatch;

figure(10);imagesc(batchdata');colormap gray;

numdims = size(batchdata,2); %data (visible) dimension


%%%%%%%%%%%%%%%%%%%%%%%%%

%% L2
%every xxx epochs, write a snapshot of the model
%will be written to snapshot_path_epxxx.mat
%snapshot_path = 'default' %don't overwrite our models

snapshotevery=100; %write out a snapshot of the weights every xx epochs

nt=n2;
numhid=numhid1;

fprintf(1,'Training Layer 1 FBM, order %d: %d-%d(%d) \n',nt,numdims, ...
  numhid,numfac);
restart=1;      %initialize weights

%train network with only feature-to-factor weights tied
% if realfeat==1
%     gaussianfbm_sharefeatfac_realfeat
% else
%     gaussianfbm_sharefeatfac
% end
binaryfbm_sharefeatfac

fprintf(1,'Training finished. Run demo_generate to generate data\n');

toc