function recon = reconstructInput(rbm, data, binary_vis)
%given a trained RBM, reconstruct the given data with numgibbs reconstructions and plot
% rbm = {weights, hidden biases, visible biases};
% data = [input dimension, number of samples];
% binaryvis = whether or not the visible units should be sampled or if
%   their activation probabilities should be treated as grayscale values.

[dim,nsample]=size(data); w=rbm{1}; hb=rbm{2}; vb=rbm{3}; recon=data; 
width = 12; height=13; 
numgibbs = 4; 

figure; set(gcf,'Position',get(0,'screensize')); %maximize figure window
for i=0:numgibbs
  for j=1:nsample/2
    subplot(numgibbs+1, nsample/2, i*nsample/2 + j);
    imagesc( reshape(recon(:,j),[height width])' );
    colormap gray; axis off; axis image;
  end
  drawnow;
  h = w * recon + repmat(hb,[1 nsample]);
  hs = h > rand(size(h));
  recon = w'*hs + repmat(vb,[1 nsample]);
  if binary_vis; recon = +(recon > rand(dim,nsample)); end
end