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.
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)2Practical Example
Here is a real-world application of tf.keras.Model() showing how it is used in production TensorFlow code.
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))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.
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)