A subclassed model gives you maximum control by defining a Python class, creating its layers in __init__(), and writing the forward pass in call(). Use it when a model cannot be expressed as a directed acyclic graph of layers; otherwise, the Functional API is usually the better tradeoff. section 7.2.3 section 7.2.5
A multi-output ticket model
CustomerTicketModel subclasses keras.Model, creates concatenation, mixing, priority-scoring, and department-classification layers in __init__(), then combines title, text_body, and tags in call(). Calling the instance creates its weights. Compile, train, evaluate, and predict it with the standard Keras methods. section 7.2.3
Use this workflow:
- Subclass keras.Model and call super().__init__(). section 7.2.3
- Instantiate reusable layers and other model components in __init__(). section 7.2.3
- Implement the forward computation in call(), including arbitrary Python control flow such as loops or recursion. section 7.2.3
- Call the model on data to create weights, then use compile(), fit(), evaluate(), and predict(). section 7.2.3
This flexibility increases the error surface: you own more model logic and should expect more debugging. Subclassed models and layers still interoperate with Functional and Sequential models, so a Functional model can contain a subclassed component and vice versa. section 7.2.3 section 7.2.4
Figure 7.1 Progressive disclosure of complexity for model building
Use the progression from Sequential models through the Functional API to model subclassing to choose the simplest workflow that provides enough control. section 7.2
