Exercise:Convolution and Pooling

From Ufldl

Jump to: navigation, search
(Step 2a: Implement convolution)
(Step 2a: Implement convolution)
Line 77: Line 77:
</math>
</math>
-
If the original layout of <tt>W</tt> was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flip <tt>W</tt> before passing it into <tt>conv2</tt>, so that after MATLAB flips <tt>W</tt> 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 <tt>W</tt> using the following code snippet, which works for <tt>W</tt> of any dimension
+
If the original layout of <tt>W</tt> was correct, after flipping, it would be incorrect. For the layout to be correct after flipping, you will have to flip <tt>W</tt> before passing it into <tt>conv2</tt>, so that after MATLAB flips <tt>W</tt> 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 shown below:
<syntaxhighlight lang="matlab">
<syntaxhighlight lang="matlab">
-
% Flip W for use in conv2 / convn
+
% Flip W for use in conv2
-
temp = W(:);
+
W = flipud(fliplr(W));
-
temp = flipud(temp);
+
-
temp = reshape(temp, size(W));
+
</syntaxhighlight>
</syntaxhighlight>

Revision as of 20:32, 23 May 2011

Personal tools