[Solved] RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

reehyezin·2022년 7월 27일
0

ErrorNote

목록 보기
2/3

pytorch를 GPU로 모델을 학습할 때 model과 dataset 모두 GPU에 올라가있어야 하는데, model이나 dataset 중 하나가 GPU가 아닌 CPU로 전달되어서 생기는 오류였다!

❤️ 에러 확인

RuntimeError: legacy constructor for device type: cpu was passed device type: cuda, but device type must be: cpu

❤️ 에러 원인

1) model: cpu, dataset: gpu

model = SRCNN()

input_sample, label_sample = torch.Tensor(input_sample), torch.Tensor(label_sample)

2) model: gpu, dataset: cpu
model = SRCNN().to(device)

input_sample, label_sample = torch.Tensor(input_sample).cuda(), torch.Tensor(label_sample).cuda()

💚 해결 방법

model = SRCNN().to(device)

input_sample, label_sample = torch.Tensor(input_sample).cuda(), torch.Tensor(label_sample).cuda()

💙 다른 방법

torch.tensor(..., device="cuda") 
torch.tensor(...).cuda() 
torch.tensor(...).to("cuda")

model.to("cuda")
profile
코딩 공부 기록💫

0개의 댓글