λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
μ—λŸ¬ 및 이슈

[PyTorch] RuntimeError: mat1 and mat2 must have the same dtype, but got Double and Float

by kaizen_bh 2025. 9. 3.

 

문제 λ°œμƒ λ°°κ²½

 

μ‚¬μš©ν•œ λ°μ΄ν„°λŠ” λ³΄μŠ€ν„΄ 데이터

 

Boston-house-price-data

Predicting house prices in Boston

www.kaggle.com

 

 

 

PyTorch λ₯Ό μ΄μš©ν•΄ ν•΄λ‹Ή λ°μ΄ν„°λ‘œ κ°„λ‹¨ν•œ μ„ ν˜• νšŒκ·€λ₯Ό ν•΄λ³΄λŠ” 쀑 μ•„λž˜μ˜ λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ°œμƒν•˜μ˜€λ‹€

RuntimeError: mat1 and mat2 must have the same dtype, but got Double and Float

 

 

 


 

 

 

상세 μ—λŸ¬ λ‚΄μš©

μ„ ν˜• νšŒκ·€ λͺ¨λΈμ— 데이터λ₯Ό μž…λ ₯ν•˜μ—¬ ν…ŒμŠ€νŠΈν•΄λ³΄λ©΄ μ•„λž˜μ™€ 같은 μ—λŸ¬κ°€ λ°œμƒν•œλ‹€

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipython-input-1627512362.py in <cell line: 0>()
      3 model.to(device)
      4 
----> 5 assert model(X_test_tensor).shape == (102, 1)
      6 print("Model is implemented correctly!")

5 frames
/usr/local/lib/python3.12/dist-packages/torch/nn/modules/linear.py in forward(self, input)
    123 
    124     def forward(self, input: Tensor) -> Tensor:
--> 125         return F.linear(input, self.weight, self.bias)
    126 
    127     def extra_repr(self) -> str:

RuntimeError: mat1 and mat2 must have the same dtype, but got Double and Float

 

  • ν–‰λ ¬ κ³± μ—°μ‚°(matmul)을 μˆ˜ν–‰ν•  λ•Œ, μž…λ ₯ 데이터(mat1)와 λͺ¨λΈ κ°€μ€‘μΉ˜(mat2)의 dtype, 데이터 νƒ€μž…μ΄ μ„œλ‘œ λ‹€λ₯΄λ‹€λŠ” 의미
  • 그럼 μ–΄λ””μ„œ 잘λͺ»λœ 걸까? μ „μ²˜λ¦¬ λ‹¨κ³„μ—μ„œ 이상이 μžˆμ—ˆμŒ

 

일반적인 μ „μ²˜λ¦¬ κ³Όμ •λ“€, x와 y 뢄리, ν•™μŠ΅ 및 ν…ŒμŠ€νŠΈμ…‹ 뢄리, μ •κ·œν™” 등을 거친 뒀에 PyTorch둜 κ΅¬μΆ•λœ μ„ ν˜• νšŒκ·€ λͺ¨λΈμ—μ„œ μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„  데이터 ν”„λ ˆμž„μœΌλ‘œ λ˜μ–΄μžˆλŠ” 데이터듀을 ν…μ„œλ‘œ λ³€ν™˜ν•΄μ€˜μ•Όν•œλ‹€

torch.tensor(X_train.values).to(device)
---
=> torch.float64

 

데이터 ν”„λ ˆμž„μ—μ„œ values둜 λ„˜νŒŒμ΄ ν˜•νƒœλ‘œ μΆ”μΆœν•œ 뒀에 ν…μ„œλ‘œ λ³€ν™˜ ν›„ GPU (λ˜λŠ” CPU) 에 데이터λ₯Ό μ˜¬λ¦°λ‹€

λ¬Έμ œλŠ” torch.tensor()μ—μ„œ 데이터 νƒ€μž…μ„ μ§€μ •ν•˜μ§€ μ•Šμ•˜μ„ λ•Œ λ³€ν™˜λœ νƒ€μž…μ΄ float64, double μ΄λΌλŠ” 것

 

ν•˜μ§€λ§Œ nn.Linear의 weight와 biasλŠ” 기본적으둜 torch.float32λ₯Ό μ‚¬μš©ν•œλ‹€

λ”°λΌμ„œ μž…λ ₯ 데이터와 λͺ¨λΈμ˜ κ°€μ€‘μΉ˜ 데이터 νƒ€μž…μ΄ μ•ˆλ§žμœΌλ©΄μ„œ κ³±μ—°μ‚° λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€

 

 

μ—¬κΈ°μ„œ μ£Όμ˜ν•  점은 torch.tensor() μ—μ„œ 데이터 νƒ€μž…μ„ μ§€μ •ν•˜μ§€ μ•Šμ„ 경우 μž…λ ₯받은 데이터 νƒ€μž…μ„ 따라간닀

X_train.values.dtype
=> dtype('float64')

 

ν…μ„œ λ³€ν™˜μ„ μœ„ν•΄ 데이터 ν”„λ ˆμž„μ—μ„œ λ„˜νŒŒμ΄λ‘œ μΆ”μΆœν–ˆμ„ λ•Œ 데이터 νƒ€μž…μ΄ float64 μ˜€κΈ° λ•Œλ¬Έμ— 이λ₯Ό ν…μ„œλ„ λ”°λΌκ°€κ²Œ 된 것이닀

 

 

 

 

 

 


 

 

 

 

ν•΄κ²° 방법

곡식 λ¬Έμ„œ 늘 μ°Έκ³ ν•˜κΈ°.

 

https://docs.pytorch.org/docs/stable/generated/torch.nn.Linear.html#torch.nn.Linear

 

Linear — PyTorch 2.8 documentation

Linear class torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)[source] Applies an affine linear transformation to the incoming data: y=xAT+by = xA^T + by=xAT+b. This module supports TensorFloat32. On certain ROCm devices, when

docs.pytorch.org

 

 

 

 

torch.tensor(X_train.values, dtype=torch.float32).to(device)
---
=> torch.float32

 

ν…μ„œ λ³€ν™˜ κ³Όμ •μ—μ„œ 데이터 νƒ€μž…μ„ λͺ¨λΈμ— 맞게 맞좰주면 λœλ‹€

torch.tensorμ—μ„œ dtype으둜 μ›ν•˜λŠ” 데이터 νƒ€μž… 선택 κ°€λŠ₯ν•˜λ‹€

 

늘 μž…λ ₯λ°›λŠ” λ°μ΄ν„°μ˜ νƒ€μž…μ„ 잘 μ‚΄νŽ΄λ³΄κ³  μ΄λ ‡κ²Œ 데이터 μ „μ²˜λ¦¬λ₯Ό ν•  경우 μ΄μŠˆκ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘ 데이터 νƒ€μž…μ„ λͺ…μ‹œν•΄λ‘μž