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
- [Solved][PyTorch] RuntimeError: Attempting to deserialize object on CUDA device 3 but torch.cuda.device_count() is 1.
- [Solved][PyTorch] RuntimeError: CUDA out of memory. Tried to allocate 2.0 GiB
- [Solved][PyTorch] return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse) RuntimeError: index out of range: Tried to access index 5 out of table with 4 rows. at /pytorch/aten/src/TH/generic/THTensorEvenMoreMath.cpp:237
- [Solved][PyTorch] IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
- [Solved][PyTorch] TypeError: not a sequence
- [Solved][PyTorch] ValueError: expected sequence of length 300 at dim 1 (got 3)