How to convert a list of tensors to a Variable argument?

Hi, I am trying to implement a seq2seq model for machine translation. I successfully encoded a batch of input sequences to their respective fixed vector representations. Then i fed the hidden representation and an input token to a decoder network. The output of decoder network is then fed to a linear layer and then finally to a softmax layer.

for i, input_t in enumerate(embedded.chunk(embedded.size(1), dim=1)):
    h_t, c_t = self.lstm(torch.squeeze(input_t), (h_t, c_t))
    output.append(F.softmax(self.linear(h_t)))

However when I try to compute the loss

loss = criterion(decoder_outputs, batch_target_sequences)

it gives the following error

expected a Variable argument, but got list

I also tried to convert it to a variable but got the following error

 Variable data has to be a tensor, but got list

Can anybody please suggest a way to convert a list of tensors to tensor so that Variable argumnet can accept it?

Thanks

1 Like

Your output seems to be a list of tensors due to the .append function. Try torch.cat(decoder_outputs) before passing it to the loss function.

1 Like

Hey thanks a lot!!

I guess this should also work

torch.stack(output)
1 Like

Only tensor can be converted to variabel in pytorch. You should convert the list to tensor and the convert to variable.