The design of the Keras API is guided by something called “the progressive disclosure of complexity”. In simple english, it means: make it easy to get started, yet make it possible to handle high-complexity use cases.
Because of this, there’s not correct “true” way of using Keras. Instead, Keras from the ground up is designed to be very flexible. As an example, here are a few ways to build Keras Models
Sequential model: The most approachable API - basically a Python list. Because of this, it’s limited to simple stacks of layers
Functional API: the focus is on graph-like model architectures. It represents a nice mid-point between usability and flexibility, and because of this… It’s the most commonly used model-building API
Model subclassing: low-level option where you write everything yourself from scratch. This is ideal if you want full control over every little thing… However, you won’t get access to many built-in Keras features, and you will be more at risk of making mistakes.
Let’s do a quick deep dive into each.
1 - Sequential Model
The easiest way to build a Keras model is to use the sequential model, this is basically what we’ve been doing so far. Here’s a quick example:
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(32, activation='relu'),
layers.Dense(5, activation='softmax')
])
model.build(input_shape=(None, 3))
Once we have a model, sometimes we would like to see how many parameters it has per layer, and what the input/output shapes are that this neural network expects.
To do this, we will use model.summary() to get it to display this information
You’ll notice up above it says Model: “sequential”, and dense, dense_1. In keras, we can actually name our model, and the layers. To do this, we will use name = “….” Code, and example below
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential(name='Raptor Model')
model.add(layers.Dense(32,activation='relu',name='my_layer'))
model.add(layers.Dense(5,activation='relu',name='my_other_layer'))
model.build(input_shape=(None, 3))
model.summary()
And…. that’s pretty much all you need for the sequential models. It’s the most straight forward way of building a model, and is great for beginners
2 - Functional API
The sequential model is quite easy to use, but it’s applicability is quite small…. It can only express models with a single input, and a single output, applying 1 layer after the other in a sequential output. But, what if we wanted to deal with a model with multiple inputs, and multiple outputs. In that case, the sequential model won’t get the job done, so we’ll have to use the Functional API instead.
Let’s do the exact same example from above, but with the functional API instead. Code below:
import keras
from tensorflow import *
from tensorflow.keras import *
inputs = keras.Input(shape=(3,),name='raptor_inputs')
features = keras.layers.Dense(32,activation='relu')(inputs)
outputs=keras.layers.Dense(5,activation='softmax')(features)
model=keras.Model(inputs=inputs,outputs=outputs)
Here’s the TLDR of what’s happening
We start off by declaring an input, with it’s name, and shape (this is called a symbolic tensor)
We then create a layer, and called it on the input above. All keras layers can be called both on real tensors of data, and on these symbolic tensors.
After obtaining the final outputs, we instantiated the model by specifying its inputs and outputs in the Model constructor
As before, we can find the summary of the model by doing model.summary()
Now let’s do the thing that the functional API is good at. Making multi-input, multi-output models.
2.1 Creating a Multi-input, multi-output Functional model
Here’s an example code we’ll take a look at, which takes in multiple inputs, and outputs multiple outputs. Here’s an example of what we are doing:
Keep reading with a 7-day free trial
Subscribe to Data Science & Machine Learning 101 to keep reading this post and get 7 days of free access to the full post archives.