Keras: TensorFlow's Model-Building API
Learn about the TensorFlow's Keras model-building API.
We'll cover the following
Keras was developed as a separate library that provides high-level building blocks to build models conveniently. It was initially platform agnostic and supported many softwares (for example, TensorFlow and Theano).
However, TensorFlow acquired Keras, and now Keras is an integral part of TensorFlow for building models effortlessly.
APIs provided by Keras
Keras’s primary focus is model building. For that, Keras provides several different APIs with varying degrees of flexibility and complexity. Choosing the right API for the job will require a sound knowledge of the limitations of each API as well as experience. The APIs provided by Keras are:
-
Sequential API: This is the most easy-to-use API. In this API, we simply stack layers on top of each other to create a model.
-
Functional API: The functional API provides more flexibility by allowing us to define custom models that can have multiple input layers and multiple output layers.
-
Subclassing API: The subclassing API enables us to define custom reusable layers and models as Python classes. This is the most flexible API, but it requires strong familiarity with the API and raw TensorFlow operations to use it correctly.
Note: Don’t confuse the Keras TensorFlow submodule with the external Keras library. They share roots in terms of where they’ve come from, but they’re not the same. We’ll run into strange issues if we treat them the same during our development. We will exclusively use
tf.keras
.
One of the most innate concepts in Keras is that a model is composed of one or more layers connected in a specific way. Here, we’ll briefly go through what the code looks like, using different APIs to develop models. We aren’t expected to fully understand the code below. Rather, we’ll focus on the code style to spot any differences between the three methods.
Sequential API
When using the sequential API, we simply define our model as a list of layers. Here, the first element in the list is the closest to the input, where the last is the output layer:
Get hands-on with 1400+ tech skills courses.