I’ve been trying to do (true/false) classification with ULMFit and I’ve had some trouble working out how to predict on just normal text (not loaded from a dataset). Or rather - I can get it to do something, but the predictions seem wrong.
So I thought I’d take the first row of the validation set and try to get it to predict the same result manually.
Here’s what I’m aiming for:
preds = learn.predict()
preds[0]
array([ 0.88123, -0.47445], dtype=float32)
Here’s my code:
# get the first row of the validation set
print("validation dataset row:", val_dl.dataset.x[0])
# convert it to string tokens
pred_toks = [itos[i] for i in val_dl.dataset.x[0]]
# convert it back to vocab indexes
pred_idxs = [stoi[p] for p in pred_toks]
# print these indexes out
print("converted row:", pred_idxs)
# they appear to be the same, so the prediction should be the same
# get the model
m = learn.model
# set the batch size
m[0].bs = 1
# put into evaluation mode
m.eval()
m.reset()
# convert the (list of) array(s) of indexes to a Pytorch vector
tensor = T([in_ints])
# we need a PyTorch variable
variable = V(tensor)
# pass to the model to make the prediction
result, *_ = m(variable)
result
Which outputs
validation dataset row: [4, 5, 6, 3, 4162, 313, 10, 256, 5251, 2]
converted row: [4, 5, 6, 3, 4162, 313, 10, 256, 5251, 2]
Variable containing:
7.8798 -10.7048
8.7254 -11.7704
6.8029 -8.6208
8.4807 -11.5354
8.1717 -9.7377
12.7380 -12.5275
6.6641 -9.7890
16.6947 -17.6726
6.9700 -8.1962
6.6253 -7.7002
[torch.cuda.FloatTensor of size 10x2 (GPU 0)]
I’ve seen code getting the maximum score from this result to get the class score. But I can’t see any way that is going to match the 0.88123, -0.47445 that .predict() gave me.
What am I doing wrong?