In this video “Declaring and stacking layers”, Laurence talks about how the double bracket syntax
eg:
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)(flatten_layer)
is similar to first returning an object of a dense layer and storing it in the variable first_dense and then passing the parameter flatten_layer to it in the next line.
eg:
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)
first_dense(flatten_layer)
However, when I try to do this in the C1_W1_Lab_1_functional-practice I get an error.
i.e., instead of this:
def build_model_with_functional():
# instantiate the input Tensor
input_layer = tf.keras.Input(shape=(28, 28))
# stack the layers using the syntax: new_layer()(previous_layer)
flatten_layer = tf.keras.layers.Flatten()(input_layer)
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)(flatten_layer)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(first_dense)
# declare inputs and outputs
func_model = Model(inputs=input_layer, outputs=output_layer)
return func_model
I tried this:
def build_model_with_functional():
# instantiate the input Tensor
input_layer = tf.keras.Input(shape=(28, 28))
# stack the layers using the syntax: variable = new_layer()
# variable(previous_layer)
flatten_layer = tf.keras.layers.Flatten()
flatten_layer(input_layer)
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)
first_dense(flatten_layer)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)
output_layer(first_dense)
# declare inputs and outputs
func_model = Model(inputs=input_layer, outputs=output_layer)
return func_model
Why is this so?
Thanks!