How to use loaded LSTM attention model to make predictions on input?

Viewed 119

I am a complete beginner in Deep Learning & Keras. I want to build a hierarchical attention network that helps to classify comments into several categories viz. toxic, severely toxic, etc. I took the code from an open repository and saved the model. I then loaded the model using model_from_json. Now I wish to use this loaded model to make predictions on the input text(given as a python input or as a separate file).

This is the code that I am using: https://www.kaggle.com/sermakarevich/hierarchical-attention-network/notebook

Then I did:

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
model.save_weights("model.h5")
print("Saved model to disk")

Then in a separate file:

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json,custom_objects={'AttentionWithContext':AttentionWithContext})
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

I am getting "loaded model from disk" perfectly. I wish to know the format in which I need to give input and how and the code snippet to use the model to classify it. Since I do not have much knowledge about it, It would be really helpful if someone could help me with the python specific code to make it work.

1 Answers

While doing prediction please make sure that you pickle tokenizer as well otherwise the output won't be correct.

    new = ["Your_text_that_you_want_to_check"]
    seq = tokenizer.texts_to_sequences(new)
    padded = pad_sequences(seq, maxlen=MAX_SEQUENCE_LENGTH)
    pred = model.predict(padded)

While predicting it is very important to convert your new text to the vector such that your model is trained. I've converted my training data to sequence and then pad it with zero so that length should be same and the same steps I repeated while predicting. But make sure you pickle your tokenzier. I hope it helps! Let me know if you're having difficulty understanding the steps.

Related