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

TensorFlow · Class

keras.Model

tf.keras.Model groups layers into a callable object with training and inference features. Create it with the Functional API or by subclassing it.

How it works

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

Syntax

tf.keras.Model(*args, **kwargs)

Parameters

NameTypeDescription
*args, **kwargsAnyPositional and keyword arguments accepted by the Model constructor.

Returns

A Keras Model object grouping layers and exposing training and inference features.

Key points

  • Model combines layers with built-in capabilities for training and inference.
  • The documentation describes three construction approaches, including the Functional API and subclassing.
  • The Functional API expresses a forward pass by chaining layers from Input tensors.
  • Finish a functional model by passing its input and output tensors to the constructor.
  • Use compile() to configure the losses and metrics used by the model.
  • Use fit() for training and predict() for generating predictions.

Common mistakes

  • When subclassing Model, put layer definitions in __init__() and the forward computation in call().
  • Configure losses, metrics, and the optimizer with compile() before training.

Example: Functional model

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.

When to use it

Functional architectures

Build a directed model graph from one or more input tensors and computed output tensors.

Subclassed models

Implement custom forward-pass behavior by defining layers and call().

End-to-end model workflows

Configure, train, evaluate, and use neural networks for prediction.

Interview questions

QWhat is tf.keras.Model?

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.

QHow do you create a functional Model?

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.

QHow is a Model trained and used for inference?

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

Source: tf.keras.Model | TensorFlow v2.16.1. Checked 2026-10-15.