liveBook · Manning

Subclassed layer

As explained in Deep Learning with Python, Third Edition

A Keras workflow pattern where models or layers are defined by writing Python classes or functions directly instead of assembling prebuilt layers with Sequential or the Functional API.

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
progressive disclosure of complexity models

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

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines initialization, forward execution, weight creation, training, interoperability, and debugging tradeoffs.
Examples
Uses a multi-input customer-support ticket model with priority scoring and department classification.
What it assumes
Assumes familiarity with Keras training methods and Layer subclassing while reinforcing the Model subclassing pattern.
What you can do after
Apply it via Keras to build, train, evaluate, export, and predict with custom model objects.
How it connects
Connects Keras Model, Layer subclassing, Sequential models, the Functional API, and directed acyclic graphs of layers.

Related concepts

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