0Pricing
Deep Learning Academy · Lektion

Binäre Cross-Entropy mit Logits

Der stabile Loss für Zwei-Klassen-Probleme.

Binäre Cross-Entropy mit Logits ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 👍

Häufig gestellte Fragen

Ist die Lektion „Binäre Cross-Entropy mit Logits“ kostenlos?

Ja — der vollständige Text von „Binäre Cross-Entropy mit Logits“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Binäre Cross-Entropy mit Logits“?

Der stabile Loss für Zwei-Klassen-Probleme. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Deep Learning Academy zu starten?

Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Binäre Cross-Entropy mit Logits“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?

Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. MSE und MAE für Regression
  2. Binäre Cross-Entropy mit Logits
  3. Cross-Entropy für mehrere Klassen
  4. Klassengewichte für unausgeglichene Daten
← Zurück zu Deep Learning Academy