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

Keras · Class

Model

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.

How it works

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.

Syntax

keras.Model(inputs=None, outputs=None, name=None)

Parameters

NameTypeDescription
inputskeras.Input tensor, or a dict, list, or tuple of input tensorsInput tensor or supported structure of input tensors used to define the model graph.
outputsTensor, or a dict, list, or tuple of tensorsOutput tensor or supported structure of tensors produced from the model inputs.
namestringOptional human-readable model name.

Returns

A Keras Model instance.

Key points

  • Model combines layers with built-in capabilities for training and inference.
  • Keras documents three construction approaches: Functional API, subclassing Model, and the Sequential special case.
  • The Functional API describes computation by connecting layer calls between input and output tensors.
  • After creation, compile() configures the model's losses and metrics.
  • fit() trains the model, while predict() runs inference on input data.
  • Intermediate tensors can define another Functional model, allowing subcomponents to be extracted.

Common mistakes

  • Functional API inputs must use dictionaries, lists, or tuples; arbitrary nested structures such as lists of lists are unsupported.
  • A subclass should create its layers in __init__() and put forward-pass logic in call().
  • Use Sequential only for a linear single-input, single-output layer stack; choose Functional API or subclassing for other architectures.

Example: Create and train a Functional API model

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.

When to use it

Functional architectures

Build arbitrary computation graphs by connecting inputs, layers, and outputs.

Model subclassing

Implement custom forward-pass behavior and complex research models.

Training and inference

Configure optimization, train data, and generate predictions through one object.

Interview questions

QWhat is the Keras Model class used for?

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

QHow is a Model created with the Functional API?

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.

QWhen should Model subclassing be used?

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

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