Google News
logo
Keras - Interview Questions
Explain the difference between sequential and functional APIs in Keras.
While Sequential and Functional APIs in Keras both facilitate the creation of neural networks, they differ in flexibility, complexity of models that can be built, and the way each API is used.

Key Distinctions :
* Control Flow :
* Sequential: Fixed.
* Functional: Arbitrary, using Keras layers.
* Model Complexity :
* Sequential: Suited for straight, linear stacks of layers where each layer has one input and one output.
* Functional: For complex, multi-input, multi-output architectures, including shared layers and branches.
* Use Case :
* Sequential: Ideal for beginners and linear networks.
* Functional: Better for advanced architectures and research.
* Evaluation and Inference :
Sequential : Simple.
* For evaluation, use model.evaluate().
* For prediction, use model.predict().
Functional : Potentially complex, with custom feedback mechanisms.
* For evaluation, use model.evaluate() and possibly custom measures.
* For prediction, typically model.predict(), but can involve multi-input or multi-output structures.

Benefits of Using the Functional API
Multi-Modal Inputs and Multi-Output Structures :
* Models can process different types of data through the use of multiple input layers.
Useful for tasks like joint sentiment analysis and emotion recognition from text and images in social media data.
Model Branching :
* Encourages the creation of 'branching' or 'dual pathway' models where different inputs may follow separate paths before being combined.
Model Sharing with Multiple Inputs and Outputs:
* Supports models where multiple output layers could be predicted simultaneously using the same internal state from the shared layers.
Non-Sequential Connectivity :
* Non-linear connections can exist between layers.
Custom Loss Functions and Model Metrics :
* Allows for the computation of more complicated loss functions to handle the multiple outputs or inputs.
Layer Sharing and Resue :
* Layers can be reused across different parts of the model, making it easier to work with complex architectures.

Code Example: Multi-Input/Output Model using Functional API :

Here is the Python code :
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Define input for numerical data
numerical_input = keras.Input(shape=(2,), name="numerical_input")

# Define input for categorical data
categorical_input = keras.Input(shape=(3,), name="categorical_input")

# Multi-modal input concatenation
concatenation = layers.Concatenate()([numerical_input, categorical_input])

# Hidden layer
hidden1 = layers.Dense(3, activation='relu')(concatenation)

# Define two output branches from the hidden layer
output1 = layers.Dense(1, name="output1")(hidden1)
output2 = layers.Dense(1, name="output2")(hidden1)

# Create the model
model = keras.Model(inputs=[numerical_input, categorical_input], outputs=[output1, output2])

# Compile the model
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.01),
              loss={"output1": "mse", "output2": "mse"},
              metrics={"output1": "mae", "output2": "mae"})?
Advertisement