Instead of employing the Sequential API, like at the beginning of this chapter, you can instead use the functional API:
model_input = tf.keras.layers.Input(shape=input_shape)
output = tf.keras.layers.Flatten()(model_input)
output = tf.keras.layers.Dense(128, activation='relu')(output)
output = tf.keras.layers.Dense(num_classes, activation='softmax')(output)
model = tf.keras.Model(model_input, output)
Notice that the code is slightly longer than it previously was. Nevertheless, the functional API is much more versatile and expressive than the Sequential API. The former allows for branching models (that is, for building architectures with multiple parallel layers for instance), while the latter can only be used for linear models. For even more flexibility, Keras also offers the possibility to subclass the Model class, as described in Chapter 3, Modern Neural Networks.
Regardless of how a Model object is built, it is composed of layers. A layer...