DLS4 Week1 Assignment 2 part 2 - how to pass on data to next line

Hi, I am getting the following error message when running the second part of the second assignment:
ValueError: Input 0 of layer max_pooling2d_36 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [1, None, 64, 64, 8]

I think I do not manage to feed the required information forward to the next line. I have included my code, but masked it, so I hope that I do not violate any rules:

{Moderator’s Edit - Code Removed}

By reading the instructions, at least it looks like this should be the format for the “Z1” and “outputs”. For the relu functions, I tried to put Z inside the brackets, A=relu(Z), but then I just get something about it’s not being a tuple… Is the above approach the correct way of feeding information to the next line, i.e., is the error somewhere else (i.e. in the some code parts)? Thanks.

Hey @G11,
You have fed the inputs in the correct manner. The problem lies somewhere else. A simple way to debug your implementation is to print the shape of all the variables once they have been defined and before you are passing them to the next layer.

I have modified your post, since it is against the community guidelines to post any code. I hope this helps.

Regards,
Elemento

Yes, it’s the right approach.
There is the another thread to discuss about the Functional API and how to pass parameters.
Please see this link Syntax - A1 = tf.keras.layers.ReLU()(Z1)

@Elemento Ok thanks. I tried to include the input_shape=‘input_shape’ for Z1 in the first parentheses, and, removed the comma after each line. It is working now.
Apparently, you are suppose to have the comma after each line in the first part of this assignment, but not in the second? I am so confused right now…

This exercise is to highlight the difference between the Sequential API (which is the first part), and the Functional API (which is the second part).
If you look at the first part, it is the list of APIs inside the square bracket, which needs “comma” for each API in it. It is intended to pass data sequentially.
On the other hand, the 2nd one is an example of Functional API. Each API can take any output from other layers. In this example, each API just gets an output from the previous layer, but, this is actually flexible. You can take an output from the another layer or even can concatenate multiple outputs. Yes, each API is independent, not a list like Sequential API.

Hey @G11,
Just to add to the detailed explanation provided by Nobu, you don’t need to memorize, where you have to put a comma or not. As mentioned by Nobu, the key thing that you have to learn from this assignment is to understand the differences between sequential and functional API.

Let’s say that you have a neural network of 5 layers, 1 input layer + 3 hidden layers + 1 output layer. If you want to simply pass the input in the following manner:

Input (Input Layer) → First HL → Second HL → Third HL → Output Layer

In that case, the sequential API is great. In this case, you can either pass a list of Tensorflow layers to the Sequential API as being done in the assignment, or use the model.add() method. For reference, check this out.

However, if you want to pass the inputs like:

Input (Input Layer) → First HL → Second HL (+ Output from First HL) → Third HL → Output Layer

In this case, the sequential API fails, and the functional API steps in, and the way we have defined a model using the functional API, is similar to what we have done in the assignment. I hope this helps.

Regards,
Elemento

Some fellow learners have found this thread helpful in the past…

2 Likes