본문 바로가기

카테고리 없음

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

입력 또는 모델을 cuda에 올리지 않아서 생기는 현상 

 

1) 문제상황 : img.to(device) 와 target.to(device)까지는 맞으나, 이를 받는 다른 변수를 선언하여 cuda.FloatTensor가 아닌 torch.FloatTensor로 받게 됨 

 for i, (img, target) in enumerate(train):
            input, label = img.to(device), target.to(device)    
            
            optimizer.zero_grad() 
            output = model(input)
            
            ...

 

 

2) 해결: .to(device)하는 입력과 동일한 변수명으로 받아야 cuda에 올리게 된다. 

 for i, (img, target) in enumerate(train):
            img, target = img.to(device), target.to(device)    
            
            optimizer.zero_grad() 
            output = model(input)
            
            ...