Exercise:Convolution and Pooling
From Ufldl
Line 66: | Line 66: | ||
More concretely, your code will look something like the following: | More concretely, your code will look something like the following: | ||
- | + | <syntaxhighlight lang="matlab"> | |
- | + | convolvedFeatures(featureNum, imageNum, r, c) | |
- | + | for imageNum = 1:numImages | |
- | + | for featureNum = 1:hiddenSize | |
- | + | % Obtain the feature matrix for this feature | |
- | + | 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); | |
- | + | for channel = 1:3 | |
- | + | % Flip the feature matrix because of the definition of convolution, as explained | |
- | + | % later | |
- | + | Wt(:, :, channel) = flipud(fliplr(squeeze(Wt(:, :, channel)))); | |
- | + | 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); | |
- | + | 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 | ||
- | + | <syntaxhighlight lang="matlab"> | |
- | + | % Flip W for use in conv2 / convn | |
- | + | temp = W(:); | |
- | + | temp = flipud(temp); | |
+ | temp = reshape(temp, size(W)); | ||
+ | </syntaxhighlight> | ||
</div> | </div> |