Conv_forward Week 1 A1 C4


{moderator edit - solution code removed}

Let me know what all is wrong.

Unable to understand the multiple loop structure

There are several things wrong there:

For the n_H and n_W computations, notice that you may not get an integer value when you divide by the stride when stride is not 1. That’s why they give you the hint about using the int() or floor.

The way you initialize Z does not make any sense. You have Z on the right hand side of that assignment statement as an argument to np.zeros. That doesn’t make sense for two reasons: Z is not defined at that point and np.zeros takes an argument which is a “tuple” listing the desired dimensions, not a numpy array. There is another function np.zeros_like which takes an array as input, but it requires that the array in question already exist. You can google “numpy zeros” and “numpy zeros_like” to read the documentation.

The assignment to A_prev_pad is wrong for a similar reason: you also have A_prev_pad on the right hand side of that assignment. That does not make sense, because it is not defined yet. You should be calling the padding function that you already wrote with the appropriate arguments.

There are other things wrong as well.

Also just as a general matter, it’s against the rules to just post your source code every time you have a problem and say (in effect): please fix it for me.

If you are having trouble with the python programming here, please realize that this course is not structured as an “intro to python”. You need to have pretty solid competence in python in order to succeed here. If you are relatively new to programming in general, you should consider putting this course on hold and taking a python course first.

Please do not post your code on the forums.

Yes, the loops are complicated here. The inputs and outputs are 4 dimensional arrays. To do the processing, you need a set of 4 levels of nested loops:

for every sample
    for every position in the vertical dimension of the output
        for every position in the horizontal dimension of the output
            for every output channel
                 here is the body of the loop where the action happens

That’s not python code, of course, just “pseudo-code” to express what needs to happen.