How to Fix "expected dtype Float but got dtype Long" in PyTorch

In PyTorch, fix "expected scalar type Float but found Long" by casting the integer tensor with .float() or .to(torch.float32) before the loss.

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

# 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}")
Try the Shape Mismatch Solver

TENSOR PREFLIGHT · DOWNLOADABLE PYTORCH TOOLKIT

Take the next step in your own PyTorch model.

Trace a local forward pass, inspect tensor shapes in an HTML report, and turn a repair into a regression check. Includes eight broken-and-fixed workflows, source code and setup instructions.

Is this toolkit right for my problem?

Use the free calculator for a layer formula or a quick shape check.

Consider Tensor Preflight when you have a local PyTorch model and want a forward-pass report plus repeatable checks. Python 3.9+ and PyTorch 2.8+ are required. It does not run your model in this website or automatically repair it.

Open the sample report before buying to check the output format.

Python 3.9+ · PyTorch 2.8+ · Runs locally

Get Tensor Preflight — $29 One-time payment · ZIP download See a sample report
By the same builder: GitHub, theluckystrike BeLikeNative, Grammar AI EarlyThunder, Dev Blog Bug Bounty Reality Zovo, AI Dev Tools