Keras: Make Predictions with Long Short-Term Memory Model

From OnnoWiki
Jump to: navigation, search


Tujuan mengembangkan model LSTM adalah agar final model yang di peroleh dapat digunakan pada sequence prediction problem.

Dalam tulisan ini, anda akan menemukan cara mem-finalisasi model anda dan menggunakannya untuk membuat prediksi pada data baru.

Setelah membaca tulisan ini, anda diharapkan tahu:

  • Bagaimana cara train final LSTM model.
  • Bagaimana cara save final LSTM model, dan kemudian di load kembali.
  • Bagaimana melakukan prediksi pada data baru.

Step 1. Train Final Model

Apakah Final Model LSTM?

Final Model LSTM adalah yang kita gunakan untuk membuat prediksi pada data baru.

Artinya, dengan diberikan contoh input data baru, kita ingin menggunakan model untuk memprediksi output yang diharapkan. Ini mungkin klasifikasi (menetapkan label) atau regresi (nilai riil).

Tujuan dari proyek prediksi urutan adalah untuk mencapai model akhir yang melakukan yang terbaik, di mana "terbaik" didefinisikan oleh:

Data: data historis yang anda miliki.
Time: waktu yang anda habiskan untuk proyek.
Procedure: langkah-langkah persiapan data, algoritma, dan konfigurasi algoritma yang dipilih.

Dalam proyek anda, anda mengumpulkan data, menghabiskan waktu yang anda miliki, dan menemukan prosedur persiapan data, algoritma untuk digunakan, dan cara mengkonfigurasinya.

Final Model adalah puncak dari proses ini, akhir yang anda cari untuk mulai benar-benar membuat prediksi.

Tidak ada model yang sempurna. Hanya ada model terbaik yang dapat anda temukan.

Cara Finalisasi 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:

   How to Train a Final Machine Learning Model

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:

sudo pip install h5py

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

For example:

# define model
model = Sequential()
# compile model
# fit model
# save model to single file'lstm_model.h5')

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.

from keras.models import load_model
# load model from single file
model = load_model('lstm_model.h5')
# make predictions
yhat = model.predict(X, verbose=0)

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.

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from numpy import array
from keras.models import load_model

# return training data
def get_train():
seq = [[0.0, 0.1], [0.1, 0.2], [0.2, 0.3], [0.3, 0.4], [0.4, 0.5]]
seq = array(seq)
X, y = seq[:, 0], seq[:, 1]
X = X.reshape((len(X), 1, 1))
return X, y

# define model
model = Sequential()
model.add(LSTM(10, input_shape=(1,1)))
model.add(Dense(1, activation='linear'))
# compile model
model.compile(loss='mse', optimizer='adam')
# fit model
X,y = get_train(), y, epochs=300, shuffle=False, verbose=0)
# save model to single file'lstm_model.h5')

# snip...
# later, perhaps run from another script

# load model from single file
model = load_model('lstm_model.h5')
# make predictions
yhat = model.predict(X, verbose=0)

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

   Save and Load Your Keras Deep Learning Models

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:

X = ...
model = ...
yhat = model.predict(X)

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

X = ...
model = ...
yhat = model.predict_classes(X)

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

X = ...
model = ...
yhat = model.predict_proba(X)

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

   The 5 Step Life-Cycle for Long Short-Term Memory Models in Keras


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.


Pranala Menarik