C4 W1 CNN Forward Pass : conv_forward (Dimensions)

Hello there,

Sorry, I have been struggling for several days now regarding the programming assignment of the fwd convolution of C4 W1 and still cannot get it to work. My problem, according to the replies I have received in the forum, has to do with the dimensions.

To solve the problem, I am trying to figure out the following programing request from the exercise:

    # Retrieve dimensions from W's shape (≈1 line)
    # (f, f, n_C_prev, n_C) = None

I already retrieved the dimensions from the shape of W, but do the dimensions mean the following?

# (f, f, n_C_prev, n_C) = (height, width, channels, number of filters)

I need this understanding to construct the nested loops, which are crashing on the second pass of the calculation of conv_single_step func with an TypeError: only size-1 arrays can be converted to Python scalars error.

Thank you very much
Jonathan

Yes, that is correct :slight_smile:

You need to post your code for conv_single_step if you need additional help. I can maybe give you hints as to where you might have a problem.

I can say directly that

does not look right. Try and read the comments for what each loop is trying to achieve, and also what your function expects as input, i.e., conv_single_step.

Hi @jonaslalin , thank you (again) for your direct answer. That really helps.

Ok, I will keep on chugging with this input. I’ll report back … :grimacing:

1 Like

Good luck. It is good to get stuck at assignments and for loops and so on, because you will probably understand the mechanics behind the exercise even better than those that pass it at first try.

1 Like

Hi @jonaslalin , thank you very much for the help. Ok, I am inching forwards, getting some values for Z but getting an error, below.

Given

A_prev = np.random.randn(2, 5, 7, 4)
W = np.random.randn(3, 3, 4, 8)
b = np.random.randn(1, 1, 1, 8)

Assuming

A_prev = (number of samples, height, width, #channels)
W = (height, width, #channels, #filters)
b = (height, width, #channels, #filters)

Then

1. Loop over samples (2 in the example above)
     2. Loop over rows (horizontally - ie. 5 rows)
         3. Loop over columns (vertically - ie. 7 rows)
             4. Loop over channels (depth -  ie. 4 channels)
                     conv_single_step(foo)

Error

IndexError: index 1 is out of bounds for axis 2 with size 1

Which makes sense, because the volume of the output Z is:

Z.shape -> (2, 1, 1, 8)
m h w c Z
0 0 0 0 1.3062333729376023
0 0 0 1 0.9076977450643843
0 0 0 2 -4.83497864578125
0 0 0 3 -2.5615502864208004
0 0 1 0 IndexError: index 1 is out of bounds for axis 2 with size 1

So it calculates Z for the first 4 channels of the input volume, not of the output volume, then the sliced window tries moving to the next step to the right and does not find it and crashes.

Any pointers would be greatly appreciated!

In the last step, i.e., innermost loop, you want to extract the weights and biases to use for the current index in number of filters

The weight matrix should be 3 dimensional, height x width x channels

1 Like

Ok, thanks: In the previous post, the weights (and the bias) are indeed being extracted as 3D for the current filter.

Yet, this is the output error:

Z.shape -> (2, 1, 1, 8)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-63-182241fd5e53> in <module>
      6                "stride": 2}
      7 
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
      9 print("Z's mean =\n", np.mean(Z))
     10 print("Z[0,2,1] =\n", Z[0, 2, 1])

<ipython-input-62-ace9970b387d> in conv_forward(A_prev, W, b, hparameters)
     98                     weights = W[ : , : , : , c ]
     99                     biases = b [ : , : , : , c ]
--> 100                     Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
    101 
    102     # YOUR CODE ENDS HERE

IndexError: index 1 is out of bounds for axis 2 with size 1

The shape of the weights results in 3x3x4, a 3D extracted from the target filter, exactly as you mentioned:

(3, 3, 4) 

Another direct feedback would do, please.
Thanks @jonaslalin !

Can you reply with full code for def conv_forward(A_prev, W, b, hparameters)?

sure, but here in the post? or in a pm?

Post it here and you can clean the thread when we have solved your problem :slight_smile: Your code should be broken right now, so it should not matter if someone else is copy pasting it :stuck_out_tongue:

The problem is now resolved. I can now say that I feel I fully understand this part of the assignment.

Thank you @jonaslalin for guiding me through this challenge :sunglasses: :+1:

1 Like