Skip to main content

Keras for Tensorflow - An (Re)Introduction 2023

Guangzhou, China

Github Repository

Keras is built on top of TensorFlow 2 and provides an API designed for human beings. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages.

See also:

You can export Keras models to JavaScript to run directly in the browser, to TF Lite to run on iOS, Android, and embedded devices. It's also easy to serve Keras models as via a web API.

pip show tensorflow-gpu

Name: tensorflow-gpu
Version: 2.11.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /usr/lib/python3.10/site-packages
Requires: absl-py, astunparse, flatbuffers, gast, google-pasta, grpcio, h5py, keras, libclang, numpy, opt-einsum, packaging, protobuf, setuptools, six, tensorboard, tensorflow-estimator, tensorflow-io-gcs-filesystem, termcolor, typing-extensions, wrapt
Required-by:
pip show keras

Name: keras
Version: 2.11.0
Summary: Deep learning for humans.
Home-page: https://keras.io/
Author: Keras team
Author-email: keras-users@googlegroups.com
License: Apache 2.0
Location: /usr/lib/python3.10/site-packages
Requires:
Required-by: tensorflow-gpu

Building a Keras Model

We can use Keras to quickly define a Tensorflow neural network with an input and output layer and the dense layers in between. Here is an example for a Sequential Model:

from keras.models import Model, Sequential
from keras.layers import Input, Dense

# Define Sequential model with 3 layers
input_layer = Input(shape=(3,))
dense_layer1 = Dense(4)(input_layer)
dense_layer2 = Dense(4)(dense_layer1)
output = Dense(4)(dense_layer2)

model = Model(inputs = input_layer, outputs=output)
model = Sequential()

model.add(Dense(4, name='dense_layer1', input_shape=(3,)))
model.add(Dense(4, name='dense_layer2'))
model.add(Dense(1, name='output'))

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_layer1 (Dense) (None, 4) 16

dense_layer2 (Dense) (None, 4) 20

output (Dense) (None, 1) 5

=================================================================
Total params: 41
Trainable params: 41
Non-trainable params: 0
_________________________________________________________________

Compiling the Keras Model

The compile method configures the model for training. There are three parameter that are mandatory:

model.compile(optimizer='adam', loss='mean_squared_error', metrics=('accuracy'))

Optimizer

An optimizer is one of the two arguments required for compiling a Keras model. The most often used optimizer is Adam. The optimization used is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments. The method is computationally efficient, has little memory requirement, invariant to diagonal rescaling of gradients, and is well suited for problems that are large in terms of data/parameters.

Losses

The purpose of loss functions is to compute the quantity that a model should seek to minimize during training.

Metrics

A metric is a function that is used to judge the performance of your model. Metric functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model. Note that you may use any loss function as a metric.

Training the Keras Model

The Fit Method trains the model for a fixed number of epochs (iterations on a dataset):

# training the model
model.fir(X, y, batch_size=32, epochs=12)

Evaluating the Model Performance

The Evaluate Method returns the loss value & metrics values for the model in test mode. And the Predict Method generates output predictions for the input samples. Computation is done in batches. This method is designed for batch processing of large numbers of inputs. It is not intended for use inside of loops that iterate over your data and process small numbers of inputs at a time:

# evaluate model performance
model.evaluate(Xval, yval)
model.predict(Xtest)

We take the validation part of our dataset and run predictions on it after training our model with the testing part of the dataset.