Saving the Model
Up until this point we have simply been retraining our models every time that we wanted to use them. This is fine for now on our small models that take only a few seconds to train but for larger models this is not realistic. Luckily for us keras provides a very easy way to save our models:
Loading the Model
Now that we have saved a trained model we never need to retrain it! We can simply load a saved model in by using the following. Simply ensure that the .h5 file is in the same directory as your python script.
Now it is time to used our saved model to make predictions. Now this is a little bit harder than it looks because we need to consider the following:
– our model accepts integer encoded data
– our model needs reviews that are of length 250 words
This means we can’t just pass any string of text into our model. It will need to be reshaped and reformed to meet the criteria above.
Transforming our Data
The data I’ll use for this tutorial will be simple raw text data of a movie review of one of my favorite movies, the lion king. I’m storing this data in a text file called “test.txt” that you can download here.
To start we will need to integer encode the data. We will do this using the following function:
Next we will open our text file, read in each of the reviews (in this case just one) and use the model to predict whether it is positive or negative.