Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

keras.Model

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.

How it works

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().

Syntax

keras.Model()

Parameters

NameTypeDescription
inputskeras.Input tensor, or dict/list/tuple of input tensorsInput tensor or supported combination of input tensors used by the Functional API.
outputsTensor, or dict/list/tuple of tensorsOutput tensor or supported combination of output tensors produced from the inputs.
namestringOptional model name.

Returns

A Keras model object representing the supplied computation graph or subclass implementation.

Key points

  • Model groups layers into one object that provides both training and inference capabilities.
  • The documented construction approaches are the Functional API, subclassing Model, and Sequential as a specialized model case.
  • The Functional API describes computation by chaining layer calls from Input tensors.
  • Intermediate tensors can serve as endpoints for quickly extracting model sub-components.
  • A subclass can accept a boolean training argument in call() to distinguish training behavior from inference behavior.
  • compile() configures losses and metrics before the model is trained or used for prediction.

Common mistakes

  • Functional-model inputs must be supplied as dictionaries, lists, or tuples of tensors; other container shapes are not supported.
  • When subclassing, initialize layers in __init__() and put the forward computation in call().

Example: Functional model with training and prediction

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.

When to use it

Functional architectures

Represent a graph with branching, merging, multiple inputs, or multiple outputs.

Model subclassing

Define custom forward computation and model-specific behavior.

Submodel extraction

Reuse a graph segment as an independently callable model.

Standard model lifecycle

Configure, train, and run inference with Keras built-in workflows.

Interview questions

QHow can keras.Model be created?

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.

QWhat are the main steps after creating a model?

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.

QWhy use intermediate tensors when creating a Functional model?

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.Input
  • keras.Sequential
  • keras.layers.Layer
  • keras.Model.compile
  • keras.Model.fit
  • keras.Model.predict
  • keras.Model.call

Source: The Model class - Keras. Checked 2026-10-15.