Single Prediction with NLP example

I think you are trying to predict for single sentence. I think below code will help you to do that.

Convert your list of string into list of integer by using tokenizer.

encoded = np.array([self.stoi[o] for o in p])
t = torch.from_numpy(encoded)
ary = np.reshape(encoded, (-1, 1))
prediction = model(t)
numpy_preds = prediction[0].data.numpy()
scores = self.softmax(numpy_preds[0])[0]

def softmax(self, x):
‘’’
Softmax on numpy
Source: fastai
‘’’
if x.ndim == 1:
x = x.reshape((1, -1))
max_x = np.max(x, axis=1).reshape((-1, 1))
exp_x = np.exp(x - max_x)
return exp_x / np.sum(exp_x, axis=1).reshape((-1, 1))

Let me know if it doesn’t work for you.

1 Like