Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Discuss how the Keras functional API can be used to create layers using Python
TensorFlow is a machine learning framework provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.
The 'tensorflow' package can be installed on Windows using the below line of code −
pip install tensorflow
Keras is a deep learning API written in Python. It is a high-level API that has a productive interface that helps solve machine learning problems. It runs on top of the TensorFlow framework and provides essential abstractions and building blocks for developing machine learning solutions.
Keras is already present within the TensorFlow package. It can be accessed using the below line of code −
import tensorflow as tf from tensorflow import keras
Keras Functional API Overview
The Keras functional API helps create models that are more flexible compared to models created using the Sequential API. The functional API can work with models that have non-linear topology, can share layers, and work with multiple inputs and outputs. A deep learning model is usually a directed acyclic graph (DAG) that contains multiple layers.
Creating Layers with Functional API
Here's how to create layers using the Keras functional API −
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Create input layer
inputs = keras.Input(shape=(784,))
print("Input layer created")
# Create image input (for demonstration)
img_inputs = keras.Input(shape=(32, 32, 3))
# Display input properties
print("Dimensions of input:", inputs.shape)
print("Data type of input:", inputs.dtype)
print("\nBuilding layers...")
# Create Dense layers
dense1 = layers.Dense(64, activation="relu")
x = dense1(inputs)
# Add another Dense layer
x = layers.Dense(64, activation="relu")(x)
# Output layer
outputs = layers.Dense(10)(x)
print("Creating model...")
# Create the model
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
print("\nModel summary:")
model.summary()
Input layer created
Dimensions of input: (None, 784)
Data type of input: <dtype: 'float32'>
Building layers...
Creating model...
Model summary:
Model: "mnist_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 784)] 0
dense (Dense) (None, 64) 50240
dense_1 (Dense) (None, 64) 4160
dense_2 (Dense) (None, 10) 650
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________
How It Works
Key Features
The functional API provides several advantages −
Layer Reusability: The same layer instance can be called multiple times
Multiple Inputs/Outputs: Models can have multiple input and output tensors
Non-linear Topology: Support for complex architectures with branches and merges
Layer Sharing: Layers can be shared between different parts of the model
Model Creation Steps
Create Input: Use
keras.Input()to define the input shapeBuild Layers: Create and connect layers by calling them on tensors
Define Model: Use
keras.Model()to specify inputs and outputsInspect Model: Use
model.summary()to view the architecture
Conclusion
The Keras functional API provides a flexible way to create complex neural network architectures. It allows layer reusability, multiple inputs/outputs, and non-linear topologies, making it ideal for advanced deep learning models.
