...

/

DL Model Training, Testing, and Evaluation

DL Model Training, Testing, and Evaluation

Learn to train models using Keras and perform model testing and evaluation on the trained model.

Keras API allows us to build DL models using the Sequential model class, functional interface, and model subclassing. Let’s design a Sequential Keras model, perform model training, and test/evaluate the trained model.

Model creation

The Sequential model class can create a DL model with layers stacked one after the other. For instance, the following code imports the Sequential model and builds a simple CNN architecture:

Press + to interact
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, Flatten, Dense
my_model = Sequential([
Input(shape=(28,28,1)),
Conv2D(filters=10, kernel_size=(7,7), padding="same", activation="relu"),
MaxPool2D(pool_size=(2,2)),
Conv2D(filters=16, kernel_size=(7,7), padding="same", activation="relu"),
MaxPool2D(pool_size=(2, 2)),
Flatten(),
Dense(units=100, activation="relu"),
Dense(units=10, activation="softmax")
])
print (my_model.summary())
  • Line 5: We use the Sequential model constructor to pass an array of the network layers.

  • Line 16: We observe the overall model architecture by printing my_model.summary().

The compile() method

Once we define our model, we use the my_model.compile() method to specify training configurations, such as the loss function to optimize, the optimizer algorithm, and the metric to monitor during the training process. Some of the built-in configurations are mentioned below. ...