Google News
logo
Keras - Interview Questions
How do you configure a neural network in Keras?

In Keras, you can configure a neural network through two approaches:

1. Sequential Model Configuration : This method is suitable for simple network architectures characterized by a linear, one-input-one-output stacking of layers.

Code Example : Sequential Model
Here is the Python code:
import keras
from keras import layers

# Initialize a sequential model
model = keras.Sequential()

# Add layers one by one
model.add(layers.Dense(64, activation='relu', input_shape=(784,)))
model.add(layers.Dense(10, activation='softmax'))?


2. Functional API Model Configuration : This method is the recommended choice for more complex architecture designs such as multi-input and multi-output models, layer sharing, and non-linear topologies.

Code Example : Functional API Model
Here is the Python code:
import keras
from keras import layers

# Input tensor placeholder
inputs = keras.Input(shape=(784,))

# Hidden layers
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)

# Output layer for 10-class classification
outputs = layers.Dense(10, activation='softmax')(x)

# Define the model
model = keras.Model(inputs=inputs, outputs=outputs)?
Advertisement