Functional architectures
Build a directed model graph from one or more input tensors and computed output tensors.
TensorFlow · Class
tf.keras.Model groups layers into a callable object with training and inference features. Create it with the Functional API or by subclassing it.
tf.keras.Model groups layers into a callable model that supports training and inference. You can construct it from input/output tensors with the Functional API or subclass it by defining layers in __init__() and the forward pass in call().
tf.keras.Model(*args, **kwargs)| Name | Type | Description |
|---|---|---|
*args, **kwargs | Any | Positional and keyword arguments accepted by the Model constructor. |
A Keras Model object grouping layers and exposing training and inference features.
import tensorflow as tf
from tensorflow import keras
inputs = keras.Input(shape=(37,))
x = keras.layers.Dense(32, activation="relu")(inputs)
outputs = keras.layers.Dense(5, activation="softmax")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.summary()Creates a functional model, configures its training behavior, and prints its architecture.
Build a directed model graph from one or more input tensors and computed output tensors.
Implement custom forward-pass behavior by defining layers and call().
Configure, train, evaluate, and use neural networks for prediction.
A Model groups layers and provides training and inference functionality. It can be built through the Functional API or by subclassing Layer-like behavior.
Follow-up: How would you choose between Functional API and subclassing? Use the Functional API for graph-shaped models with explicit inputs and outputs; subclass when custom forward-pass logic is needed.
The Functional API starts with Input tensors, applies layers to create outputs, and constructs the model from those inputs and outputs. The resulting model remains callable like a layer.
Follow-up: Can models be nested? Yes. A model can be used as a layer, so one model can contain another model.
After construction, call compile() to configure losses and metrics, fit() to train, and predict() to generate predictions. These are built-in model operations.
Follow-up: What other standard operation evaluates a trained model? evaluate() returns loss and metric values configured through compile().
tf.keras.layers.Layertf.keras.Inputtf.keras.Sequentialtf.keras.Model.compiletf.keras.Model.fittf.keras.Model.predicttf.keras.Model.evaluate