Loss Functions Guide

Compare PyTorch loss functions: CrossEntropyLoss, MSELoss, BCELoss, and more. Formulas, when to use, code examples, and common pitfalls for each loss function.

Built by Michael Lip

Frequently Asked Questions

Which loss function should I use?

Multi-class classification: CrossEntropyLoss (no Softmax needed, it's built in). Binary classification: BCEWithLogitsLoss. Regression: MSELoss (L2) or L1Loss (more robust to outliers). Regression with outliers: HuberLoss.

Why does CrossEntropyLoss include Softmax?

PyTorch's CrossEntropyLoss combines LogSoftmax + NLLLoss for numerical stability. Do NOT apply Softmax before CrossEntropyLoss, you'll get wrong gradients. Your model's final layer should output raw logits.

What is the difference between BCE and CrossEntropy?

BCELoss is for binary classification (one output per sample, 0 or 1). CrossEntropyLoss is for multi-class classification (one-of-N classes). For multi-label classification (multiple labels per sample), use BCEWithLogitsLoss.

Is this tool free?

Yes. All HeyTensor tools are free, run in your browser, and require no signup.

Does this work offline?

Once loaded, the tool runs entirely in your browser. No internet needed after the initial page load.

About This Tool

This tool is part of HeyTensor, a free suite of PyTorch and deep learning utilities. All calculations run entirely in your browser, no calculator inputs or results are sent to any server. The source code is open on GitHub.

Contact

HeyTensor is built and maintained by Michael Lip. For questions or feedback, email [email protected].

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
📊 Based on real data from our Most Common PyTorch Errors research — 20 errors ranked by frequency
By the same builder: GitHub, theluckystrike BeLikeNative, Grammar AI EarlyThunder, Dev Blog Bug Bounty Reality Zovo, AI Dev Tools