SyntaxError: tfl.Conv2D(32, (7,7), strides = (1,1)) invalid syntax

My code:
tfl.ZeroPadding2D(padding = 3, data_format = (64,64,3))
tfl.Conv2D(32, (7,7), strides = (1,1))
tfl.BatchNormalization(axis = 3)
tfl.Dense(1, activation=‘sigmoid’)

I have a problem in code execution. Where is the mistake?

tfl.Conv2D(32, (7,7), strides = (1,1))
SyntaxError: invalid syntax

1 Like

Which week?
Which assignment?
And please do not post your code on the Forum. That breaks the course Honor Code.

Week1, assignment 2 - Convolution Model Application. Sorry, but I have not found any other way to show where my mistake could be, other than to place the code.

Is there someone here who can help?

These are entries in an array. Make sure you comma separate the lines.

All the necessary commas, I think, are present in the line with Conv2D. In the previous version of this task, the same line of code was formatted as follows: X = Conv2D (32, (7, 7), strides = (1, 1), name = ‘conv0’) (X)
And there were no mistakes. Now the design was slightly changed and the problems began. (((

It looks like you are using the Keras Sequential API in the code you showed at the beginning of this thread. In that case, you are making a list of “instantiated functions”, but not actually “invoking” them with inputs and outputs. So in that case, because you are creating a list, you need commas between each instantiated function line to turn it into a list.

Note that in the second major portion of this assignment, you will be using the Functional API, which is a different syntax. In that case you are actually explicitly invoking the instantiated functions with inputs and returning explicit outputs. So in that case, you will not want the commas between the lines.

Here’s a thread that gives a much more thorough explanation of how the Sequential and Functional APIs work than we get in the assignments.


Whether you use commas or not depends on which Keras style you’re using.
From the Part 3 introduction:

  • For the first part of this assignment, you’ll create a model using TF Keras’ Sequential API, which allows you to build layer by layer, and is ideal for building models where each layer has exactly one input tensor and one output tensor.

That’s what used in the “happyModel”. You create a list of layers, separated by commas.

  • As you’ll see, using the Sequential API is simple and straightforward, but is only appropriate for simpler, more straightforward tasks. Later in this notebook you’ll spend some time building with a more flexible, powerful alternative: the Functional API.

That’s what is used in the “convolutional_model”. You create the layers directly.


Thanks a lot, the problem is solved. There was also an error with data_format = (64,64,3)) and was fixed by replacing it with input_shape = (64,64,3)), which is not clear to me, data_format is specified in the documentation.


Hey! I am having the same error. Can you elaborate on how you solved that error. My ZeroPadding2D function is correct. I am unable to figure out how I am getting an invalid syntax error

Did you read my earlier reply on this thread?

I kind of misread the above messages and assumed that the commas were to be added after every argument which I had already done and therefore got even more confused. I re-read the above thread and just realized that after each layer a comma has to be inserted.

Thank you Paulin!!

But I am still getting “Test failed” as my output. And now I can’t figure out why?

Expected value

[‘ReLU’, (None, 64, 64, 32), 0]

does not match the input value:

[‘Activation’, (None, 64, 64, 32), 0]

That is a different problem. That means you are specifying the ReLU layer in a way that is different than what they expect. They give you the link to the documentation in the instructions.