NLP - Individual Prediction (lesson 4)

How to get an individual prediction from our NLP model? I’ve tried this, but it didn’t seem to work:

m3.predict_array(np.array(TEXT.numericalize("I really love this movie")))

Check out this discussion and see if you can make KevinB’s code work for IMDB. It is the method for word prediction in the language model, applied to the other model

Thanks @rob!

It may be working, not sure. Something weird is happening, when I try to run it against the incorrect classified texts, it predicts the right label, not the incorrect one.

val_preds, y = newModel.predict_with_targs()
res = np.argmax(val_preds, axis=1)

val_ds = pd.DataFrame(splits[1].examples)
incorrect = val_ds[:len(res)][y != res]

# Show some incorrect classified texts
for t in incorrect[:10][0]:
    print(t.label + ': ' + ' ' . join(t.text[:100]) + "\n")

def predict_text(text):
    res, *_ = newModel.model(TEXT.numericalize([spacy_tok(text)]))
    prediction = to_np(torch.topk(res[-1], 1)[1])[0]
    return AMAZON_LABEL.vocab.itos[prediction]

# it's returning the right label, not the incorrect, as I was expecting
predict_text(' '.join(incorrect[0][4].text))
predict_text("this book surprised me with the storyline .   it was different , fresh , and just plain good .   the only issue i had were all the typos and incorrect order of words .   overall though , i really liked this book .")

Any idea?

Ps.: I’m using a dataset from http://jmcauley.ucsd.edu/data/amazon/links.html.

Any idea?

Yeah, you don’t want to use predict_with_targs(). torchtext shuffles batches, so the order is wrong.

To get predictions you have to do it one by one as in the other post I linked. It’s worth reading the whole discussion there.

Thanks, @rob, I’ll check it out.

Running it one by one I got 439 incorrect texts, comparing res with y I got 436 (np.sum(y != res)). Wondering why.