Model subclassing creates a Keras model by extending the Model class. Define its layers during initialization, describe the forward pass in call, then invoke the instance on data to create its weights. section 7.2.3
Customer ticket model
A subclassed customer-support model combines title, text-body, and tag inputs, mixes their features, then produces both a priority score and a department classification. After instantiation and an initial call, compile it with separate losses, then use fit, evaluate, and predict like other Keras models. section 7.2.3
Use the pattern when you need:
- Full control over model logic, including computation expressed with loops or recursion rather than a directed acyclic graph of layers. section 7.2.3
- A top-level trainable object with fit, evaluate, predict, and model-saving support; layers provide building blocks but do not provide these model methods. section 7.2.3
- Interoperation with Sequential models, Functional models, or subclassed layers, since Keras components can be nested across these patterns. section 7.2.4
Figure 7.1 Progressive disclosure of complexity for model building
Use the complexity spectrum to compare Sequential models, the Functional API, and model subclassing before choosing an implementation pattern. section 7.2
This flexibility increases the error surface: subclassing requires responsibility for more model logic and therefore more debugging. When a model fits a directed acyclic graph of layers, the Functional API is generally preferred for its balance of usability, flexibility, and layer-connectivity access. section 7.2.3 section 7.2.5
