Inclusive vs Exclusive Centroids in GE2E: Speaker Verification Loss Explained

Inclusive centroids compute the mean of all utterance embeddings for a speaker to enable stable inter-speaker comparisons, while exclusive centroids use a leave-one-out mean to prevent self-bias during intra-speaker comparisons in the GE2E loss function.

The Generalized End-to-End (GE2E) loss drives speaker verification in the Real-Time-Voice-Cloning repository by learning discriminative voice embeddings that cluster same-speaker utterances while separating different speakers. At the core of this implementation lies the critical distinction between inclusive and exclusive centroids in GE2E, which ensures that similarity scores reflect genuine speaker characteristics rather than trivial self-similarity artifacts.

What Are Inclusive and Exclusive Centroids in GE2E?

In the GE2E framework implemented in Real-Time-Voice-Cloning, a centroid represents the average embedding vector of a speaker's utterances. The loss function calculates similarities between individual utterance embeddings and these centroids to optimize the embedding space. The repository computes two distinct types of centroids within the SpeakerEncoder.similarity_matrix method in encoder/model.py (lines 74-92):

  • Inclusive centroids include every utterance from the speaker when computing the average
  • Exclusive centroids exclude the specific utterance currently being evaluated (leave-one-out mean)

This dual approach ensures fair comparisons both across different speakers and within the same speaker's utterance set.

Inclusive Centroids for Inter-Speaker Comparisons

Inclusive centroids serve as the reference point when comparing utterances against speakers different from their own origin.

According to the source code in encoder/model.py, the inclusive centroid is computed as:

centroids_incl = torch.mean(embeds, dim=1, keepdim=True)
centroids_incl = F.normalize(centroids_incl, dim=2)

Where embeds has shape (speakers, utterances, dimensions). This calculates the arithmetic mean across all utterances for each speaker, followed by L2 normalization.

The inclusive centroid is used exclusively for inter-speaker comparisons—when an utterance from speaker i is compared against the centroid of speaker j where j ≠ i. Because the centroid contains all utterances from speaker j, it provides a stable, unbiased reference that remains constant regardless of which specific utterance from speaker i is being evaluated.

Exclusive Centroids for Intra-Speaker Comparisons

Exclusive centroids eliminate self-similarity bias when comparing an utterance to its own speaker's centroid.

The exclusive centroid employs a leave-one-out calculation to remove the current utterance from the average:

sum_embeds = torch.sum(embeds, dim=1, keepdim=True)
centroids_excl = (sum_embeds - embeds) / (utterances_per_speaker - 1)
centroids_excl = F.normalize(centroids_excl, dim=2)

This subtracts the current utterance embedding from the sum before averaging, ensuring the centroid represents the speaker's voice characteristics using only the other utterances from that speaker. The result is L2-normalized to maintain consistent cosine similarity calculations.

Exclusive centroids are used strictly for intra-speaker comparisons—when an utterance is compared against its own speaker's centroid. Without this exclusion, the model could achieve artificially high similarity scores by detecting its own presence in the centroid, effectively preventing the learning of meaningful discriminative features.

Code Implementation in Real-Time-Voice-Cloning

The SpeakerEncoder class in encoder/model.py constructs both centroid types within the similarity_matrix method. The implementation builds a similarity matrix where off-diagonal blocks (different speakers) use inclusive centroids, while diagonal blocks (same speaker) use exclusive centroids.

The following minimal implementation mirrors the logic found in the repository:

import torch
import torch.nn.functional as F

def inclusive_centroids(embeds):
    """Compute mean of all utterances per speaker."""
    cent = torch.mean(embeds, dim=1, keepdim=True)  # (S, 1, D)

    return F.normalize(cent, dim=2)

def exclusive_centroids(embeds):
    """Compute leave-one-out centroids."""
    S, U, D = embeds.shape
    sum_ = torch.sum(embeds, dim=1, keepdim=True)   # (S, 1, D)

    excl = (sum_ - embeds) / (U - 1)                # (S, U, D)

    return F.normalize(excl, dim=2)

def similarity_matrix(embeds, w=10.0, b=-5.0):
    """
    Compute GE2E similarity matrix using appropriate centroids.
    w and b correspond to similarity_weight and similarity_bias.
    """
    S, U, D = embeds.shape
    incl = inclusive_centroids(embeds)  # (S, 1, D)

    excl = exclusive_centroids(embeds)  # (S, U, D)

    
    sim = torch.zeros(S, U, S)
    
    # Inter-speaker: compare utterances with other speakers' inclusive centroids

    for j in range(S):
        mask = [i for i in range(S) if i != j]
        sim[mask, :, j] = (embeds[mask] * incl[j]).sum(dim=2)
    
    # Intra-speaker: compare utterances with own exclusive centroid

    for j in range(S):
        sim[j, :, j] = (embeds[j] * excl[j]).sum(dim=1)
    
    return sim * w + b

The resulting similarity matrix is scaled by learned parameters similarity_weight and similarity_bias (defined in the model initialization and configurable via encoder/params_model.py) before being fed to the softmax cross-entropy loss.

Key Files in the Repository

Several files work together to implement the centroid calculations and GE2E loss:

  • encoder/model.py – Contains the core SpeakerEncoder class with the similarity_matrix method (lines 74-92) that computes both inclusive and exclusive centroids
  • encoder/params_model.py – Defines hyperparameters including embedding size and LSTM dimensions that affect centroid dimensions
  • utils/default_models.py – Provides pre-trained encoders containing learned similarity_weight and similarity_bias values optimized for the centroid scaling operation

Why the Distinction Matters for Training

The separation between inclusive and exclusive centroids in GE2E serves distinct optimization purposes that prevent training collapse:

Inclusive centroids provide a consistent, stable reference for negative samples (different speakers). Since all utterances from the comparison speaker are included, the centroid accurately represents that speaker's complete embedding space, allowing fair evaluation of how distant the current utterance lies from other speakers.

Exclusive centroids enforce hard positive mining for same-speaker comparisons. By removing the target utterance from its own centroid, the model must learn embeddings that align with the speaker's other utterances rather than relying on self-similarity. This prevents the network from collapsing to trivial solutions where every utterance achieves perfect similarity with a centroid containing itself.

The final scaled similarity matrix drives the GE2E loss to effectively separate speakers in the embedding space while maintaining tight, discriminative clusters for individual speakers.

Summary

  • Inclusive centroids calculate the mean of all utterances for a speaker and are used when comparing utterances against different speakers in the GE2E loss
  • Exclusive centroids use a leave-one-out mean (excluding the current utterance) and are used when comparing utterances against their own speaker to prevent self-bias
  • The implementation resides in encoder/model.py within the SpeakerEncoder.similarity_matrix method (lines 74-92) of the Real-Time-Voice-Cloning repository
  • Inclusive centroids provide stable inter-speaker references, while exclusive centroids force the model to learn genuinely discriminative features rather than exploiting self-similarity
  • Learned scaling parameters similarity_weight and similarity_bias adjust the final similarity matrix before loss computation

Frequently Asked Questions

What is the GE2E loss function?

The Generalized End-to-End (GE2E) loss is a metric learning approach for speaker verification that optimizes embedding spaces by comparing utterances against speaker centroids. Unlike traditional pairwise losses, GE2E processes entire batches of utterances from multiple speakers simultaneously, computing similarities between each utterance and both its own speaker's centroid (using exclusive calculation) and other speakers' centroids (using inclusive calculation) to maximize inter-speaker distance while minimizing intra-speaker variance.

Why can't inclusive centroids be used for intra-speaker comparisons?

Using inclusive centroids for intra-speaker comparisons would allow the model to achieve near-perfect similarity scores by including the target utterance in its own centroid calculation. This self-similarity bias would prevent the network from learning meaningful discriminative features, as the model could achieve minimal loss simply by recognizing its own presence in the centroid rather than learning to match the broader speaker characteristics represented by the other utterances from that speaker.

Where are the similarity weights and biases defined?

The similarity_weight and similarity_bias parameters are learned during training and are defined as nn.Parameter tensors in the SpeakerEncoder class within encoder/model.py. These scaling factors adjust the sharpness of the similarity distribution before applying softmax, allowing the network to learn optimal decision boundaries between speakers. Pre-trained values for these parameters are available through utils/default_models.py when loading official checkpoints.

How does the leave-one-out calculation affect training stability?

The leave-one-out calculation in exclusive centroids ensures that the gradient updates depend on how well an utterance matches the other utterances from the same speaker, not on how well it matches itself. This forces the network to extract consistent speaker characteristics across different utterances rather than overfitting to specific utterance artifacts. During early training when embeddings are random, this prevents the model from settling into a local minimum where each utterance simply maps to a distinct region of the embedding space.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →