🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

tf.keras.Model()

AI & DATA SCIENCE // tf-keras-model

tf.keras.Model() groups layers into an object with training and inference features, and is the base class underlying every Keras model, including the Functional API.

Syntax

tf.keras.Model(inputs, outputs)

Deep Dive Course

Used directly, tf.keras.Model(inputs, outputs) builds a model with the Functional API — you first define one or more Input tensors, then call layers on them as functions, chaining and branching them however you need, and finally wrap the whole computation graph by passing its input and output tensors to Model(). This unlocks architectures a Sequential model can't express, like multiple inputs, multiple outputs, shared layers, and non-linear connections such as residual/skip connections. Model() is also commonly subclassed directly for full custom control, defining layers in __init__ and the forward pass in a call() method.

1Understanding tf.keras.Model()

Used directly, tf.keras.Model(inputs, outputs) builds a model with the Functional API — you first define one or more Input tensors, then call layers on them as functions, chaining and branching them however you need, and finally wrap the whole computation graph by passing its input and output tensors to Model(). This unlocks architectures a Sequential model can't express, like multiple inputs, multiple outputs, shared layers, and non-linear connections such as residual/skip connections. Model() is also commonly subclassed directly for full custom control, defining layers in __init__ and the forward pass in a call() method.

💡

Subclassing tf.keras.Model() directly, defining layers in __init__ and the forward pass in call(), gives you the same flexibility as writing raw, code-driven model logic, useful when a model's forward pass involves logic too dynamic to express as a static Functional-API graph.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Model

inputs = tf.keras.Input(shape=(32,))
x = layers.Dense(64, activation='relu')(inputs)
outputs = layers.Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
print(model.output_shape)
localhost:3000

2Practical Example

Here is a real-world application of tf.keras.Model() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Model

class MyModel(Model):
    def __init__(self):
        super().__init__()
        self.dense1 = layers.Dense(64, activation='relu')
        self.dense2 = layers.Dense(10, activation='softmax')

    def call(self, inputs):
        x = self.dense1(inputs)
        return self.dense2(x)

model = MyModel()
print(isinstance(model, tf.keras.Model))
localhost:3000

3Best Practices

Follow these guidelines when working with tf.keras.Model():

1. Use the Functional API, tf.keras.Model(inputs, outputs), for static graphs with branching or multiple inputs/outputs, and subclass Model() directly only when you need truly dynamic, code-driven forward-pass logic

2. Give each Input layer and named output a clear name so multi-input/multi-output models remain easy to work with when compiling and fitting

3. Reuse the same layer instance on multiple tensors when you want shared weights across different parts of the architecture, a pattern only the Functional/subclassing approach supports

⚠️

Tip: Subclassing tf.keras.Model() directly, defining layers in __init__ and the forward pass in call(), gives you the same flexibility as writing raw, code-driven model logic, useful when a model's forward pass involves logic too dynamic to express as a static Functional-API graph.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Model

inputs = tf.keras.Input(shape=(32,))
x = layers.Dense(64, activation='relu')(inputs)
outputs = layers.Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
print(model.output_shape)
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf
from tensorflow.keras import layers, Model

inputs = tf.keras.Input(shape=(32,))
x = layers.Dense(64, activation='relu')(inputs)
outputs = layers.Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
print(model.output_shape)
Example 02Advanced Example
import tensorflow as tf
from tensorflow.keras import layers, Model

class MyModel(Model):
    def __init__(self):
        super().__init__()
        self.dense1 = layers.Dense(64, activation='relu')
        self.dense2 = layers.Dense(10, activation='softmax')

    def call(self, inputs):
        x = self.dense1(inputs)
        return self.dense2(x)

model = MyModel()
print(isinstance(model, tf.keras.Model))

Best Practices

  • Use the Functional API, tf.keras.Model(inputs, outputs), for static graphs with branching or multiple inputs/outputs, and subclass Model() directly only when you need truly dynamic, code-driven forward-pass logic
  • Give each Input layer and named output a clear name so multi-input/multi-output models remain easy to work with when compiling and fitting
  • Reuse the same layer instance on multiple tensors when you want shared weights across different parts of the architecture, a pattern only the Functional/subclassing approach supports

Interview Question

What's the key architectural limitation of a Sequential model that the Functional API, built on tf.keras.Model(), removes?

Hint: Think about how many inputs/outputs each layer can have, and whether outputs can be combined.

A Sequential model can only represent a single, linear chain where each layer takes exactly one input, the previous layer's output, and produces exactly one output feeding into the next layer — there's no way to give a layer two separate inputs, to route one layer's output to two different downstream layers, or to merge two different tensors back together. The Functional API removes this limitation entirely by treating each layer as a plain function you call explicitly on a tensor, letting you freely wire together multiple inputs, multiple outputs, shared layers used more than once, and merge operations like Add or Concatenate, then package the whole resulting computation graph into a single Model() by specifying its overall input and output tensors.

Exercises

MediumPractice using tf.keras.Model() in a real scenario.
View Solution
import tensorflow as tf
from tensorflow.keras import layers, Model

inputs = tf.keras.Input(shape=(32,))
x = layers.Dense(64, activation='relu')(inputs)
outputs = layers.Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
print(model.output_shape)

Frequently Asked Questions

What's the key architectural limitation of a Sequential model that the Functional API, built on tf.keras.Model(), removes?

A Sequential model can only represent a single, linear chain where each layer takes exactly one input, the previous layer's output, and produces exactly one output feeding into the next layer — there's no way to give a layer two separate inputs, to route one layer's output to two different downstream layers, or to merge two different tensors back together. The Functional API removes this limitation entirely by treating each layer as a plain function you call explicitly on a tensor, letting you freely wire together multiple inputs, multiple outputs, shared layers used more than once, and merge operations like Add or Concatenate, then package the whole resulting computation graph into a single Model() by specifying its overall input and output tensors.

Related Functions

tf-keras-sequentialmodel-compilegradienttape