본문 바로가기

프로그래밍/AI

RuntimeError: Found dtype Double but expected Float

 

import torch
from torch import nn

model = nn.Linear(20, 1)
input = torch.randn(10, 20)
output = torch.tensor([[i] for i in range(10)], dtype=torch.float64)
criterion = nn.MSELoss()
optimizer = torch.optim.AdamW(params=model.parameters(), lr=0.01)
optim_loss = 1e9
epochs = 1000
best_prediction = output
best_epoch = -1
for epoch in range(epochs):
    prediction = model(input)
    # print(prediction.shape)
    # print(output.shape)
    loss = criterion(input=prediction, target=output)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    # print(output)
    if optim_loss > loss:
        best_epoch = epoch
        optim_loss = loss
        best_prediction = prediction
        # print(f"{epoch}/{epochs}, optim_loss: {optim_loss}")
print(optim_loss, best_epoch)
print(best_prediction)
print(output)

다음과 같은 코드가 있을 때, 17번 째 줄의

loss.backward()
 
부분에서 제목과 같은 에러가 발생한다.
 
 
잠시 다른 얘기를 먼저 하면, 코드 상단의 output = ~~ 부분에서
 
output = [[i] for i in range(10)]

라고 입력하면, loss.backward()에서

AttributeError: 'list' object has no attribute 'size'

라는 에러가 출력된다.

 

output = torch.tensor([[i] for i in range(10)])

로 바꿔주면, loss.backward()에서

RuntimeError: Found dtype Long but expected Float

라는 에러가 출력된다.

 

output = torch.tensor([[i] for i in range(10)], dtype=torch.float64)

로 바꿔주면(본문과 같은 코드), loss.backward()에서

RuntimeError: Found dtype Double but expected Float

라는 에러가 출력된다.

 

output = torch.tensor([[i] for i in range(10)], dtype=torch.float32)

로 바꿔주면, 정상적으로 잘 동작하는 것을 확인할 수 있다.

 

 

결론: dtype=torch.float32 로 바꿔주자.

dtype=torch.float64는 double형으로 인식하는 것 같다.

 

'프로그래밍 > AI' 카테고리의 다른 글

AttributeError: module 'torch.nn' has no attribute 'Maxpool2d'  (0) 2022.09.18