Lesson6-rnn notebook RuntimeError when calling get_next() function

I tried to run Lesson6-rnn notebook and encountered:

RuntimeError: dimension specified as 0 but tensor has no dimensions

after calling get_next() function in the Test Model section:

def get_next(inp):
idxs = T(np.array([char_indices[c] for c in inp]))
p = m(*VV(idxs))
i = np.argmax(to_np§)
return chars[i]

I was wondering if you guys would help me figure out how to solve this. Thank you!

I have the same issue as well

Looks like only us got this issue. I wonder why…

Are your environment and fastai library up-to-date ?


git pull
conda env update

I’m thinking the problem is probably because of the pytorch version.
Run this to see your pytorch version. Fastai is not ready yet to use with the latest pytorch 0.4. So maybe there are some bugs in the updating.

import torch

Thank you for the hint! I have modified the code so that it works with pytorch 0.4 now. It’s the forward method that needs to be changed a little bit. I have pasted my code here in case somebody else might find it useful:

class CharLoopConcatModel2(nn.Module):
    def __init__(self, vocab_size, n_fac):
        self.e = nn.Embedding(vocab_size, n_fac)
        self.l_in = nn.Linear(n_fac, n_hidden)
        self.l_hidden = nn.Linear(2*n_hidden, n_hidden)
        self.l_out = nn.Linear(n_hidden, vocab_size)
    def forward(self, *cs):
        if cs[0].size():
            bs = cs[0].size(0)
            h = V(torch.zeros(bs, n_hidden))
            h = V(torch.zeros(n_hidden))
        for c in cs:
            inp = F.relu(self.l_in(self.e(c)))
            h = F.tanh(self.l_hidden(torch.cat((h, inp), -1)))
        return F.log_softmax(self.l_out(h), dim=-1)

Hello Yuan,

do you have any idea how to solve this issue inside the section “RNN with Pytorch”? because it gets the same error…

In function get_next change
idxs = T(np.array([char_indices[c] for c in inp]))
idxs = T(np.array([char_indices[c] for c in inp])).view(1,-1)
Worked for me.


Unfortunately, I was not able to fix it with the code line posted above.

After checking/wrapping my head through the dimensions for hours I was able to find a solution that worked for me with pytorch 0.4:
idxs = T(np.array([char_indices[c] for c in inp])).view(-1,1)

So in the end just a dimension mismatch with .view(). Maybe someone can verify this?

Best regards