Usign ULMFiT for inference

Hi,
I’m training ULMFiT text classification models for a few topics. They seem to be performing quite well on the validation data, so I’d like to wrap them in a small webapp that’d get a new text document as input and will output the models’ prediction. However, I’m can’t call the models’ forward function on a new tensor variable.
The code I’m using (this is reusing the validation data, just to mock a new document):

m = get_rnn_classifer(bptt, 20*70, c, vs, emb_sz=em_sz, n_hid=nh, n_layers=nl, pad_token=1,
          layers=[em_sz*3, 50, c], drops=[dps[4], 0.1],
          dropouti=dps[0], wdrop=dps[1], dropoute=dps[2], dropouth=dps[3])
opt_fn = partial(optim.Adam, betas=(0.7, 0.99))
learn = RNN_Learner(md, TextModel(to_gpu(m)), opt_fn=opt_fn)
learn.reg_fn = partial(seq2seq_reg, alpha=2, beta=1)
learn.clip=25.
learn.metrics = [accuracy]

lr=3e-3
lrm = 2.6
lrs = np.array([lr/(lrm**4), lr/(lrm**3), lr/(lrm**2), lr/lrm, lr])
wd = 1e-7
wd = 0
learn.load_encoder('lm1_enc')
learn.load('clas_final')
idx = np.array(val_clas[i])[None]
idx = np.transpose(idx)
tensorIdx = VV(idx)
m.forward(tensorIdx)

but I get the following error message:
AttributeError: 'MultiBatchRNN' object has no attribute 'hidden'

Please see


To solve that AttributeError: 'MultiBatchRNN' object has no attribute 'hidden' error I think you need to do learn.reset() before the m.forward.

Some examples don’t have that, but they call learn.predict() (which runs predictions against the validation set) calls reset() internally.

1 Like

Thanks so much @Samuel and @nickl !
solved the problem by starting the inference part with:
m.eval()
m.reset()
:slight_smile:

1 Like

I just wanted to ask if ULMFiT can be used for classification of sentences based on my tags like experience, quotation, motivation, etc ?

@athena: Sure, as long as you have a training dataset.