FYI as I’m sure you’ve noticed, I haven’t used a test set with this class before - sorry about the shuffling thing! I’m working on tomorrow’s class at the moment so won’t be able to debug right away, but if you want to do so, try looking at how torchtext is handling this. I’m not sure if the issue is in torchtext, or just how I’m calling it.
Both torchtext and fastai are pretty simple code to read - hopefully it’ll be reasonably clear what’s going on. Let me know if I can help clarify anything!
@KevinB was able to get a submission into the Happiness competition, so I think he has something that works, and I don’t think it’s as complicated as we are making it. Perhaps he can enlighten us when he has a chance.
If you look at the source code for BucketIterator here, you can see that it always sorts (even if you set sort=False. That simply, shifts the sorting to happen in the batches.
So all I did is use what Jeremy did in his lesson 4 notebook to predict what the sentence will be. I set my batchsize to 1 and I pulled the text from the CSV file directly. Then I just looped through those one at a time and tied them to a file. Then I just chose the top prediction and converted it from the index to the actual word. Is there any specific code/questions you are wondering about?
m = m3.model
m[0].bs = 1
for i in range(tst.values[:,1].shape[0]):
ss = tst["Description"][i] #Actual text review
s = [spacy_tok(ss)]
t = TEXT.numericalize(s)
m.eval()
m.reset()
res,*_ = m(t)
prediction = PH_LABEL.vocab.itos[to_np(torch.topk(res[-1], 1)[1])[0]]
I had previously missed the first line where you have to do m = m3.model before calling m(t). I guess that is easy to miss since m3 is the result of a call to get_model().
Anyway, good on you for getting this to work. It must mean you understand the code quite well.
I think I’m getting close to solving this by replacing BucketIterator with Iterator.
I’m getting predictions from my test dataset, BUT there are less predictions than there are examples in the test dataset for some reason (8 less to be specific). See below:
So basically, we need BucketIterator for performance reasons … and BucketIterator is going to do some kind of sorting (either over the entire dataset or within each minibatch). So for validation and test datasets, we’re going to have to change our bs=1 in order for the results to come back in order.
Yah I ultimately went with @KevinB’s approach above.
I was trying to modify the framework to make the behavior more similar to image classification … got close, but not close enough. I will at least have a PR coming your way to include an optional test dataset to TextData.from_splits() that works.
After building our ModelData object, it automatically fills the TEXT object with a very important attribute: TEXT.vocab. This is a vocabulary, which stores which words (or tokens) have been seen in the text, and how each word will be mapped to a unique integer id. We’ll need to use this information again later, so we save it.
m=m3.model
test_str ="bitcoin is bad"
tokenized_str = spacy_tok(test_str)
token_lst = [sent.string.strip() for sent in tokenized_str]
t = TEXT.numericalize([token_lst])
print(t)
# Set batch size to 1
m[0].bs=1
# Turn off dropout
m.eval()
# Reset hidden state
m.reset()
# Get predictions from model
res,*_ = m(t)
num_res = to_np(torch.topk(res[-1], 1)[1])[0]
print(num_res)