How to Make Predictions with Keras

Once you choose and fit a final deep learning model in Keras, you can use it to make predictions on new data instances.

There is some confusion amongst beginners about how exactly to do this. I often see questions such as:

How do I make predictions with my model in Keras?

In this tutorial, you will discover exactly how you can make classification and regression predictions with a finalized deep learning model with the Keras Python library.

After completing this tutorial, you will know:

  • How to finalize a model in order to make it ready for making predictions.
  • How to make class and probability predictions for classification problems in Keras.
  • How to make regression predictions in in Keras.

Let’s get started.

How to Make Classification and Regression Predictions for Deep Learning Models in Keras

How to Make Classification and Regression Predictions for Deep Learning Models in Keras
Photo by mstk east, some rights reserved.

Tutorial Overview

This tutorial is divided into 3 parts; they are:

  1. Finalize Model
  2. Classification Predictions
  3. Regression Predictions

1. Finalize Model

Before you can make predictions, you must train a final model.

You may have trained models using k-fold cross validation or train/test splits of your data. This was done in order to give you an estimate of the skill of the model on out of sample data, e.g. new data.

These models have served their purpose and can now be discarded.

You now must train a final model on all of your available data. You can learn more about how to train a final model here:

2. Classification Predictions

Classification problems are those where the model learns a mapping between input features and an output feature that is a label, such as “spam” and “not spam“.

Below is an example of a finalized neural network model in Keras developed for a simple two-class (binary) classification problem.

If developing a neural network model in Keras is new to you, see the post:

After finalizing, you may want to save the model to file, e.g. via the Keras API. Once saved, you can load the model any time and use it to make predictions. For an example of this, see the post:

For simplicity, we will skip this step for the examples in this tutorial.

There are two types of classification predictions we may wish to make with our finalized model; they are class predictions and probability predictions.

Class Predictions

A class prediction is given the finalized model and one or more data instances, predict the class for the data instances.

We do not know the outcome classes for the new data. That is why we need the model in the first place.

We can predict the class for new data instances using our finalized classification model in Keras using the predict_classes() function. Note that this function is only available on Sequential models, not those models developed using the functional API.

For example, we have one or more data instances in an array called Xnew. This can be passed to the predict_classes() function on our model in order to predict the class values for each instance in the array.

Let’s make this concrete with an example:

Running the example predicts the class for the three new data instances, then prints the data and the predictions together.

If you had just one new data instance, you could provide this as an instance wrapped in an array to the predict_classes() function; for example:

Running the example prints the single instance and the predicted class.

A Note on Class Labels

Note that when you prepared your data, you will have mapped the class values from your domain (such as strings) to integer values. You may have used a LabelEncoder.

This LabelEncoder can be used to convert the integers back into string values via the inverse_transform() function.

For this reason, you may want to save (pickle) the LabelEncoder used to encode your y values when fitting your final model.

Probability Predictions

Another type of prediction you may wish to make is the probability of the data instance belonging to each class.

This is called a probability prediction where, given a new instance, the model returns the probability for each outcome class as a value between 0 and 1.

You can make these types of predictions in Keras by calling the predict_proba() function; for example:

In the case of a two-class (binary) classification problem, the sigmoid activation function is often used in the output layer. The predicted probability is taken as the likelihood of the observation belonging to class 1, or inverted (1 – probability) to give the probability for class 0.

In the case of a multi-class classification problem, the softmax activation function is often used on the output layer and the likelihood of the observation for each class is returned as a vector.

The example below makes a probability prediction for each example in the Xnew array of data instance.

Running the instance makes the probability predictions and then prints the input data instance and the probability of each instance belonging to class 1.

This can be helpful in your application if you want to present the probabilities to the user for expert interpretation.

3. Regression Predictions

Regression is a supervised learning problem where given input examples, the model learns a mapping to suitable output quantities, such as “0.1” and “0.2”, etc.

Below is an example of a finalized Keras model for regression.

We can predict quantities with the finalized regression model by calling the predict() function on the finalized model.

The predict() function takes an array of one or more data instances.

The example below demonstrates how to make regression predictions on multiple data instances with an unknown expected outcome.

Running the example makes multiple predictions, then prints the inputs and predictions side by side for review.

The same function can be used to make a prediction for a single data instance, as long as it is suitably wrapped in a surrounding list or array.

For example:

Running the example makes a single prediction and prints the data instance and prediction for review.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.


In this tutorial, you discovered how you can make classification and regression predictions with a finalized deep learning model with the Keras Python library.

Specifically, you learned:

  • How to finalize a model in order to make it ready for making predictions.
  • How to make class and probability predictions for classification problems in Keras.
  • How to make regression predictions in in Keras.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Frustrated With Your Progress In Deep Learning?

Deep Learning with Python

 What If You Could Develop A Network in Minutes

…with just a few lines of Python

Discover how in my new Ebook: Deep Learning With Python

It covers self-study tutorials and end-to-end projects on topics like:
Multilayer PerceptronsConvolutional Nets and Recurrent Neural Nets, and more…

Finally Bring Deep Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

8 Responses to How to Make Predictions with Keras

  1. Nitin April 9, 2018 at 10:14 am #

    Great article Jason. Do you recommend any articles for hyperparameter tuning to further improve accuracies? Also any articles for common problems and solutions during model tuning?

  2. bekky April 10, 2018 at 7:32 pm #

    Thanks for the tutorial! If I want to build a CNN which has both classification and regression heads, I suppose I cannot use a sequential model. Do you know an example for such a multi-head CNN? Thank you

  3. Moustafa April 16, 2018 at 6:23 am #

    Thanks for your the explication,
    Could you please put photos of network architectures aside the code ?
    I think it will help us to understand best the architecture

  4. Janne April 17, 2018 at 11:38 pm #

    Hi Jason and thanks for this post. I have a quick question about regression with bounded target values.
    If my target values are always restricted between [0,1] with most of the values close to 0.5 (i.e., values are rarely close to 0 or 1), is it useful to use sigmoid output activation instead of linear? Would it help in convergence or stability when training a complex model? It seems like a waste not to take any advantage of the fact that target values belong into bounded interval.

    So in your code, one would simply make a replacement

    model.add(Dense(1, activation=’linear’)) –> model.add(Dense(1, activation=’sigmoid’))

    • Jason Brownlee April 18, 2018 at 8:08 am #

      Good question.

      Yes, interesting idea. It might change the loss function used to fit the model, which may result in optimizing the wrong problem (e.g. logloss instead of mse). Nevertheless, try it and compare error results between the two approaches.

      Yes, that is the code change.

Leave a Reply