Skip to content

Commit c47e800

Browse files
committed
Convolution
1 parent d0cc5a6 commit c47e800

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

cnn_exercise/cnnConvolve.m

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@
4040
% that you need to take into account the whitening and mean subtraction
4141
% steps
4242

43-
44-
45-
46-
43+
wt = W * ZCAWhite;
44+
bt = b - wt * meanPatch;
4745

4846
% --------------------------------------------------------
4947

@@ -53,14 +51,15 @@
5351

5452
% convolution of image with feature matrix for each channel
5553
convolvedImage = zeros(imageDim - patchDim + 1, imageDim - patchDim + 1);
56-
for channel = 1:3
54+
for channel = 1:imageChannels
5755

5856
% Obtain the feature (patchDim x patchDim) needed during the convolution
5957
% ---- YOUR CODE HERE ----
60-
feature = zeros(8,8); % You should replace this
61-
62-
63-
58+
feature = zeros(patchDim, patchDim); % You should replace
59+
% this
60+
first = patchDim * patchDim * (channel - 1) + 1;
61+
last = first + patchDim * patchDim - 1;
62+
feature = reshape(wt(featureNum, first:last), patchDim, patchDim);
6463

6564
% ------------------------
6665

@@ -74,9 +73,8 @@
7473
% be sure to do a 'valid' convolution
7574
% ---- YOUR CODE HERE ----
7675

77-
78-
79-
76+
convolvedImage = convolvedImage + conv2(im, feature, 'valid');
77+
8078
% ------------------------
8179

8280
end
@@ -85,9 +83,8 @@
8583
% Then, apply the sigmoid function to get the hidden activation
8684
% ---- YOUR CODE HERE ----
8785

88-
89-
90-
86+
convolvedImage = sigmoid(convolvedImage + bt(featureNum));
87+
9188
% ------------------------
9289

9390
% The convolved feature is the sum of the convolved values for all channels
@@ -98,3 +95,6 @@
9895

9996
end
10097

98+
function sigm = sigmoid(x)
99+
sigm = 1 ./ (1 + exp(-x));
100+
end

0 commit comments

Comments
 (0)