liveBook · Manning

Model subclassing

As explained in Deep Learning with R, Third Edition

A model subclass is a Keras model class created by extending the Keras Model class and defining its computation in a call method.

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
figure

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

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains initialization, forward-pass definition, weight creation, training, composition, and debugging tradeoffs without detailing framework internals.
Examples
Uses a multi-input customer-ticket model with priority and department outputs, plus nested subclassed and Functional models.
What it assumes
Assumes familiarity with Keras models, layers, compile, fit, evaluate, and predict while extending the related Layer-subclassing workflow.
What you can do after
Build it from scratch, compile it, train it, evaluate it, predict with it, and combine it with other Keras model patterns.
How it connects
Connects model subclassing to Keras layers, Sequential models, the Functional API, custom losses, metrics, and training loops.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.