Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Saturday 26 October 2024

How to Make Predictions with Long Short-Term Memory Models in Keras

 The goal of developing an LSTM model is a final model that you can use on your sequence prediction problem.

In this post, you will discover how to finalize your model and use it to make predictions on new data.

After completing this post, you will know:

  • How to train a final LSTM model.
  • How to save your final LSTM model, and later load it again.
  • How to make predictions on new data.

    Step 1. Train a Final Model

    What Is a Final LSTM Model?

    A final LSTM model is one that you use to make predictions on new data.

    That is, given new examples of input data, you want to use the model to predict the expected output. This may be a classification (assign a label) or a regression (a real value).

    The goal of your sequence prediction project is to arrive at a final model that performs the best, where “best” is defined by:

    • Data: the historical data that you have available.
    • Time: the time you have to spend on the project.
    • Procedure: the data preparation steps, algorithm or algorithms, and the chosen algorithm configurations.

    In your project, you gather the data, spend the time you have, and discover the data preparation procedures, algorithm to use, and how to configure it.

    The final model is the pinnacle of this process, the end you seek in order to start actually making predictions.

    There is no such thing as a perfect model. There is only the best model that you were able to discover.

    How to Finalize an LSTM Model?

    You finalize a model by applying the chosen LSTM architecture and configuration on all of your data.

    There is no train and test split and no cross-validation folds. Put all of the data back together into one large training dataset and fit your model.

    That’s it.

    With the finalized model, you can:

    • Save the model for later or operational use.
    • Load the model and make predictions on new data.

    For more on training a final model, see the post:

    Need help with LSTMs for Sequence Prediction?

    Take my free 7-day email course and discover 6 different LSTM architectures (with code).

    Click to sign-up and also get a free PDF Ebook version of the course.

    Step 2. Save Your Final Model

    Keras provides an API to allow you to save your model to file.

    The model is saved in HDF5 file format that efficiently stores large arrays of numbers on disk. You will need to confirm that you have the h5py Python library installed. It can be installed as follows:

    You can save a fit Keras model to file using the save() function on the model.

    For example:

    This single file will contain the model architecture and weights. It also includes the specification of the chosen loss and optimization algorithm so that you can resume training.

    The model can be loaded again (from a different script in a different Python session) using the load_model() function.

    Below is a complete example of fitting an LSTM model, saving it to a single file and later loading it again. Although the loading of the model is in the same script, this section may be run from another script in another Python session. Running the example saves the model to the file lstm_model.h5.

    For more on saving and loading your Keras model, see the post:

    Step 3. Make Predictions on New Data

    After you have finalized your model and saved it to file, you can load it and use it to make predictions.

    For example:

    • On a sequence regression problem, this may be the prediction of the real value at the next time step.
    • On a sequence classification problem, this may be a class outcome for a given input sequence.

    Or it may be any other variation based on the specifics of your sequence prediction problem. You would like an outcome from your model (yhat) given an input sequence (X) where the true outcome for the sequence (y) is currently unknown.

    You may be interested in making predictions in a production environment, as the backend to an interface, or manually. It really depends on the goals of your project.

    Any data preparation performed on your training data prior to fitting your final model must also be applied to any new data prior to making predictions.

    Predicting is the easy part.

    It involves taking the prepared input data (X) and calling one of the Keras prediction methods on the loaded model.

    Remember that the input for making a prediction (X) is only comprised of the input sequence data required to make a prediction, not all prior training data. In the case of predicting the next value in one sequence, the input sequence would be 1 sample with the fixed number of time steps and features used when you defined and fit your model.

    For example, a raw prediction in the shape and scale of the activation function of the output layer can be made by calling the predict() function on the model:

    The prediction of a class index can be made by calling the predict_classes() function on the model.

    The prediction of probabilities can be made by calling the predict_proba() function on the model.

    For more on the life-cycle of your Keras model, see the post:

    Further Reading

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

    Posts

    API

    Summary

    In this post, you discovered how to finalize your model and use it to make predictions on new data.

    Specifically, you learned:

    • How to train a final LSTM model.
    • How to save your final LSTM model, and later load it again.
    • How to make predictions on new data.

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

No comments:

Post a Comment

Connect broadband

Mini-Course on Long Short-Term Memory Recurrent Neural Networks with Keras

  Long Short-Term Memory (LSTM) recurrent neural networks are one of the most interesting types of deep learning at the moment. They have be...