0Pricing
Deep Learning Academy · Lesson

Binary Cross-Entropy with Logits

The stable loss for two-class problems.

Binary Cross-Entropy with Logits is a free Deep Learning Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Classes, One Decision

When your model answers a yes or no question like spam or not spam, you reach for binary cross-entropy, the loss built for two-class problems. ✅

From Score to Probability

A model first outputs a raw score called a logit. The sigmoid function squashes that logit into a probability between 0 and 1.

import torch
prob = torch.sigmoid(logit)

What Cross-Entropy Rewards

Cross-entropy gives a small loss when the predicted probability is close to the true label, and a large loss when it is confidently wrong.

Confident and Wrong Hurts

Predicting 0.99 when the true label is 0 produces a huge loss. Cross-entropy punishes confident mistakes far more than uncertain ones.

The Naive Loss Choice

You could apply sigmoid yourself, then use nn.BCELoss on the probabilities. It works, but there is a safer way for real training.

import torch.nn as nn
loss_fn = nn.BCELoss()
loss = loss_fn(prob, target)

Feed Logits, Not Probabilities

The preferred loss is BCEWithLogitsLoss. It takes raw logits directly and applies the sigmoid internally for you.

import torch.nn as nn
loss_fn = nn.BCEWithLogitsLoss()
loss = loss_fn(logit, target)

Why Combine the Steps

Fusing sigmoid and the loss uses the log-sum-exp trick, which avoids overflow and gives stable gradients even at extreme logits.

Skip the Extra Sigmoid

Because BCEWithLogitsLoss applies sigmoid itself, your model's last layer should output raw logits with no sigmoid attached.

Targets Are 0 or 1

Pass float targets of 0.0 or 1.0 with the same shape as your logits. A mismatched shape is the classic BCE bug to watch for.

target = torch.tensor([1.0, 0.0, 1.0])

Sigmoid Only at Inference

During training the loss handles the squashing. To read a probability for a prediction, apply sigmoid to the logit at inference time.

Tilt the Balance with pos_weight

If positives are rare, the pos_weight argument scales up their contribution so the model stops ignoring the minority class.

Quick Check

You are doing binary classification and want numerical stability. What should you feed the loss?

Recap: Stable Yes or No

For two-class tasks you feed raw logits to BCEWithLogitsLoss, which squashes and scores in one stable step. Save sigmoid for reading probabilities later. 👍

Frequently asked questions

Is the “Binary Cross-Entropy with Logits” lesson free?

Yes — the full text of “Binary Cross-Entropy with Logits” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.

What will I learn in “Binary Cross-Entropy with Logits”?

The stable loss for two-class problems. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Deep Learning Academy?

No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Binary Cross-Entropy with Logits” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Deep Learning Academy lesson?

Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. MSE & MAE for Regression
  2. Binary Cross-Entropy with Logits
  3. Cross-Entropy for Multiclass
  4. Class Weights for Imbalanced Data
← Back to Deep Learning Academy