Exercise:Convolution and Pooling

From Ufldl

Jump to: navigation, search
Line 66: Line 66:
More concretely, your code will look something like the following:
More concretely, your code will look something like the following:
-
convolvedFeatures(featureNum, imageNum, r, c)
+
<syntaxhighlight lang="matlab">
-
for imageNum = 1:numImages
+
convolvedFeatures(featureNum, imageNum, r, c)
-
for featureNum = 1:hiddenSize
+
for imageNum = 1:numImages
-
% Obtain the feature matrix for this feature
+
  for featureNum = 1:hiddenSize
-
Wt = W(featureNum, :);
+
    % Obtain the feature matrix for this feature
-
Wt = reshape(Wt, patchDim, patchDim, 3);
+
    Wt = W(featureNum, :);
-
+
    Wt = reshape(Wt, patchDim, patchDim, 3);
-
% Get convolution of image with feature matrix for each channel
+
   
-
convolvedTemp = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1, 3);
+
    % Get convolution of image with feature matrix for each channel
-
for channel = 1:3
+
    convolvedTemp = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1, 3);
-
% Flip the feature matrix because of the definition of convolution, as explained
+
    for channel = 1:3
-
% later
+
      % Flip the feature matrix because of the definition of convolution, as explained
-
Wt(:, :, channel) = flipud(fliplr(squeeze(Wt(:, :, channel))));
+
      % later
-
convolvedTemp(:, :, channel) = conv2(squeeze(images(:, :, channel, imageNum)), squeeze(Wt(:, :, channel)), 'valid');
+
      Wt(:, :, channel) = flipud(fliplr(squeeze(Wt(:, :, channel))));
-
end
+
      convolvedTemp(:, :, channel) = conv2(squeeze(images(:, :, channel, imageNum)), squeeze(Wt(:, :, channel)), 'valid');
-
+
    end
-
% The convolved feature is the sum of the convolved values for all channels
+
   
-
convolvedFeatures(featureNum, imageNum, :, :) = sum(convolvedTemp, 3);
+
    % The convolved feature is the sum of the convolved values for all channels
-
end
+
    convolvedFeatures(featureNum, imageNum, :, :) = sum(convolvedTemp, 3);
-
end
+
  end
 +
end
 +
</syntaxhighlight>
   
   
One detail in the above code needs to be explained - observe that the we "flip" the feature matrix about its rows and columns before passing it into <tt>conv2</tt>. This is necessary because the mathematical definition of convolution involves "flipping" the matrix that is convolved with, as explained in more detail in the implementation tip section below.  
One detail in the above code needs to be explained - observe that the we "flip" the feature matrix about its rows and columns before passing it into <tt>conv2</tt>. This is necessary because the mathematical definition of convolution involves "flipping" the matrix that is convolved with, as explained in more detail in the implementation tip section below.  
Line 124: Line 126:
If the original layout of <math>W</math> was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flip <math>W</math> before passing it into <tt>conv2</tt>, so that after MATLAB flips <math>W</math> in <tt>conv2</tt>, the layout will be correct. For <tt>conv2</tt>, this means reversing the rows and columns, which can be done with <tt>flipud</tt> and <tt>fliplr</tt>, as we did in the example code above. This is also true for the general convolution function <tt>convn</tt>, in which case MATLAB reverses every dimension. In general, you can flip the matrix <math>W</math> using the following code snippet, which works for <math>W</math> of any dimension
If the original layout of <math>W</math> was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flip <math>W</math> before passing it into <tt>conv2</tt>, so that after MATLAB flips <math>W</math> in <tt>conv2</tt>, the layout will be correct. For <tt>conv2</tt>, this means reversing the rows and columns, which can be done with <tt>flipud</tt> and <tt>fliplr</tt>, as we did in the example code above. This is also true for the general convolution function <tt>convn</tt>, in which case MATLAB reverses every dimension. In general, you can flip the matrix <math>W</math> using the following code snippet, which works for <math>W</math> of any dimension
-
% Flip W for use in conv2 / convn
+
<syntaxhighlight lang="matlab">
-
temp = W(:);
+
% Flip W for use in conv2 / convn
-
temp = flipud(temp);
+
temp = W(:);
-
temp = reshape(temp, size(W));
+
temp = flipud(temp);
 +
temp = reshape(temp, size(W));
 +
</syntaxhighlight>
</div>
</div>

Revision as of 07:22, 20 May 2011

Personal tools