2

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.

Code231
  • 21
  • 1

1 Answers1

0

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.

Sagar Dubey
  • 121
  • 4
  • I added the following after saving model: with open('tokenizer.pickle','wb') as handle: pickle.dump(tokenizer,handle,protocol=pickle.HIGHEST_PROTOCOL) And in the file where I am loading the model, I added the following: texts=[ "hey"] with open('tokenizer.pickle','rb') as handle: tk=pickle.load(handle) tk.fit_on_texts(texts) index_list = tk.texts_to_sequences(texts) data = pad_sequences(index_list, maxlen=1) y =loaded_model.predict(data) I get the error: ValueError: Error when checking input: expected input_2 to have 3 dimensions, but got array with shape (1, 1) – Code231 Oct 13 '19 at 05:09
  • hi @Code231 don't fit the text again. You don't have to write this line tk.fit_on_texts(texts) and make sure the maxlen should be same while training and in this part. you've used 1 as maxlen so make sure while traininig you used the same maxlen. `with open('tokenizer.pickle','rb') as handle: tk=pickle.load(handle) texts=[ "hey"] index_list = tk.texts_to_sequences(texts) data = pad_sequences(index_list, maxlen=max_len_you_used_while_training) y =loaded_model.predict(data) ` – Sagar Dubey Oct 14 '19 at 09:46