That statement is true, there is no parameter named “x” for tf.keras.Sequential.
However,
-
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.
-
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