Exercise:Convolution and Pooling

From Ufldl

Jump to: navigation, search
(Step 2a: Implement convolution)
(Step 2a: Implement convolution)
Line 51: Line 51:
   for featureNum = 1:hiddenSize
   for featureNum = 1:hiddenSize
     % Obtain the feature matrix for this feature
     % Obtain the feature matrix for this feature
-
     Wt = W(featureNum, :);
+
     Wfeat = W(featureNum, :);
-
     Wt = reshape(Wt, patchDim, patchDim, 3);
+
     Wfeat = reshape(Wfeat, patchDim, patchDim, 3);
      
      
     % Get convolution of image with feature matrix for each channel
     % Get convolution of image with feature matrix for each channel
-
     convolvedTemp = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1, 3);
+
     convolvedImage = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1);
     for channel = 1:3
     for channel = 1:3
 +
       % Flip the feature matrix because of the definition of convolution, as explained later
       % Flip the feature matrix because of the definition of convolution, as explained later
-
       Wt(:, :, channel) = flipud(fliplr(squeeze(Wt(:, :, channel))));
+
       filter = flipud(fliplr(squeeze(Wfeat(:, :, channel))));
-
       convolvedTemp(:, :, channel) = conv2(squeeze(images(:, :, channel, imageNum)), squeeze(Wt(:, :, channel)), 'valid');
+
       im = squeeze(images(:, :, channel, imageNum));
 +
 
 +
      % Convolve "filter" with "im", adding the result
 +
      convolvedImage = convolvedImage + conv2(im, filter), 'valid');
 +
 
     end
     end
      
      
     % The convolved feature is the sum of the convolved values for all channels
     % The convolved feature is the sum of the convolved values for all channels
-
     convolvedFeatures(featureNum, imageNum, :, :) = sum(convolvedTemp, 3);
+
     convolvedFeatures(featureNum, imageNum, :, :) = convolvedImage;
   end
   end
end
end

Revision as of 01:57, 23 May 2011

Personal tools