Search code examples
tensorflowdeep-learningcomputer-visionartificial-intelligence

Explain - x = tf.Keras.layers.Dense(128, activation='relu')(pretrained_model.output)


Could anyone explain this code in detail to me, I don't understand the highlighted part. I mean why did they put :

x =  tf.Keras.layers.Dense(128, activation='relu')(pretrained_model.output)

Dense()() followed by another bracket wat could be the reason?

Full Code:

inputs = pretrained_model.input
x = tf.keras.layers.Dense(128, activation='relu')(pretrained_model.output)
outputs = tf.keras.layers.Dense(9, activation='softmax')(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs)

model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

Solution

  • In the first line, you define inputs to be equal to the inputs of the pretrained model. Then you define x to be equal to the pretrained models outputs (after applying an additional dense layer). Tensorflow now automatically recognizes, how inputs and x are connected. If we assume, the the pretrained model consists of the five layers [pretrained_in, pretrained_h_1, pretrained_h_2, pretrained_h_3, pretrained_out], this means that tensorflow realizes, that the information will take the following way:

    inputs -> pretrained_in -> pretrained_h_1 -> pretrained_h_2 -> pretrained_h_3 -> pretrained_out -> new_dense_layer -> x

    If we now take the final layers into account, we will have the following information flow:

    inputs -> pretrained_in -> pretrained_h_1 -> pretrained_h_2 -> pretrained_h_3 -> pretrained_out -> new_dense_layer -> x -> dense_layer_softmax -> outputs

    Now the "model = tf.keras.Model(inputs=inputs, outputs=outputs)" statement just tells tensorflow, that it is supposed to treat this information flow as a new model, so that you can easily pass new information through all of these layers by just using this new model.

    Edit: You asked why Dense is followed by two brackets. The layers.Dense() call is actually not the function that processes your data. Instead, if you call tf.keras.layers.Dense(), tensorflow basically creates a new dense layer and returns it to you, which you can then use to process your data. You could actually write this in two lines to make it more clear:

    dense_layer = layers.Dense(128, activation='relu')   # We define a new dense layer
    dense_layer_output = dense_layer(pretrained_model.output)    # We apply the dense layer to the data