Site icon DataFlair

Compile, Evaluate and Predict Model in Keras

Free Flink course with real-time projects Start Now!!

Welcome to DataFlair Keras Tutorial series. This chapter explains how to compile, evaluate and make predictions from Model in Keras.

Keras Compile Models

After defining our model and stacking the layers, we have to configure our model. We do this configuration process in the compilation phase.
Before training the model we need to compile it and define the loss function, optimizers, and metrics for prediction.

We compile the model using .compile() method.

model.compile ( optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors)

Optimizer, loss, and metrics are the necessary arguments.

Keras provides various loss functions, optimizers, and metrics for the compilation phase.

Loss Function in Keras

These are available in the losses module and is one of the two arguments required for compiling a Keras model.

from keras import losses

Below are the various available loss functions.

Keras Model Optimization

These are very important since we use optimizers to adjust input weights. We optimize input weights by comparing prediction and the loss function. These are available in the optimizer module.

from keras import optimizers

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Below are the various available optimizers:

We need to specify the learning rate for the following optimizers.

keras.optimizers.Adam(learning_rate=0.001)

Keras Metrics

This specifies the evaluation criteria for the model. These are present in the Keras metrics module. We import it as below:

from keras import metrics

Below are the various available metrics in Keras.

Keras Model Evaluation

In this phase, we model, whether it is the best to fit for the unseen data or not. For this, Keras provides .evaluate() method.

model.evaluate(X_test,Y_test, verbose)

As you can observe, it takes three arguments, Test data, Train data and verbose {true or false}

.evaluate() method returns a score which is used to measure the performance of our model.

Keras Model Prediction

When we get satisfying results from the evaluation phase, then we are ready to make predictions from our model. This is the final phase of the model generation. For this Keras provides .predict() method.

model.predict( X_test, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)

Where X_test is the necessary parameter.

Summary

This article explains the compilation, evaluation and prediction phase of model in Keras. After adding all the layers to our model, we need to define the loss function, optimizers and metrics to train our model. We define these in the compilation phase. After compilation we evaluate our model on unseen data to test the performance. Finally we take output from the model. This article also explains about the various arguments and their uses in these three phases.

Do share your feedback in the comment section if you liked the article.

Exit mobile version