Skip to main content

Binary Cross-Entropy (BCE)


Binary Cross-Entropy Loss (BCELoss)

Binary Cross-Entropy Loss (BCELoss) is a loss function commonly used in binary classification problems. It measures how well a model’s predicted probabilities match the true binary labels (0 or 1).

Why BCELoss is Important

BCELoss is widely used in tasks such as:

  • Binary classification
  • Logistic regression
  • Discriminator training in Generative Adversarial Networks (GANs)

In GANs, the discriminator decides whether an image is real or fake, making BCELoss a natural choice.

Mathematical Definition

The Binary Cross-Entropy Loss is defined as:

L(y, ลท) = − [ y · log(ลท) + (1 − y) · log(1 − ลท) ]
        

Where:

  • y is the true label (0 or 1)
  • ลท is the predicted probability (between 0 and 1)

BCELoss in PyTorch

In PyTorch, BCELoss is implemented as:

import torch.nn as nn

criterion = nn.BCELoss()
        

The model output must be passed through a Sigmoid activation before computing the loss.

BCELoss in GANs

Discriminator

The discriminator is trained to:

  • Output 1 for real images
  • Output 0 for fake images
loss_real = BCE(D(real_images), 1)
loss_fake = BCE(D(fake_images), 0)
        

Generator

The generator tries to fool the discriminator by making fake images look real:

loss_generator = BCE(D(fake_images), 1)
        

Limitations of BCELoss

Although BCELoss is simple and effective, it can cause:

  • Vanishing gradients
  • Training instability in GANs

Because of this, modern GANs often use alternatives like BCEWithLogitsLoss, Wasserstein loss, or hinge loss.

Summary

Binary Cross-Entropy Loss is a fundamental loss function for binary classification and GAN training. While it is easy to implement and understand, more advanced loss functions are often preferred for improved stability in deep generative models.

People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *

Popular Posts

Constellation Diagrams of ASK, PSK, and FSK

๐Ÿ“˜ Overview of Energy per Bit (Eb / N0) ๐Ÿงฎ Online Simulator for constellation diagrams of ASK, FSK, and PSK ๐Ÿงฎ Theory behind Constellation Diagrams of ASK, FSK, and PSK ๐Ÿงฎ MATLAB Codes for Constellation Diagrams of ASK, FSK, and PSK ๐Ÿ“š Further Reading ๐Ÿ“‚ Other Topics on Constellation Diagrams of ASK, PSK, and FSK ... ๐Ÿงฎ Simulator for constellation diagrams of m-ary PSK ๐Ÿงฎ Simulator for constellation diagrams of m-ary QAM BASK (Binary ASK) Modulation: Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1.    BFSK (Binary FSK) Modulation: Transmits one of two signals: +√Eb​ ( On the y-axis, the phase shift of 90 degrees with respect to the x-axis, which is also termed phase offset ) or √Eb (on x-axis), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  BPSK (Binary PSK) Modulation: Transmits one of two signals...

Fading : Slow & Fast and Large & Small Scale Fading

๐Ÿ“˜ Overview ๐Ÿ“˜ LARGE SCALE FADING ๐Ÿ“˜ SMALL SCALE FADING ๐Ÿ“˜ SLOW FADING ๐Ÿ“˜ FAST FADING ๐Ÿงฎ MATLAB Codes ๐Ÿ“š Further Reading LARGE SCALE FADING The term 'Large scale fading' is used to describe variations in received signal power over a long distance, usually just considering shadowing.  Assume that a transmitter (say, a cell tower) and a receiver  (say, your smartphone) are in constant communication. Take into account the fact that you are in a moving vehicle. An obstacle, such as a tall building, comes between your cell tower and your vehicle's line of sight (LOS) path. Then you'll notice a decline in the power of your received signal on the spectrogram. Large-scale fading is the term for this type of phenomenon. SMALL SCALE FADING  Small scale fading is a term that describes rapid fluctuations in the received signal power on a small time scale. This includes multipath propagation effects as well as movement-induced Doppler fr...

Online Simulator for ASK, FSK, and PSK

Try our new Digital Signal Processing Simulator!   Start Simulator for binary ASK Modulation Message Bits (e.g. 1,0,1,0) Carrier Frequency (Hz) Sampling Frequency (Hz) Run Simulation Simulator for binary FSK Modulation Input Bits (e.g. 1,0,1,0) Freq for '1' (Hz) Freq for '0' (Hz) Sampling Rate (Hz) Visualize FSK Signal Simulator for BPSK Modulation ...

Theoretical BER vs SNR for binary ASK, FSK, and PSK

๐Ÿ“˜ Overview & Theory ๐Ÿงฎ MATLAB Codes ๐Ÿ“š Further Reading Theoretical BER vs SNR for Amplitude Shift Keying (ASK) The theoretical Bit Error Rate (BER) for binary ASK depends on how binary bits are mapped to signal amplitudes. For typical cases: If bits are mapped to 1 and -1, the BER is: BER = Q(√(2 × SNR)) If bits are mapped to 0 and 1, the BER becomes: BER = Q(√(SNR / 2)) Where: Q(x) is the Q-function: Q(x) = 0.5 × erfc(x / √2) SNR : Signal-to-Noise Ratio N₀ : Noise Power Spectral Density Understanding the Q-Function and BER for ASK Bit '0' transmits noise only Bit '1' transmits signal (1 + noise) Receiver decision threshold is 0.5 BER is given by: P b = Q(0.5 / ฯƒ) , where ฯƒ = √(N₀ / 2) Using SNR = (0.5)² / N₀, we get: BER = Q(√(SNR / 2)) Theoretical BER vs ...

DFTs-OFDM vs OFDM: Why DFT-Spread OFDM Reduces PAPR Effectively (with MATLAB Code)

DFT-spread OFDM (DFTs-OFDM) has lower Peak-to-Average Power Ratio (PAPR) because it "spreads" the data in the frequency domain before applying IFFT, making the time-domain signal behave more like a single-carrier signal rather than a multi-carrier one like OFDM. Deeper Explanation: Aspect OFDM DFTs-OFDM Signal Type Multi-carrier Single-carrier-like Process IFFT of QAM directly QAM → DFT → IFFT PAPR Level High (due to many carriers adding up constructively) Low (less fluctuation in amplitude) Why PAPR is High Subcarriers can add in phase, causing spikes DFT "pre-spreads" data, smoothing it Used in Wi-Fi, LTE downlink LTE uplink (as SC-FDMA) In OFDM, all subcarriers can...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...

๐Ÿ“˜ Overview of BER and SNR ๐Ÿงฎ Online Simulator for BER calculation of m-ary QAM and m-ary PSK ๐Ÿงฎ MATLAB Code for BER calculation of M-ary QAM, M-ary PSK, QPSK, BPSK, ... ๐Ÿ“š Further Reading ๐Ÿ“‚ View Other Topics on M-ary QAM, M-ary PSK, QPSK ... ๐Ÿงฎ Online Simulator for Constellation Diagram of m-ary QAM ๐Ÿงฎ Online Simulator for Constellation Diagram of m-ary PSK ๐Ÿงฎ MATLAB Code for BER calculation of ASK, FSK, and PSK ๐Ÿงฎ MATLAB Code for BER calculation of Alamouti Scheme ๐Ÿงฎ Different approaches to calculate BER vs SNR What is Bit Error Rate (BER)? The abbreviation BER stands for Bit Error Rate, which indicates how many corrupted bits are received (after the demodulation process) compared to the total number of bits sent in a communication process. BER = (number of bits received in error) / (total number of tran...

MATLAB Code for ASK, FSK, and PSK

๐Ÿ“˜ Overview & Theory ๐Ÿงฎ MATLAB Code for ASK ๐Ÿงฎ MATLAB Code for FSK ๐Ÿงฎ MATLAB Code for PSK ๐Ÿงฎ Simulator for binary ASK, FSK, and PSK Modulations ๐Ÿ“š Further Reading ASK, FSK & PSK HomePage MATLAB Code MATLAB Code for ASK Modulation and Demodulation % The code is written by SalimWireless.Com % Clear previous data and plots clc; clear all; close all; % Parameters Tb = 1; % Bit duration (s) fc = 10; % Carrier frequency (Hz) N_bits = 10; % Number of bits Fs = 100 * fc; % Sampling frequency (ensure at least 2*fc, more for better representation) Ts = 1/Fs; % Sampling interval samples_per_bit = Fs * Tb; % Number of samples per bit duration % Generate random binary data rng(10); % Set random seed for reproducibility binary_data = randi([0, 1], 1, N_bits); % Generate random binary data (0 or 1) % Initialize arrays for continuous signals t_overall = 0:Ts:(N_bits...

Drone Detection via Low Complexity Zadoff-Chu Sequence Root Estimation

Summary Based on  Yeung, 2025:  Yeung, C.K.A., Lo, B.F. and Torborg, S. Drone detection via low complexity zadoff-chu sequence root estimation. In 2020 IEEE 17th Annual Consumer Communications & Networking Conference (CCNC) (pp. 1-4). IEEE, 2020, January.   The rise in drone usage—from agriculture and delivery to surveillance and racing—has introduced major privacy and security challenges. Modern drones often use OFDM (Orthogonal Frequency Division Multiplexing) with Zadoff-Chu (ZC) sequences for synchronization. While powerful, detecting these sequences blindly (without knowing their parameters) remains a challenge. Aim This article presents a low-complexity solution to blindly detect ZC sequences used by unknown drones. The approach uses a novel double differential method that works without large correlation banks, making it efficient and real-time capable. ZC Sequence Fundamentals A ZC sequence of prime length P and roo...