I am going through the examples in keras and I ran the example for using an LSTM for classifying sentiment on the inbuilt imdb dataset (https://github.com/fchollet/keras/blob/master/examples/imdb_lstm.py).
On inspecting the data, each review is depicted as an array of numbers which I assume is their index from a vocabulary built using this dataset.
My question however is how do I input a new piece of text(something that I make up) into this model to get a prediction? How would I get access to this vocabulary of words?
After that I could preprocess by input text into an array of numbers and feed it in. Thanks!
To give inputs to a machine learning model, you have to create a NumPy array, where you have to input the values of the features you used to train your machine learning model. Then we can use that array in the model. predict() method, and at the end, it will give the predicted value as an output based on the inputs.
When Predicting new text, you have to follow the same step you have done for training.
sentences = clean_text(text)
word_index = imdb.get_word_index()
x_test = [[self.word_index[w] for w in sentences if w in self.word_index]]
x_test = pad_sequences(x_test, maxlen=maxlen) # Should be same which you used for training data
vector = np.array([x_test.flatten()])
model.predict_classes(vector)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With