Solve the problem of pytorch data type error reporting

  • 2021-09-16 07:23:50
  • OfStack

pytorch error report:

RuntimeError: Expected object of type Variable [torch. LongTensor] but found type Variable [torch. cuda. ByteTensor] for argument # 1 'argument1'

Solution:

The pytorch framework uses LongTensor to store labels, so when dataset returns label at the beginning of 1, it is necessary to return the data type corresponding to LongTensor, that is, numpy. int64

Supplement: Various problems and solutions encountered in using pytorch

Various problems and solutions encountered in using pytorch:

RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1'

RuntimeError: The size of tensor a (12800) must match the size of tensor b (100) at non-singleton dimension 0

The input dimension is 12800, the output dimension is 100, and the input and output dimensions are not 1. The correct examples are as follows:


inputs = [(1,2,3), (2,3,4)]
outsputs = [4, 5]

Change the length of input and output to 1

Take the first element of tensor


XXX.item() # XXX For tensor Object 

Elements in tensor change data types


#  It is often necessary to modify the data type because of data type errors 
XXX.int()
XXX.float()

Supplement: Dataloader error of Pytorch: TypeError: batch must contain tensors, numbers, dicts or lists

Specific error reporting:

TypeError: batch must contain tensors, numbers, dicts or lists; found < class 'PIL.Image.Image' >

Code for loader:


dataloader=torch.utils.data.DataLoader(dataset,batch_size=1,shuffle=True)

On the surface, there is no problem with this code. In fact, the problem lies in the requirements of dataloader mechanism. dataloader requires receiving an tensor, while my dataset does not do transform, so the getitem function of dataset returns an Image object of PIL, so it will report an error


Related articles: