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.

## Tutorial Overview

This tutorial is divided into 3 parts; they are:

- Finalize Model
- Classification Predictions
- 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# example of training a final classification model from keras.models import Sequential from keras.layers import Dense from sklearn.datasets.samples_generator import make_blobs from sklearn.preprocessing import MinMaxScaler # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) scalar = MinMaxScaler() scalar.fit(X) X = scalar.transform(X) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') model.fit(X, y, epochs=200, verbose=0) |

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.

1 2 |
Xnew = [[...], [...]] ynew = model.predict_classes(Xnew) |

Let’s make this concrete with an example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# example making new class predictions for a classification problem from keras.models import Sequential from keras.layers import Dense from sklearn.datasets.samples_generator import make_blobs from sklearn.preprocessing import MinMaxScaler # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) scalar = MinMaxScaler() scalar.fit(X) X = scalar.transform(X) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') model.fit(X, y, epochs=500, verbose=0) # new instances where we do not know the answer Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1) Xnew = scalar.transform(Xnew) # make a prediction ynew = model.predict_classes(Xnew) # show the inputs and predicted outputs for i in range(len(Xnew)): print("X=%s, Predicted=%s" % (Xnew[i], ynew[i])) |

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

1 2 3 |
X=[0.89337759 0.65864154], Predicted=[0] X=[0.29097707 0.12978982], Predicted=[1] X=[0.78082614 0.75391697], Predicted=[0] |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# example making new class prediction for a classification problem from keras.models import Sequential from keras.layers import Dense from sklearn.datasets.samples_generator import make_blobs from sklearn.preprocessing import MinMaxScaler from numpy import array # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) scalar = MinMaxScaler() scalar.fit(X) X = scalar.transform(X) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') model.fit(X, y, epochs=500, verbose=0) # new instance where we do not know the answer Xnew = array([[0.89337759, 0.65864154]]) # make a prediction ynew = model.predict_classes(Xnew) # show the inputs and predicted outputs print("X=%s, Predicted=%s" % (Xnew[0], ynew[0])) |

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

1 |
X=[0.89337759 0.65864154], Predicted=[0] |

### 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:

1 2 |
Xnew = [[...], [...]] ynew = model.predict_proba(Xnew) |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# example making new probability predictions for a classification problem from keras.models import Sequential from keras.layers import Dense from sklearn.datasets.samples_generator import make_blobs from sklearn.preprocessing import MinMaxScaler # generate 2d classification dataset X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1) scalar = MinMaxScaler() scalar.fit(X) X = scalar.transform(X) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') model.fit(X, y, epochs=500, verbose=0) # new instances where we do not know the answer Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1) Xnew = scalar.transform(Xnew) # make a prediction ynew = model.predict_proba(Xnew) # show the inputs and predicted outputs for i in range(len(Xnew)): print("X=%s, Predicted=%s" % (Xnew[i], ynew[i])) |

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

1 2 3 |
X=[0.89337759 0.65864154], Predicted=[0.0087348] X=[0.29097707 0.12978982], Predicted=[0.82020265] X=[0.78082614 0.75391697], Predicted=[0.00693122] |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# example of training a final regression model from keras.models import Sequential from keras.layers import Dense from sklearn.datasets import make_regression from sklearn.preprocessing import MinMaxScaler # generate regression dataset X, y = make_regression(n_samples=100, n_features=2, noise=0.1, random_state=1) scalarX, scalarY = MinMaxScaler(), MinMaxScaler() scalarX.fit(X) scalarY.fit(y.reshape(100,1)) X = scalarX.transform(X) y = scalarY.transform(y.reshape(100,1)) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='linear')) model.compile(loss='mse', optimizer='adam') model.fit(X, y, epochs=1000, verbose=0) |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# example of making predictions for a regression problem from keras.models import Sequential from keras.layers import Dense from sklearn.datasets import make_regression from sklearn.preprocessing import MinMaxScaler # generate regression dataset X, y = make_regression(n_samples=100, n_features=2, noise=0.1, random_state=1) scalarX, scalarY = MinMaxScaler(), MinMaxScaler() scalarX.fit(X) scalarY.fit(y.reshape(100,1)) X = scalarX.transform(X) y = scalarY.transform(y.reshape(100,1)) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='linear')) model.compile(loss='mse', optimizer='adam') model.fit(X, y, epochs=1000, verbose=0) # new instances where we do not know the answer Xnew, a = make_regression(n_samples=3, n_features=2, noise=0.1, random_state=1) Xnew = scalarX.transform(Xnew) # make a prediction ynew = model.predict(Xnew) # show the inputs and predicted outputs for i in range(len(Xnew)): print("X=%s, Predicted=%s" % (Xnew[i], ynew[i])) |

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

1 2 3 |
X=[0.29466096 0.30317302], Predicted=[0.17097184] X=[0.39445118 0.79390858], Predicted=[0.7475489] X=[0.02884127 0.6208843 ], Predicted=[0.43370453] |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# example of making predictions for a regression problem from keras.models import Sequential from keras.layers import Dense from sklearn.datasets import make_regression from sklearn.preprocessing import MinMaxScaler from numpy import array # generate regression dataset X, y = make_regression(n_samples=100, n_features=2, noise=0.1, random_state=1) scalarX, scalarY = MinMaxScaler(), MinMaxScaler() scalarX.fit(X) scalarY.fit(y.reshape(100,1)) X = scalarX.transform(X) y = scalarY.transform(y.reshape(100,1)) # define and fit the final model model = Sequential() model.add(Dense(4, input_dim=2, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='linear')) model.compile(loss='mse', optimizer='adam') model.fit(X, y, epochs=1000, verbose=0) # new instance where we do not know the answer Xnew = array([[0.29466096, 0.30317302]]) # make a prediction ynew = model.predict(Xnew) # show the inputs and predicted outputs print("X=%s, Predicted=%s" % (Xnew[0], ynew[0])) |

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

1 |
X=[0.29466096 0.30317302], Predicted=[0.17333156] |

## Further Reading

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

- How to Train a Final Machine Learning Model
- Save and Load Your Keras Deep Learning Models
- Develop Your First Neural Network in Python With Keras Step-By-Step
- The 5 Step Life-Cycle for Long Short-Term Memory Models in Keras
- How to Make Predictions with Long Short-Term Memory Models in Keras

## Summary

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.

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?

Yes, see this post:

http://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/

Thanks for tutorial.

I have a problem with my data because there aren’t of the same dimensions, so the software don’t work. If I fill in the blanks with random numbers to have the dimensions, it’s work but I haven’t the good answers. Can you explain me how I can do to solve this problem.

Thanks

Perhaps this post will help:

https://machinelearningmastery.com/handle-missing-timesteps-sequence-prediction-problems-python/

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

Here are some examples of multiple output models:

https://machinelearningmastery.com/keras-functional-api-deep-learning/

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

Thanks for the suggestion.

hello Jason,

thanks for your articles. you really help us a lot.

may i ask one question ?

Can Keras be used to build clustering models?

keras.wrappers.scikit_learn can be used to build KerasClassifier model, Keras be used to build clustering models? If it can be, are there any examples for that?

you know i want to use some features like age, city, education, company, job title and so on to cluster people into some groups and to get the key features of each group.

Perhaps, I have not seen this.

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’))

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.

What are the considerations in using scikit-learn over Keras for classification problems (or vis versa) in R or Python?

scikit-learn offers a suite of general ML algorithms, Keras is focused on neural network algorithms only.

Regarding Python vs R, I answer that here:

https://machinelearningmastery.com/faq/single-faq/what-programming-language-should-i-use-for-machine-learning

Does that help?

I am trying to predict a new image on a model that I trained with emnist letters. Here is the code snippet that tries to do so.

import matplotlib

# Force matplotlib to not use any Xwindows backend.

matplotlib.use(‘Agg’)

import keras

import matplotlib.pyplot as plt

from keras.models import load_model

from keras.preprocessing import image

from keras import backend as K

from scipy.misc import imread

from PIL import Image

import skimage.io as io

import skimage.transform as tr

import numpy as np

from keras.utils import plot_model

from keras.datasets import mnist

# Returns a compiled model identical to the previous one

model = load_model(‘matLabbed.h5’)

print(“Testing the model on our own input data”)

imgA = imread(‘A.png’)

imgA = tr.resize(imgA, (28, 28, 1)).astype(‘float32’)

imgA = imgA[np.newaxis, …]

y_classes = model.predict_classes(imgA)

When I try to print y_classes, it gives me numerical outputs e.g 4, 10. I am trying to figure out the labels that my data uses and compare that with the y_classes output. Any suggestion? Thanks.

The integer will relate to a class in your training data.

how to predict classes with model developed using functional API ?

You can call model.predict() then use argmax() on the resulting vector to get the class index.

Hi Jason, this code editor is really irritating, please consider substituting it with another or just with some simple text.

Here is more information on how to copy code from the snippets:

https://machinelearningmastery.com/faq/single-faq/how-do-i-copy-code-from-a-tutorial

Does that help?

The networks for classification and regression differ only a little (activation function of the output neuron and the the loss function) yet in the case of classification it is so easy to estimate the probability of the prediction (via predict_proba) while in the case of regression the analog is the prediction interval which is difficult to calculate for non-linear models like neural networks.

Why is there such a difference? Shouldn’t the probability estimation for regression be done as easily as for classification? A straightforward (but maybe naive) solution that I see is to bin the target variable, make a classification, then use predict_proba to get the probability of the predicted value to be in a certain interval, and then to construct prediction interval from that.

Can it be done this way? Or changing the loss function will make two problems (regression and classification for the binned target) so much different that the result of one problem cannot be transferred to another?

The binning approach would be my first/naive thought too. Try it and see.

OK, I tried it for a toy 1-dim regression task.

A few days ago, I asked this question on stackexchange. Didn’t get many replies, so I now answered my own question by implementing in Keras the idea that I outlined in my comment above.

It looks like it works. I am still not sure if I do all this correctly, and if it will work for real multi-dimensional problems. If you like, you can see it here: https://datascience.stackexchange.com/questions/31773/how-to-estimate-the-variance-of-regressors-in-scikit-learn/32486#32486

The approach makes me nervous.

Hi Jason,

I built a model to classify a financial time series for stock trading having 10 inputs and one output (‘0’ for stock going down, ‘1’ going up). Now I have 1400 such inputs for training the model and managed to get an accuracy of 97%! …

but when presenting new inputs to the model I don’t get a classification accuracy more than 30%! ..in my opinion the problem would be:

1. Overfitting

2, time series not predictable?

etc…

do you have any idea what the problem might be or how to improve the accuracy for new data presented to the model?

Thanks

Low skill might mean underfitting, the wrong model, an intractable problem, etc.

how to get the RMSE for the regression model?

You can calculate the MSE and then calculate the square root of the MSE.

sklearn has an implementation here:

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html

Does that help?

very helpful. thanks

I’m glad it helped.

Hi, I am working on a multilabel problem. My X_train has this shape (for instance) [[0 1 0 0 1]…]. I want to predict the next, like [1 0 0 1 0]. How can I do that? Each label is independent from the others. How train that? Where can I find some information to learn about it?

Thanks!:)

Sorry, I don’t have examples of multilabel forecasting, only multiclass forecasting.

Very nice! could you plz also make a similar tutorial with PyToorch.

Thanks for the suggestion.

Thanks for your post, Jason.

When we are predicting, we are required to revert back to the original scaling. as both the input and output seem to be scaled for the model to predict correctly.

Correct, we should return yhat to the original scale before evaluating error in the case of regression.

Hi, thanks for a great post.

I have question related to regression problem.

I am trying to develop a model that predcits 3 outputs from 36 input variables. To be clear, I have 3 output parameters and 36 input parameters. Which means that in the output layer I have 3 neurons. The model can sucessively predict whe there is only one output, but when it comes to 3 outputs it gives somethig useless. What do you suggest? By the way I use keras mlp regressor.

Perhaps try tuning the model to see if you can find a configuration that best suits your specific problem, here are some ideas:

http://machinelearningmastery.com/improve-deep-learning-performance/

Hi Jason, Thanks for the explanation of classification and regression architecture.

I am trying to use the regression model on 300 dimensions vectors. So the problem statement is to Know which two vectors are similar or non-similar. So for this, I have taken the difference between two similar vectors and non-similar vectors. We labeled similar diff vectors as 0 and non-similar diff vectors as 1. And feeding it to the network of which looks like this:

model = Sequential()

model.add(Dense(300, input_dim=301, activation=’relu’))

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

model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘acc’])

Training_Loss: 0.0171

Training_Acc: 0.9988

Testing_Loss = 0.6456110666

Testing_Acc = 0.821831868464

I am not sure, why the model is overfitting, Can you please give some insight into what could be wrong?

Perhaps try some different model configurations and see if they over fit? E.g. less nodes.