Week1, assignment 2, exercise 2: shape problem

Hi, I am having a problem with the convolutional_model exercise.

I get an error about incompatible layer sizes. In the screenshot I attached, I also put the result of printing Z1, A1.shape, and A1. So between Z1 and A1, somehow, an additional dimension was added (by the ReLU layer??). I don’t understand why that would be.

Anybody have an idea?

1 Like

Hi @marie000
Please take a look at how to use the arguments of MaxPool2D in this link here and you will find what is wrong in your code.
Keep learning!

1 Like

@carlosrl, the line of code for “P1 = …” seems OK to me, when I tested it. Maybe I overlooked something.

The code for “A1 = …” also seems OK.

Maybe the problem is with the code for Z1.

1 Like

Yes, it is possible.

1 Like

Thanks everyone. I somehow fixed it. I rewrote some of the lines and somehow it worked. I have no idea what was wrong the first time. I looked at it over and over again and never found anything wrong. At the end, the only difference I could see was some spacing in the parameters, which I doubt caused the issue in the first place.

Ah, the joys of working with technology!

1 Like

The problem is most likely that you had an extra comma at the end of one of the lines of code. You can see one such in the Conv2D layer right after the MaxPool2D layer that “threw” in the exception trace that you show. Maybe you had one of those commas in an earlier line. That has the effect of turning the RHS into a “tuple” and it adds an extra dimension.

You need the commas in the Sequential API, because you are building a list of instantiated layer functions. But they are a disaster in the Functional API where you are invoking instantiated layer functions with a tensor as input. Here’s a thread that is worth a look for a more complete explanation of how the Sequential and Functional APIs work.

1 Like

Good catch Paul.

Right here:

1 Like

Right, that’s the one that I noticed, so that would have caused a problem later. But it “threw” before it got to that line, so my theory is that there must have been a similar error on one of the earlier lines that constructs the model.

1 Like

that makes sense. I might have done a copy-paste of some layers to create the later ones.

I am so used to using the Sequential API and I often forget the commas when I do. I just can’t win.

Thanks for all the help.

1 Like