title: Calculate convolutional layer in CNN implementation
body: <p>I am trying to train a Convolutional Neural Network using Sparse autoenconders in order to compute the filters for the convolution layer.  I am using UFLDL code in order to construct patches and to train the CNN network. My code is the following:</p>
  
  <pre><code>===========================================================================
  imageDim = 30;         % image dimension
  imageChannels = 3;     % number of channels (rgb, so 3)
  
  patchDim = 10;          % patch dimension
  numPatches = 100000;    % number of patches
  
  visibleSize = patchDim * patchDim * imageChannels;  % number of input units 
  outputSize = visibleSize;   % number of output units
  hiddenSize = 400;           % number of hidden units 
  
  epsilon = 0.1;         % epsilon for ZCA whitening
  
  poolDim = 10;          % dimension of pooling region
  
  optTheta =  zeros(2*hiddenSize*visibleSize+hiddenSize+visibleSize, 1);
  ZCAWhite =  zeros(visibleSize, visibleSize);
  meanPatch = zeros(visibleSize, 1);
  
  load patches_16_1
  ===========================================================================
  
  % Display and check to see that the features look good
  W = reshape(optTheta(1:visibleSize * hiddenSize), hiddenSize, visibleSize);
  b =     optTheta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize);
  
  displayColorNetwork( (W*ZCAWhite));
  
  stepSize = 100; 
  assert(mod(hiddenSize, stepSize) == 0, stepSize should divide hiddenSize);
  
  load train.mat % loads numTrainImages, trainImages, trainLabels
  load train.mat  % loads numTestImages,  testImages,  testLabels
  % size 30x30x3x8862
  
  numTestImages = 8862;
  numTrainImages = 8862;
  
  pooledFeaturesTrain = zeros(hiddenSize, numTrainImages, floor((imageDim -     patchDim + 1) / poolDim), floor((imageDim - patchDim + 1) / poolDim) );
  pooledFeaturesTest = zeros(hiddenSize, numTestImages, ...
  floor((imageDim - patchDim + 1) / poolDim), ...
  floor((imageDim - patchDim + 1) / poolDim) );
  
   tic();
  
   testImages = trainImages;
  
  for convPart = 1:(hiddenSize / stepSize)
  
   featureStart = (convPart - 1) * stepSize + 1;
   featureEnd = convPart * stepSize;
  
    fprintf('Step %d: features %d to %d\n', convPart, featureStart, featureEnd);  
    Wt = W(featureStart:featureEnd, :);
    bt = b(featureStart:featureEnd);    
  
    fprintf('Convolving and pooling train images\n');
    convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ...
      trainImages, Wt, bt, ZCAWhite, meanPatch);
    pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis);
    pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis;   
    toc();
    clear convolvedFeaturesThis pooledFeaturesThis;
  
    fprintf('Convolving and pooling test images\n');
    convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ...
      testImages, Wt, bt, ZCAWhite, meanPatch);
    pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis);
    pooledFeaturesTest(featureStart:featureEnd, :, :, :) = pooledFeaturesThis;   
    toc();
  
    clear convolvedFeaturesThis pooledFeaturesThis;
  
   end
  </code></pre>
  
  <p>I have problems calculating the convolution and pooling layers. I am getting  pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis; subscripted assignment dimension mismatch. The pathces have normally calculated and they are:</p>
  
  <p><img src="https://i.stack.imgur.com/N8QBj.jpg" alt="enter image description here"></p>
  
  <p>I am trying to understand what exactly the convPart variable is doing and what pooledFeaturesThis. Secondly I notice that my problem is a mismatch in this line <code>pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis;</code>
  where I got the message that the variables is mismatching. THe size of pooledFeaturesThis is 100x3x2x2 where the size of pooledFeaturesTrain is 400x8862x2x2. What exactly pooledFeaturesTrain represents? Is the 2x2 result for every filter?  CnnConvolve could be found <a href="https://github.com/danluu/UFLDL-tutorial/blob/master/cnn_exercise/cnnConvolve.m" rel="noreferrer">here</a> : </p>
  
  <p>EDIT: I have changed a little bit my code and it works. However I a little bit concerned about the comprehension of the code. </p>