Sorry for that,
Here is my code.
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
map_location = lambda storage, loc: storage
if torch.cuda.is_available():
map_location = None
def load_model(tokenizer_path, model_path, num_classes):
# these parameters aren't used, but this is the easiest way to get a model
bptt, em_sz, nh, nl = 70, 400, 1150, 3
drop_out = np.array([0.4, 0.5, 0.05, 0.3, 0.4]) * 0.5
drop_mult = 0.5
dps = drop_out * drop_mult
ps = [0.1]
ps = [dps[4]] + ps
file = open(tokenizer_path, "rb")
tokenizer = pickle.load(file)
# turn it into a string to int mapping.
stoi = collections.defaultdict(lambda: 0, {str(v): int(k) for k, v in enumerate(tokenizer)})
lin_ftrs = [50]
layer = [em_sz * 3] + lin_ftrs + [num_classes]
vs = len(tokenizer)
model = get_rnn_classifier(bptt, 20 * 70, num_classes, vs, emb_sz=em_sz, n_hid=nh, n_layers=nl, pad_token=1,
layers=layer, drops=ps, weight_p=dps[1], embed_p=dps[2], hidden_p=dps[3])
model.load_state_dict(torch.load(model_path, map_location=map_location))
model.to(device)
model.eval()
return stoi, model
stoi, model = load_model(os.path.join(tok_path "itos.pkl"),
os.path.join(models_path_sent, "model.pth"), 3)
def predict( padded_sentence):
# do the predictions
encoded = np.transpose(np.array([[stoi[o] for o in p] for p in padded_sentence]))
t = torch.from_numpy(encoded).to(device)
variable = Variable(t)
predictions, *_ = model(variable)
scores = [[soft_max(m.data.numpy())[0]] for m in predictions.cpu()]
classes = ['One', 'Two', 'Three']
result = [classes[np.argmax(res)] for res in scores]
return result
My input to the function is
messages = ["I don't see the difference between these bodysuits and the more expensive ones. Fits my boy just right",
"Very nice basic clothing. I think the size is fine. I really like being able to find these shades of green, though I have decided the lighter shade is really a feminine color. This is the only brand that I can find these muted greens",
"I love these socks. They fit great (my 15 month old daughter has thick ankles) and she can zoom around on the kitchen floor and not take a nose dive into things.",
"These shoes are very comfortable and apparently well-made. My single quibble with the black pair I got here on amazon has to do with the flimsiness of the tongue. I like a nice strong tongue. (And who doesn't?) Whatever they saved on material here may have been a mistake. On the other hand, maybe the sole will wear out first (or it will be a dead heat)."]
messages = [message.lower() for message in messages]
tok = [message.split() for message in messages]
max_len = max([len(s) for s in tok])
padded_sentence = [pad(t, max_len - len(t))
if len(t) < max_len else ['xxbos', 'xxfld', '1'] + t for t in tok]
print(predict(messages)
If I use the CPU to predict above message it take 40 times more than GPU prediction time.