C4 W1 A2 Convolution Model Application


I’m having trouble setting up the code for the first exercise of this week’s assignment. I understand which functions to use, but I don’t get how I should format them inside tf.keras.Sequential([ … ]).

Can someone give me a hint?

Thank you!

Here use this page, you can find examples and explanation on how to use this method:

The sequential model is a python list, with the layer objects separated by commas and enclosed in square brackets.

You do not have to pass any data to the Sequential model when you create it - only when it is run. TensorFlow handles that for you.

OK, maybe I am dense, but a single line example here would have saved me hours of trying to figure out what I want. The page linked in both the class and by gent.spah above both use the model.add() function which is actually quite clear to me. I have tried google search and find no examples of using this method. As TMosh mentions above I can see by the expected output that it is a python list. But after figuring that out I still get the error:

TypeError: The added layer must be an instance of class Layer. Found: [<tensorflow.python.keras.layers.convolutional.ZeroPadding2D object at 0x7f9dde414350>]

My code is as follows:

{moderator edit: code removed}

Also, am I supposed to define an input layer? Easy enough to do, but the code “hints” don’t imply I should. Otherwise I am not sure how I am supposed to specify in input shape of 64x64x3. Easy enough to do, but it appears that you aren’t passing any data in or out of this function, so defining an input layer seems out of place here.

Just a quick read through without doing any additional testing, a few comments:

Too many sets of square brackets I think, you’re creating a nested list instead of a simple list.

Try removing the ones I’ve marked, and also from each layer.

You don’t need the 'data_format" argument in any of the layers.

There are many places you can put the input_shape argument - you only need to specify it once, and in the first layer in a Seququential model is fine. You can replace the data_format argument with the shape, and the shape data is a tuple, as input_shape = (64,64,3)

A lot of the arguments you’re providing aren’t required, because they will get default values if you don’t specify them.

The assignment just tells you the arguments you need to specify because we don’t want the defaults.

So in short you’re writing a lot more code than is necessary.

More later if I get a chance to do some testing.

In the future, please don’t post your code. That’s not allowed by the Code of Conduct. I’ll deleted it from the thread once we’re done here.

Data isn’t passed into a model until you train it. The size of the data items is part of the model definition, but the data itself isn’t.

Thanks for the fast response. I actually wrote the code originally without those, but added them trying to figure out what was wrong, as the error messages were not helpful at all. The key seems to be adding the input_shape = (64, 64,. 3) tuple (after removing extra brackets) which makes the code run fine and passes all tests.

This is my first time in all the classes asking for help, despite the fact I never programmed in python before this class, so forgive me if I am missing something, I just don’t know how that someone who is not familiar with this library is supposed to know that attribute even exists. I went back and reread the documentation that the link points to just in case I missed something the last 3 times I read it, and it does not mention that attribute at all. That is actually one of my pet peeves with this class – you are instructed to use functions without clear definitions of inputs and outputs. Maybe it is just my old fashioned way of programming, but clear definitions of functions uses would have saved me many hours (and tons of print statements) in previous programming assignments.

I apologize for posting the code, but it is by far the most expeditious way for you to see what I was trying to do – and it allowed you to give me concise help, which I appreciate. One of the biggest disadvantages of this learning environment vs. an in-person class is not being able to share knowledge and help others on this type of assignment. I have looked at many posts for help, and they are essentially useless because the code the person was needing help with is not visible. I guess if you can respond so quickly (also greatly appreciated) then it isn’t necessary to see and try to assist others, then again, I have learned a great deal helping others find errors in their code. I do realize that there are some people would just copy other’s code, but every programming class I ever took in college that was kind of the case. But in the end, if you didn’t know the material it was you that lost out. I don’t know what the best solution is, but I do wish you could see other’s code attempts – lots to learn there.

Anyway, thank you again for the quick and helpful response.

You’ve done great getting this far, having no previous Python experience. The course assumes you’re already at least an entry-level Python programmer.

The issue you’re discussing is common among object oriented Python tools.

The Keras layers that you use inherit properties from a parent layer, and in each layer’s documentation, they only discuss unique attributes for that layer. So for example if you’re trying to use ZeroPadding2D(), and don’t see the attribute you’re interested in, you have to go look at its parent (or base) layer.

Sometimes you may have to go back a couple of steps in the class definitions to find the core attributes.

As complex as this is for the TensorFlow documentation, it’s even worse for the Keras documentation.

In general the documentation for software packages tends to be written by the expert developers, who already know exactly how it works. So their descriptions are rather opaque for the newcomer.

No problem about posting your code. Generally if a mentor needs to see your code, we’ll contact you privately so you can send your notebook via a personal message.

Thanks for the explanation, it does make sense, from the OOP perspective but it is annoying. As you mention the docs do appear to be written by someone who already knows how it works and doesn’t have their heart into explaining it to newcomers.

Python isn’t too hard to pick up as I have done a lot of programming in other languages. In that respect they Jupyter notebooks are incredibly helpful, although I do wish there was a better way to examine variables rather than extensive print statements. Perhaps there is, I just don’t know it.