Ошибка времени выполнения: форма ‘[1, 71, 1]’ недопустима для ввода размера 18176

#nlp #pytorch

Вопрос:

Я пытался напечатать предсказанное предложение из моей модели Seq2Seq. Я нашел вспомогательный код отсюда: вспомогательный код

Мой полный блокнот можно найти здесь: Мой код

Я получаю ошибку в функции Persuasive_sentence в форме вложения. Я новичок в PyTorch и не уверен, в чем проблема.

Кодировщик:

 class Encoder(nn.Module):
    def __init__(self, input_dim, emb_dim, hid_dim, n_layers, dropout):
        super().__init__()
        
        self.hid_dim = hid_dim
        self.n_layers = n_layers
        
        self.embedding = nn.Embedding(input_dim, emb_dim)
        
        self.rnn = nn.LSTM(emb_dim, hid_dim, n_layers, dropout = dropout)
        
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, src):
        embedded = self.dropout(self.embedding(src))
        #print(embedded.shape)
        
        embedded = embedded.reshape(1, embedded.shape[0], embedded.shape[1])
        
        outputs, (hidden, cell) = self.rnn(embedded)
        return hidden, cell
 

Функция Persuasive_sentence (), которая в основном печатает предсказанное предложение из моей модели:

 def Persuasive_sentence(model, sentence, input, per_out, device, max_length=50):

    # Create tokens using spacy and everything in lower case (which is what our vocab is)
    if type(sentence) == str:
        tokens = [token.text for token in eng(sentence)]
    else:
        tokens = [token for token in sentence]

    # Add <SOS> and <EOS> in beginning and end respectively
    tokens.insert(0, input.init_token)
    tokens.append(input.eos_token)

    # Go through each german token and convert to an index
    text_to_indices = [input.vocab.stoi[token] for token in tokens]

    # Convert to Tensor
    sentence_tensor = torch.LongTensor(text_to_indices).unsqueeze(1).to(device)

    outputs = [per_out.vocab.stoi["<sos>"]]
    for i in range(max_length):
        trg_tensor = torch.LongTensor(outputs).unsqueeze(1).to(device)

        with torch.no_grad():
            output = model(sentence_tensor, trg_tensor)

        best_guess = output.argmax(2)[-1, :].item()
        outputs.append(best_guess)

        if best_guess == per_out.vocab.stoi["<eos>"]:
            break

    translated_sentence = [per_out.vocab.itos[idx] for idx in outputs]
    # remove start token
    return translated_sentence[1:]
 

Я получаю ошибку в форме встроенного вектора:

 RuntimeError                              Traceback (most recent call last)
<ipython-input-69-1805f27846e2> in <module>()
      1 model.eval()
      2 print(inputs[3])
----> 3 print(Persuasive_sentence(model, inputs[3], inp_fields, out_fields, device, max_length=50))

4 frames
<ipython-input-62-387f239d5614> in Persuasive_sentence(model, sentence, input, per_out, device, max_length)
     22 
     23         with torch.no_grad():
---> 24             output = model(sentence_tensor, trg_tensor)
     25 
     26         best_guess = output.argmax(2)[-1, :].item()

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

<ipython-input-56-9072bb990be1> in forward(self, src, trg, teacher_forcing_ratio)
     25 
     26         #last hidden state of the encoder is used as the initial hidden state of the decoder
---> 27         hidden, cell = self.encoder(src)
     28 
     29         #first input to the decoder is the <sos> tokens

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

<ipython-input-54-ea8f333f9008> in forward(self, src)
     19         #print(embedded.shape)
     20 
---> 21         embedded = embedded.reshape(1, embedded.shape[0], embedded.shape[1])
     22         #embedded = [src len, batch size, emb dim]
     23 

RuntimeError: shape '[1, 71, 1]' is invalid for input of size 18176
 

I added the embedded = embedded.reshape(1, embedded.shape[0], embedded.shape[1]) because earlier I was getting an error that the input to RNN must be a 3-dimensional matrix. The shape of the embedded vector earlier was [32, 256] which is [batch_size, embedding_dim].
Please suggest if reshaping the vector was right or not and also, what is wrong in the Persuasive_sentence() function