In part 1, we could generate predictions from a LM (in my case, get it to turn prompts into full sentences) using something like
def test(s):
m=learner.model
#m[0].bs = 1
t=TEXT.numericalize(s)
res,*_ = m(t)
i = 0
prnt = ''
while prnt != '.' and i < bptt:
n=res[-1].topk(2)[1]
n = n[1] if n.data[0]==0 else n[0]
prnt = TEXT.vocab.itos[n.data[0]]
print(prnt, end=' ')
res,*_ = m(n[0].unsqueeze(0))
i += 1
However, that was using torchtext - I looked around the source code of text.py and couldn’t figure out how to do this with the new class. I’m not interested in a classifier, just in the language model.
I get this error message ~/dl/fastai/courses/dl2/fastai/lm_rnn.py in forward(self, input) 79 dropouth, list of tensors evaluated from each RNN layer using dropouth, 80 """ ---> 81 sl,bs = input.size() 82 if bs!=self.bs: 83 self.bs=bs
ValueError: not enough values to unpack (expected 2, got 1)
I’m trying to run the same code from Lesson 4 with the language model trained at the different word corpus and after running this piece:
for i in range(50):
n=res[-1].topk(2)[1]
n = n[1] if n.data[0]==0 else n[0]
print(TEXT.vocab.itos[n.data[0]], end=' ')
res,*_ = m(n[0].unsqueeze(0))
print('...')
I get the same error:
~/fastai/courses/dl1/fastai/lm_rnn.py in forward(self, input)
91 dropouth, list of tensors evaluated from each RNN layer using dropouth,
92 """
---> 93 sl,bs = input.size()
94 if bs!=self.bs:
95 self.bs=bs
ValueError: not enough values to unpack (expected 2, got 1)
It’s probably something very simple and I’m not getting it… Please help.
Are you getting this error with the original Lesson 4 notebook or after introducing the modifications discussed above? Try to check out a fresh copy of the fastai github repo and also update your conda dependencies. With this PR merged this morning, predictions should work out of the box in that lesson again.
This is how I’m generating text with the Part 2 type language model. This function takes a raw text string, tokenizes it, numericalizes it and creates a variable from the numericalized text. I ran into the ValueError: not enough values to unpack (expected 2, got 1) issue and fixed it by adding a unit axis to the variable with V(np.array(s_nums))[None].
For predicting text I found that torch.topk resulted in the model repeating itself a lot. I get better looking predictions using torch.multinomial instead.
def sample_model(m, s, l=50):
s_toks = Tokenizer().proc_text(s)
s_nums = [stoi[i] for i in s_toks]
s_var = V(np.array(s_nums))[None]
m[0].bs=1
m.eval()
m.reset()
res, *_ = m(s_var)
print('...', end='')
for i in range(l):
r = torch.multinomial(res[-1].exp(), 2)
#r = torch.topk(res[-1].exp(), 2)[1]
if r.data[0] == 0:
r = r[1]
else:
r = r[0]
word = itos[to_np(r)[0]]
res, *_ = m(r[0].unsqueeze(0))
print(word, end=' ')
m[0].bs=bs
Yes, I’m getting this error with original notebook code. The only part changed is the dataset–I use another set of text files. I’ve pulled the repo and noticed there were some changes made to the lesson 4 notebook but, unfortunately, the code is still throwing me that error. Anything else I could do?
I’m using the Lesson 4 code and looks like I need to load Lesson 10 dependencies. Do you think I should try doing that or it’s too much of a change in code between the lessons?
This is due to the PyTorch’s changes of data dimension. Change n[0].unsqueeze(0) to n[0].unsqueeze(0).unsqueeze(0) can solve the problem. In addition, since in this example tensor n only contains one element after n = n[1] if n.data[0]==0 else n[0], so you want to avoid using n[0]. So in summary this is what you need:
FROM