How to Fix "expected dtype Float but got dtype Long" in PyTorch
The error expected dtype Float but got dtype Long means pyTorch operations like loss functions, matrix multiplications, and neural network layers expect float tensors. When you pass integer (Long) tensors, PyTorch raises this error because it does not auto-cast between these types.
What Causes This Error
PyTorch operations like loss functions, matrix multiplications, and neural network layers expect float tensors. When you pass integer (Long) tensors, PyTorch raises this error because it does not auto-cast between these types.
Scenario 1: Labels in Binary Classification (BCELoss)
BCELoss and BCEWithLogitsLoss expect float targets, but integer labels are common.
The Error
labels = torch.tensor([0, 1, 1, 0]) # dtype: torch.int64 (Long)
output = model(x) # float32
loss = nn.BCEWithLogitsLoss()(output, labels)
# RuntimeError: expected dtype Float but got dtype Long
The Fix
labels = torch.tensor([0, 1, 1, 0]).float() # Convert to float32
output = model(x)
loss = nn.BCEWithLogitsLoss()(output, labels) # Works!
# Alternative: specify dtype at creation
labels = torch.tensor([0, 1, 1, 0], dtype=torch.float32)
Binary cross-entropy needs float targets because it computes log probabilities. Integer labels must be cast to float first.
Scenario 2: Regression with MSELoss
MSELoss needs float inputs but data loading may produce integers.
The Error
targets = torch.tensor([3, 7, 2, 9]) # int64
predictions = model(x) # float32
loss = nn.MSELoss()(predictions, targets)
# RuntimeError: expected dtype Float but got dtype Long
The Fix
targets = torch.tensor([3, 7, 2, 9]).float() # float32
predictions = model(x)
loss = nn.MSELoss()(predictions, targets) # Works!
# Or in your Dataset.__getitem__:
def __getitem__(self, idx):
return self.features[idx].float(), self.targets[idx].float()
MSE computes (prediction - target)^2, which requires both tensors to be floating point. Converting in your Dataset is the cleanest fix.
Scenario 3: Matrix Multiplication with Integer Tensors
torch.matmul and @ operator require float tensors for most operations.
The Error
a = torch.tensor([[1, 2], [3, 4]]) # int64
b = torch.tensor([[5, 6], [7, 8]]) # int64
# On some operations this works, but mixing with float fails:
w = torch.randn(2, 2) # float32
result = a @ w
# RuntimeError: expected dtype Float but got dtype Long
The Fix
a = torch.tensor([[1, 2], [3, 4]]).float()
w = torch.randn(2, 2)
result = a @ w # Works! Both are float32
# Or use .to() for explicit control:
a = torch.tensor([[1, 2], [3, 4]]).to(torch.float32)
When mixing tensor types in arithmetic, PyTorch requires explicit casting. Using .float() or .to(torch.float32) converts Long tensors to Float.
Quick Debugging Checklist
- Print tensor
.dtypeand.devicebefore operations - Check for in-place operations:
+=,*=,.add_(),.mul_() - Verify shapes with
print(tensor.shape)at each step - Use
torch.autograd.set_detect_anomaly(True)to pinpoint the exact operation
# Enable anomaly detection to find the exact line
torch.autograd.set_detect_anomaly(True)
# Check tensor properties
print(f"dtype: {tensor.dtype}, device: {tensor.device}, shape: {tensor.shape}")
print(f"requires_grad: {tensor.requires_grad}")