Day 7 - Building Dynamic Models with the Keras Subclassing API
-
Sequential and Functional APIs are declarative: first declare which
layers are required and how they are connected. Data can be fed only
after that. Model is a static graph of layers.
-
Subclassing API allows an imperative programming style. Loops,
varying shapes, conditional branching and other dynamic behaviors
can be added.
-
To use the Subclassing API:
- Subclass the
keras.Model
class.
- Create the layers you need in the constructor.
-
Perform the computations in the
call()
method.
-
Advantages:
-
High flexibility - anything can be done within the
call()
method.
-
Models can be used as regular layers so they can be combined to
build complex architectures.
-
Disadvantages:
-
Model architecture hidden in
call()
method so Keras
cannot easily inspect, save or clone it.
-
The
summary()
method only gives list of layers and
no information about how they are connected to each other.
-
Keras cannot check types and shapes ahead of time, so it is
easier to make mistakes.