Functional architectures
Represent a graph with branching, merging, multiple inputs, or multiple outputs.
Keras · Class
keras.Model groups layers into a trainable object with built-in training and inference features. Create it with the Functional API, by subclassing it, or use Sequential for a simple layer stack.
keras.Model represents a trainable model assembled from layers. You can construct it from input and output tensors with the Functional API or subclass it and implement call(); afterward, compile it for losses and metrics, train with fit(), and predict with predict().
keras.Model()| Name | Type | Description |
|---|---|---|
inputs | keras.Input tensor, or dict/list/tuple of input tensors | Input tensor or supported combination of input tensors used by the Functional API. |
outputs | Tensor, or dict/list/tuple of tensors | Output tensor or supported combination of output tensors produced from the inputs. |
name | string | Optional model name. |
A Keras model object representing the supplied computation graph or subclass implementation.
import keras
inputs = keras.Input(shape=(4,))
x = keras.layers.Dense(8, activation="relu")(inputs)
outputs = keras.layers.Dense(1)(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="mse")
model.fit([[1, 2, 3, 4]], [[0]], epochs=1, verbose=0)
print(model.predict([[1, 2, 3, 4]], verbose=0))Creates a functional model, configures its optimizer and loss, trains it briefly, and runs inference.
Represent a graph with branching, merging, multiple inputs, or multiple outputs.
Define custom forward computation and model-specific behavior.
Reuse a graph segment as an independently callable model.
Configure, train, and run inference with Keras built-in workflows.
The Functional API builds a graph by starting with Input, applying layers to tensors, and passing the resulting inputs and outputs to keras.Model. Subclassing instead defines layers in __init__() and the forward pass in call().
Follow-up: When is Sequential preferable? It is the special case where the model is purely a stack of single-input, single-output layers.
After construction, compile() configures losses and metrics, fit() trains the model, and predict() performs inference. These built-in operations are part of the model’s training and inference interface.
Follow-up: How can behavior differ between training and inference in a subclass? Add an optional boolean training argument to call() and branch on it.
A Functional model can be created from intermediate tensors, which allows extracting a sub-component of an existing model. The new model reuses the selected graph endpoints.
Follow-up: What input structures can the Functional API accept? It supports dicts, lists, and tuples of input tensors.
keras.Inputkeras.Sequentialkeras.layers.Layerkeras.Model.compilekeras.Model.fitkeras.Model.predictkeras.Model.call