Week 1, Assignment 2, Exercise 1

The instructions for Exercise 1 (Assignment 2) says:

ZeroPadding2D with padding 3, input shape of 64 x 64 x 3

We are asked to look for help at Module: tf.keras.layers  |  TensorFlow Core v2.5.0) which gives us this syntax:

tf.keras.layers.ZeroPadding2D(padding=(1, 1), data_format=None, **kwargs)

Why am I given a syntax error?

" File “”, line 36
tf.keras.layers.ZeroPadding2D(padding=(3,3))
^
SyntaxError: invalid syntax"

I did try using tfl.ZeroPadding2D but that did not work either.

Also, It is not clear where to put the input shape in the ZeroPadding2D.

I used something like this but it does not seem to help:
tf.keras.layers.Input(shape=(64,64,3))
tf.keras.layers.ZeroPadding2D(padding=(3,3))

I just realized that I need to separate the items with commas.

It works now. I got an “all tests passed.”

[‘ZeroPadding2D’, (None, 70, 70, 3), 0, ((3, 3), (3, 3))]
[‘Conv2D’, (None, 64, 64, 32), 4736, ‘valid’, ‘linear’, ‘GlorotUniform’]
[‘BatchNormalization’, (None, 64, 64, 32), 128]
[‘ReLU’, (None, 64, 64, 32), 0]
[‘MaxPooling2D’, (None, 32, 32, 32), 0, (2, 2), (2, 2), ‘valid’]
[‘Flatten’, (None, 32768), 0]
[‘Dense’, (None, 1), 32769, ‘sigmoid’]
All tests passed!

1 Like

When you get a syntax error at the beginning of the line like that, it means the problem is on the previous line. This is the first part, so you are doing the Sequential API, right? That means you are not really invoking the functions, just making a list of instantiated functions. The syntax of a list in python requires commas to delimit the list items. You did not include that after the Input line, which is why you get the syntax error on the next line.

Note that data_format is not the argument to use. It’s input_shape. That is not documented on the ZeroPadding2D page because it’s inherited from the containing Layer class.

You can leave out the explicit Input layer in the Sequential API and just add the input_shape argument to the ZeroPadding layer.

2 Likes

@paulinpaloalto Yes, I was doing the Sequential API.
Thank you for the follow up explanation. It was very helpful.
I added the input_shape to the ZeroPadding layer and it worked. Yay!

I am brand new to Python and Tensor flow, but I made it this far in the Specialization. I actually do well on the coding part, it’s the commas and for loop indentations that make my life challenging :smile:

1 Like

The course assumes you’re already an expert in python syntax. That makes the assignments even more complicated to complete.

1 Like

Thanks…Good to know.

@TMosh @paulinpaloalto
What is your go to book/online resource to get help with Python and TensorFlow?
I would like to have a physical copy of a book, in addition to online resources, so let me know if you have any favorites.

@nagyka: I haven’t purchased any python books, so I can’t give a personal recommendation. Generally speaking, I find anything I’ve ever looked at from Manning Publications to be really well done. Here’s a book by Francis Chollet from their site that sounds like it might be a good one. If you just search their site for “python”, they have lots of other choices depending on the base level you want to start with.

For online resources, there a lots. If you already have a good programming background in other languages, I find just googling the topic in question will get you the answer pretty quickly. E.g. yesterday I had a question about what the “copy” method of a dictionary does, so I googled … wait for it … “python dictionary copy” and found the answer straightaway. :nerd_face:

1 Like

Another great source of technical books is O’Reilly Publishing. E.g. here’s one of their python books and here’s another. Their website is worth a look for any technical topic.

1 Like