LSTM Networks for Sentiment Analysis - How to extend this model to 3 classes and classify new examples?

276 Views Asked by At

I'm trying the code from this link http://deeplearning.net/tutorial/lstm.html and have changed the imdb data to my own data. I'm trying to extend this model to a 3-class multinomial model with a "neutral" class added instead of the binomial model provided in the tutorial that classifies only "positive" and "negative" classes.

To build this new model, I rewrote the build_dict and grab_data functions to build my custom dictionary and return data for the three classes and made the following changes to main() in imdb_preprocess.py:

dictionary = build_dict()

train_x_pos, train_x_neu, train_x_neg = grab_data('train', dictionary)
test_x_pos, test_x_neu, test_x_neg = grab_data('test', dictionary)

train_x = train_x_pos + train_x_neu + train_x_neg
train_y = [1] * len(train_x_pos) + [0] * len(train_x_neu) + [-1] * len(train_x_neg)

test_x = test_x_pos + test_x_neu + test_x_neg
test_y = [1] * len(test_x_pos) + [0] * len(test_x_neu) + [-1] * len(test_x_neg)

Are my modifications to train_y and test_y correct?

Assuming they are correct, I went ahead and trained this model on my data, and I was successfully able to save new versions of lstm_model.npz and lstm_model.npz.pkl.

Now, how do I test this model on a new example? Suppose a new, unseen example is the following text:

new_example = "I am very happy that I've been able to create a new LSTM sentiment model!"

My understanding is that the function pred_probs needs to be called, which takes parameters f_pred_prob, prepare_data, data, iterator and verbose=False. What do I pass in for these parameters? I'm guessing the function call would then be something like:

pred_probs(f_pred_prob, prepare_data, data=new_example, iterator, verbose=False)
1

There are 1 best solutions below

0
On BEST ANSWER

After turning this over for weeks I finally got a working model. It turns out my modifications to main() in imdb_preprocess.py were almost correct, the relevant lines are:

train_y = [2] * len(train_x_pos) + [1] * len(train_x_neu) + [0] * len(train_x_neg)
test_y = [2] * len(test_x_pos) + [1] * len(test_x_neu) + [0] * len(test_x_neg)

As for how to classify a new example, I had the right idea:

new_example_pair = (new_example, 0)
pred_probs(f_pred_prob, prepare_data, new_example_pair, iterator, verbose=False)

pred_probs should be modified to accommodate three classes, and changing it properly should return a probability value for each possible class.