Exercise:Convolution and Pooling
From Ufldl
(→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 | ||
- | + | Wfeat = W(featureNum, :); | |
- | + | 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 | ||
- | + | 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 | ||
- | + | filter = flipud(fliplr(squeeze(Wfeat(:, :, channel)))); | |
- | + | 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, :, :) = | + | convolvedFeatures(featureNum, imageNum, :, :) = convolvedImage; |
end | end | ||
end | end |