본문 바로가기

ERROR!

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same 오류 해결방법

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same 오류 해결방법

File c:\Users\User\anaconda3\envs\kaggle\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
 1515 # If we don't have any hooks, we want to skip the rest of the logic in
 1516 # this function, and just call forward.
...
 455 _pair(0), self.dilation, self.groups)
--> 456 return F.conv2d(input, weight, bias, self.stride,
 457 self.padding, self.dilation, self.groups)

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
오류 내용

 

이름 그대로 데이터 형식이 달라서 생기는 문제로 모델의 weight는 torch.FloatTensor로 cpu에 있는데 input은 Gpu에 있어서 생기는 문제다.

 

weight를 cpu에서 gpu로 옮기거나 input을 gpu에서 cpu로 옮기면 된다.

 

나는 weight를 gpu로 옮겼다.

 

model.train()전에 model.to(DEVICE)를 하면 된다.

    model.to(DEVICE)  # 모델을 GPU로 이동
    model.train()

 

여기서 DEVICE는 cuda를 의미한다.

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

 

해결되었다.

train_loss = train(model, train_loader, criterion, 
 optimizer, save_distrib=True)
 valid_loss = validate(model, valid_loader, criterion)

 print(f"Epoch {i+1}/{config['epochs']}\nTrain loss: {train_loss:.4f}\t Validation loss: {valid_loss:.4f}\tlr: {curr_lr:.4f}")
잘 돌아감 휴