0
0
TensorFlowml~5 mins

Functional API basics in TensorFlow

Choose your learning style9 modes available
Introduction
The Functional API lets you build flexible machine learning models by connecting layers like building blocks. It helps create models with multiple inputs or outputs easily.
You want to build a model with more than one input, like images and text together.
You need a model that has multiple outputs, such as predicting both price and category.
You want to create complex models with shared layers or non-linear connections.
You want clear and easy-to-read model structure for debugging or sharing.
You want to reuse parts of a model in different places.
Syntax
TensorFlow
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model

inputs = Input(shape=(input_size,))
x = Dense(units, activation='relu')(inputs)
outputs = Dense(output_size, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
Use Input() to define the shape of your input data.
Connect layers by calling them like functions on tensors.
Examples
A simple model with one input layer of size 10, one hidden layer with 32 units, and one output unit for binary classification.
TensorFlow
inputs = Input(shape=(10,))
x = Dense(32, activation='relu')(inputs)
outputs = Dense(1, activation='sigmoid')(x)
model = Model(inputs, outputs)
A model with two inputs of different sizes that are processed separately and then combined before the output.
TensorFlow
import tensorflow as tf
input1 = Input(shape=(20,))
input2 = Input(shape=(5,))
x1 = Dense(16, activation='relu')(input1)
x2 = Dense(8, activation='relu')(input2)
concat = tf.keras.layers.concatenate([x1, x2])
outputs = Dense(3, activation='softmax')(concat)
model = Model([input1, input2], outputs)
Sample Model
This example builds a simple model with one input layer of size 4, one hidden layer, and an output layer for 3 classes. It trains on random data and prints predictions for new random inputs.
TensorFlow
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
import numpy as np

# Define input shape
inputs = Input(shape=(4,))

# Add a hidden layer
x = Dense(8, activation='relu')(inputs)

# Output layer for 3 classes
outputs = Dense(3, activation='softmax')(x)

# Create the model
model = Model(inputs=inputs, outputs=outputs)

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Generate some random data
x_train = np.random.random((100, 4))
y_train = np.random.randint(0, 3, 100)

# Train the model
history = model.fit(x_train, y_train, epochs=3, batch_size=10, verbose=0)

# Make predictions on new data
x_test = np.random.random((5, 4))
predictions = model.predict(x_test)

# Print predictions
print(predictions)
OutputSuccess
Important Notes
The Functional API is more flexible than the Sequential API and can handle complex models.
Always define inputs first, then connect layers step-by-step.
You can name layers and inputs for easier debugging.
Summary
The Functional API helps build flexible and complex models by connecting layers explicitly.
Use Input() to start and Model() to finalize your model.
It supports multiple inputs and outputs, unlike the simpler Sequential API.