Declaring and stacking layers

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!

Return of the above call is not captured.

Hello Mr. Ambresh,
So this syntax does not work for the input layer? I tried removing it from the input layer and only applying it to the dense layert with flatten_layer as it parameter. Even then I get the error.
Here is the error:
AttributeError: ‘Dense’ object has no attribute 'shape

Rohit. The expression tf.keras.layers.Flatten() (input_layer) does 2 things:

  1. Creates an instance of Flatten class
  2. Invokes the __call__ method of the object constructed above.

You should capture the return value of the __call__ method.

In the case of Input function, it’s sufficient to capture the return value of the invocation.