Functional architectures
Build arbitrary computation graphs by connecting inputs, layers, and outputs.
Keras · Class
Keras Model groups layers into an object that supports training and inference. Create one with the Functional API, by subclassing Model, or through the Sequential specialization.
Model groups layers into a trainable object. You can create it from connected input and output tensors with the Functional API, or subclass it by defining layers in __init__() and the forward computation in call(). After construction, configure it with compile(), train with fit(), evaluate or predict, and use its saving and serialization capabilities.
keras.Model(inputs=None, outputs=None, name=None)| Name | Type | Description |
|---|---|---|
inputs | keras.Input tensor, or a dict, list, or tuple of input tensors | Input tensor or supported structure of input tensors used to define the model graph. |
outputs | Tensor, or a dict, list, or tuple of tensors | Output tensor or supported structure of tensors produced from the model inputs. |
name | string | Optional human-readable model name. |
A Keras Model instance.
import keras
inputs = keras.Input(shape=(4,))
x = keras.layers.Dense(8, activation="relu")(inputs)
outputs = keras.layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="binary_crossentropy")
model.fit([[0, 1, 0, 1]], [[1]], epochs=1, verbose=0)Builds a Functional API model, configures training, and runs one training step.
Build arbitrary computation graphs by connecting inputs, layers, and outputs.
Implement custom forward-pass behavior and complex research models.
Configure optimization, train data, and generate predictions through one object.
Model represents a group of layers and adds training and inference features. A Functional model is built by connecting Input tensors through layers and passing the resulting inputs and outputs to keras.Model.
Follow-up: How would you implement a model with custom forward logic? Subclass keras.Model, define layers in __init__(), and implement the computation in call().
The Functional API starts with keras.Input, chains layer calls to describe the forward pass, and constructs the model from inputs and outputs. The resulting object supports standard model operations such as compile(), fit(), and predict().
Follow-up: Can a Functional model expose an intermediate computation? Yes; intermediate tensors can be used to create a new Functional model for extracting a subcomponent.
Subclassing is appropriate when the model's computation is not conveniently represented as a simple layer graph. Layers belong in __init__(), while call() defines the forward pass; the resulting model still provides training and inference APIs.
Follow-up: What simpler model type is available for a pure layer stack? keras.Sequential, which is a special Model case for single-input, single-output stacks.
keras.Sequentialkeras.Inputkeras.layers.LayerModel.compile()Model.fit()Model.predict()