A Beginners Guide to Keras, a High-Level API for TensorFlow 2
Keras is a high-level API for TensorFlow 2. Keras was developed by François Chollet at Google. Keras has become extremely popular for fast prototyping, for building and training deep learning models, and for research and production. Keras is a very rich API; it supports eager execution and data pipelines, and other features.
Keras has been available for TensorFlow since 2017, but its use has been extended and further integrated into TensorFlow with the release of TensorFlow 2.0. TensorFlow 2.0 has embraced Keras as the API of choice for the majority of deep learning development work.
It is possible to import Keras as a standalone module, but we will concentrate on using Keras from within TensorFlow 2. The module is, thus, tensorflow.keras
In this blog post, we will cover the following topics:
- Advantages of Keras
- Features of Keras
- Keras data types
- Keras models and
- Keras datasets
Advantages of Keras
Keras has a number of advantages, including the following:
- It's designed for both new users and experts alike, offering consistent and simple APIs.
- It's user friendly with a simple, consistent interface that is optimized for common use cases.
- It provides excellent feedback for user errors that are easily understood and often accompanied by helpful advice.
- It's modular and composable; models in Keras are constructed by joining up configurable building blocks
- It's easy to extend by writing custom building blocks
.
Features of Keras
If you want to know which version of Keras came with your TensorFlow, use the following command:
import tensorflow as tf
print(tf.keras.__version__)
At the time of writing, this produced the following:
2.2.4-tf
Keras included built-in support for multi-GPU data parallelism, and also the fact that Keras models can be turned into TensorFlow Estimators and trained on clusters of GPUs on Google Cloud.
Keras is, perhaps, unusual in that it is has a reference implementation maintained as an independent open source project, located at www.keras.io.
It's maintained independently of TensorFlow, although TensorFlow does have a full implementation of Keras in the
tf.keras
module. The implementation has TensorFlow-specific augmentations, including support for eager execution, by default.Eager execution means that the execution of code is an imperative programming environment, rather than a graph-based environment. This imperative style allows for intuitive debugging, fast development iteration, support for the TensorFlow
SavedModel
format, and built-in support for distributed training on CPUs, GPUs, and even Google's own hardware, Tensor Processing Units (TPUs).The TensorFlow implementation also has support for
tf.data
,
distribution strategies, exporting models, which can be deployed on
mobile via TensorFlow Lite, and feature columns for
representing and classifying structured data.Keras data types
Keras data types (dtypes) are the same as TensorFlow data types, as shown in the following table:
Python type | Description |
---|---|
tf.float16 | 16-bit floating-point |
tf.float32 | 32-bit floating-point |
tf.float64 | 64-bit floating-point |
tf.int8 | 8-bit signed integer |
tf.int16 | 16-bit signed integer |
tf.int32 | 32-bit signed integer |
tf.int64 | 64-bit signed integer |
tf.uint8 | 8-bit unsigned integer |
tf.string | Variable-length byte arrays |
tf.bool | Boolean |
tf.complex64 | A complex number made of two 32-bit floating points—one real and imaginary part |
tf.complex128 | A complex number made of two 64-bit floating points—one real and one imaginary part |
tf.qint8 | An 8-bit signed integer used in quantized Ops |
tf.qint32 | 32-bit signed integer used in quantized Ops |
tf.quint8 | An 8-bit unsigned integer used in quantized Ops |
Keras models
Keras is based on the concept of a neural network model. The predominant model is called a Sequence, which is a linear stack of layers. There is also a system using the Keras functional API.
The Keras Sequential model
To build a KerasSequential
model, you add layers to it in the same order that you want the computations to be undertaken by the network.After you have built your model, you compile it; this optimizes the computations that are to be undertaken, and is where you allocate the optimizer and the loss function you want your model to use.
The next stage is to fit the model to the data. This is commonly known as training the model and is where all the computations take place.
Next, you evaluate your model to establish its accuracy, loss, and other metrics. Finally, having trained your model, you can use it to make predictions on new data. So, the workflow is: build, compile, fit, evaluate, make predictions.
The Keras functional API
The functional API lets you build much more complex architectures than the simple linear stack ofSequential
models. It also supports more advanced models.
These models include multi-input and multi-output models, models with
shared layers, and models with residual connections.Keras datasets
The following datasets are available from within Keras:
boston_housing
, cifar10
, cifar100
, fashion_mnist
, imdb
, mnist
, and reuters
.They are all accessed with the
load_data()
function. For example, to load the cifar10
dataset, use the following:(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
Leave a Comment