Compile, Evaluate and Predict Model in Keras

Free Keras 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.

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.

  • mean_squared_error
  • mean_absolute _error
  • hinge
  • mean_absolute_percentage _error
  • mean_squared_logarithmic_error
  • Poisson
  • binary_crossentropy
  • categorical_crossentropy
  • and there are also some more.

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

Below are the various available optimizers:

  • SGD ( Stochastic Gradient Descent )
  • RMSprop
  • Adagrad
  • Adam
  • Adamax
  • Nadam

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.

  • accuracy
  • binary_accuracy
  • categorical_accuracy
  • cosine_proximity
  • clone_metric

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.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

3 Responses

  1. amrutha says:

    Hi! This helped me a lot in my thesis. i understood clearly even though i was a total newbie to this. thank you so much for this! great work

  2. Steven Dascoli says:

    We should be clear that the “loss” figure is the sum of **ALL** the losses calculated for each item in the x_test array. x_test would contain your test data and y_test would contain your labels. The loss figure is the sum of ALL the losses, not just one loss from one item in the x_test array.

  3. Amit Khan says:

    Please specify why evaluate score and prediction score are different??

Leave a Reply

Your email address will not be published. Required fields are marked *