Hey @sebastianruder I’ve done a bit of work on this. I wanted to make it as clear and simple as possible.
I think this version is simple and better than the one above.
I do have one question. Is there a way to create a rnn_classifer without all those parameters being required? A lot of them look like they should only be required at train time.
Also, generally, does this code look right to you?
def load_model(self):
bptt,em_sz,nh,nl = 70,400,1150,3
dps = np.array([0.4,0.5,0.05,0.3,0.4])*0.5
num_classes = 2 # this is the number of classes we want to predict
vs = len(self.itos)
self.model = get_rnn_classifer(bptt, 20*70, num_classes, vs, emb_sz=em_sz, n_hid=nh, n_layers=nl, pad_token=1,
layers=[em_sz*3, 50, num_classes], drops=[dps[4], 0.1],
dropouti=dps[0], wdrop=dps[1], dropoute=dps[2], dropouth=dps[3])
trained_encoder_path = str((self.MODEL_PATH/'lm1_enc.h5').absolute())
trained_classifier_path = str((self.MODEL_PATH/'clas_2.h5').absolute())
self.model[0].load_state_dict(torch.load(trained_encoder_path, map_location=lambda storage, loc: storage))
self.model.load_state_dict(torch.load(trained_classifier_path, map_location=lambda storage, loc: storage))
self.model.reset()
self.model.eval()
def predict_text(self, text):
# prefix text with tokens:
# xbos: beginning of sentence
# xfld 1: we are using a single field here
input_str = 'xbos xfld 1 ' + text
# predictions are done on arrays of input.
# We only have a single input, so turn it into a 1x1 array
texts = [input_str]
# tokenize using the fastai wrapper around spacy
tok = Tokenizer().proc_all_mp(partition_by_cores(texts))
# turn into integers for each word
encoded = [self.stoi[p] for p in tok[0]]
# we want a [x,1] array where x is the number
# of words inputted (including the prefix tokens)
ary = np.reshape(np.array(encoded),(-1,1))
# turn this array into a tensor
tensor = torch.from_numpy(ary)
# wrap in a torch Variable
variable = Variable(tensor)
# do the predictions
predictions = self.model(variable)
# convert back to numpy
numpy_preds = predictions[0].data.numpy()
return(numpy_preds[0])