Building the Model

Layers can be added using tf.keras.layers and specifying the layer to be added. Again, if you’ve worked with Keras, this will be very intuitive. We start by initializing the layers Sequential and afterward adding the different layers. Below, we’ll show how that can be accomplished.

Compiling the Model

The next step is usually to compile the model by specifying the loss , optimizer , and evaluation metrics. Let’s show how a model can be compiled using the adam optimizer, using sparse_categorical_crossentropy loss, and identifying accuracy as metrics .

Training the Model

In order to train the model, we’ll need to pass in the training data, its labels, and the number of epochs.

Evaluate the Accuracy

Evaluating the model can be done using the evaluate function as shown below.

Making Predictions

As you guessed, predictions can be made using the predict method. All you have to do is pass in the testing set.

Saving and Restoring the Model

In order to save a model, one has to first install these two dependencies.

Saving the entire model HDF5 file is also very easy, as we’ll see below.