Skip to content

[Solved][PyTorch] TypeError: expected Tensor as element 0 in argument 0, but got list

Problem

Today, when I was trying to use nn.Embedding() to splice and calculate sentence vectors by myself, I got an error like this on the DataLoader I prepared in advance:

TypeError: expected Tensor as element 0 in argument 0, but got list

After studying it carefully, I found that the problem occurred when I made the DataLoader. My data was not in Tensor data type, so I couldn’t use DataLoader to determine the batch size by myself and feed my model as training data.

The same error will occur on:

torch.cat((tensor1, tensor2), 0)T

Or

torch.stack((tensor1, tensor2), 0)


In the above case, as long as one of tensor1 or tensor2 is not the data type of tensor, there will be an error at the top of the article.

As for why DataLoader has this error? I have checked it now and it is mostly because DataLoader will use torch.stack() to help us stitch together data.


Solution

To put it bluntly, the solution is very simple. Generally, when error is reported when splicing data, print out the tensor to confirm that the data is not a tensor type.

As for my case, since the format of my data cannot be compromised, I decided to abandon the DataLoader and decide the batch size by myself before putting it into the model.

Although it is a little troublesome, it is indeed feasible.


References


Read More

Leave a Reply