C4W1 Convolutional Neural Networks: Application

The graded block for the happyModel is not allowing me to run anything without saying syntax error on the ‘=’

As a test I tried to run
x=1
print x

and got a syntax error for x=1.

I can run things in a python console no issue but even copying and pasting from a console to the jupyter cell is erroring out. I have not had this issue a single time up to this point.

Hi @tpatrick,

Try by fetching a new copy of the assignment, the instructions (how to refresh your workspace) for which can be found in the first week. Use Option 2. You can copy/paste your already done graded functions in the new version as well. Let me know how that goes.

Best,
Mubsi

I tried this method and still get the same error.

Hi @tpatrick,

Can you share your lab ID with me ? In the assignment, when you click the top right “Help” button, a panel will open and your lab ID will be shown at the bottom.

I shall take a look.

Thanks,
Mubsi

Here is the lab id: nrtwiwnb

This is the new copy of the assignment and all I have put into the code block for the assignment is
“x=1”

There may or may not be an issue with the notebook, but for sure the test case here is not valid. That is because tf.keras.Sequential() is expecting a valid Python list to be passed in. And [x=1] is not a valid Python list.

Hi @tpatrick,

It is probably because tf.keras.Sequential has no parameter named “x”.

Best,
Mubsi

That statement is true, there is no parameter named “x” for tf.keras.Sequential.

However,

  1. in the OP code above, “x” isn’t being passed as a parameter; it is inside the Python list square brackets. The list is the parameter.

  2. in the event that “x” is passed as a parameter, it won’t produce the reported syntax error

Supporting scaffold code follows

    #Python interpreter accepts all of these
sequential_model_no_args = keras.Sequential()

x = [
    layers.Dense(64, activation="relu", name="dense_1_sequential_def"),
    layers.Dense(64, activation="relu", name="dense_2_sequential_def"),
    layers.Dense(10, name="predictions_sequential_def"),
]

sequential_model_one_arg = keras.Sequential(x)

sequential_model_more_verbose = keras.Sequential(layers=x,name='verbose')

sequential_model_dynamic_layers_list = keras.Sequential(
    [
        layers.Dense(64, activation="relu", name="dense_1_sequential_def"),
        layers.Dense(64, activation="relu", name="dense_2_sequential_def"),
        layers.Dense(10, name="predictions_sequential_def"),
    ]
)

print('all the above worked fine!')

all the above worked fine!

    #what about defining the list this way...
x = 1
my_layers =  [
    layers.Dense(64, activation="relu", name="dense_1_sequential_def"),
    layers.Dense(64, activation="relu", name="dense_2_sequential_def"),
    layers.Dense(10, name="predictions_sequential_def"),
    x
]
print('no problemo')

no problemo

    #now try assignment inside Python list operators []
my_layers =  [
    layers.Dense(64, activation="relu", name="dense_1_sequential_def"),
    layers.Dense(64, activation="relu", name="dense_2_sequential_def"),
    layers.Dense(10, name="predictions_sequential_def"),
    x = 1
]

nope

File “/var/folders/v3/cqv82ph57nq2c4sggjqqlm180000gn/T/ipykernel_854/2366826741.py”, line 6
x = 1
^
SyntaxError: invalid syntax

    #or passing x as a formal argument to Sequential
sequential_model_with_x_in_args = keras.Sequential(x,layers=my_layers,name='problems')

TypeError Traceback (most recent call last)
/var/folders/v3/cqv82ph57nq2c4sggjqqlm180000gn/T/ipykernel_854/2714734362.py in
1 #or passing x as a formal argument to Sequential
----> 2 sequential_model_with_x_in_args = keras.Sequential(x,layers=my_layers,name=‘problems’)

~/miniforge3/envs/tf_macos/lib/python3.9/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
520 self._self_setattr_tracking = False # pylint: disable=protected-access
521 try:
→ 522 result = method(self, *args, **kwargs)
523 finally:
524 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

TypeError: init() got multiple values for argument ‘layers’

    #or passing x as the only argument to Sequential
sequential_model_x_only_args = keras.Sequential(x)

~/miniforge3/envs/tf_macos/lib/python3.9/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
184 layer = functional.ModuleWrapper(layer)
185 else:
→ 186 raise TypeError('The added layer must be ’
187 'an instance of class Layer. ’
188 'Found: ’ + str(layer))

TypeError: The added layer must be an instance of class Layer. Found: 1

@tpatrick, It is hard to see with all those #comments in line, but what you are being asked to do is define the body of a Python list. The clue is the square brackets. If you take out all the comments, the code in the notebook looks like this…

model = tf.keras.Sequential(
                            [
                                   # YOUR CODE STARTS HERE
                                   # YOUR CODE ENDS HERE
                            ]
               )

According to the tf.keras.Sequential documentation (linked above), what is expected there is …list of layers to add to the model. A Python list is a sequence of variables 0 to n in length. Sequences of length longer than 1 are comma delimited. Assignment is not permitted within the sequence.

python_list_with_length_zero = []
python_list_with_length_one = [1]
x = 1
another_list_with_length_one = [x]
y = 2
python_list_with_length_two = [x,y]  #comma delimited

invalid_python_list_syntax = [x=1]  #assignment inside causes syntax error

You might get some additional insight into what is happening inside that call to the Sequential model constructor from this related thread. Good luck.

I missed the brackets! Thank you so much

1 Like