Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Speechbrain Speechbrain Xvector Custom Model

From Leeroopedia
Revision as of 16:46, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Speechbrain_Speechbrain_Xvector_Custom_Model.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Sound_Classification, Model_Architecture
Last Updated 2026-02-09 00:00 GMT

Overview

Concrete tool for a custom TDNN-based X-vector model for sound classification provided by the SpeechBrain library.

Description

This module implements a custom Time-Delay Neural Network (TDNN) based X-vector architecture for sound class identification, specifically designed for the UrbanSound8k dataset. The Xvector class builds a stack of configurable TDNN blocks, each consisting of a 1D convolution layer with specified kernel size and dilation factor, followed by an activation function (LeakyReLU by default) and batch normalization. After the TDNN blocks, a statistics pooling layer converts variable-length temporal features into fixed-length vectors by computing mean and standard deviation across time. A final linear layer projects the pooled features to the desired embedding dimension (default 512). The companion Classifier class adds a fully-connected classification head on top of X-vector embeddings, with configurable linear blocks, batch normalization, and a log-softmax output layer.

Usage

Reference this module from a HyperPyYAML configuration file using the !new: tag. Can be replaced with any built-in SpeechBrain model or another custom PyTorch module.

Code Reference

Source Location

Signature

class Xvector(torch.nn.Module):
    """This model extracts X-vectors for sound class recognition.

    Arguments
    ---------
    device : str
    activation : torch class
    tdnn_blocks : int
    tdnn_channels : list of ints
    tdnn_kernel_sizes : list of ints
    tdnn_dilations : list of ints
    lin_neurons : int
    in_channels : int
    """

    def __init__(
        self,
        device="cpu",
        activation=torch.nn.LeakyReLU,
        tdnn_blocks=5,
        tdnn_channels=[512, 512, 512, 512, 1500],
        tdnn_kernel_sizes=[5, 3, 3, 1, 1],
        tdnn_dilations=[1, 2, 3, 1, 1],
        lin_neurons=512,
        in_channels=40,
    ):
        ...

    def forward(self, x, lens=None):
        """Returns the x-vectors."""
        ...

class Classifier(sb.nnet.containers.Sequential):
    """Last MLP on top of xvector features.

    Arguments
    ---------
    input_shape : tuple
    activation : torch class
    lin_blocks : int
    lin_neurons : int
    out_neurons : int
    """

    def __init__(
        self,
        input_shape,
        activation=torch.nn.LeakyReLU,
        lin_blocks=1,
        lin_neurons=512,
        out_neurons=1211,
    ):
        ...

Import

from custom_model import Xvector, Classifier

I/O Contract

Inputs

Name Type Required Description
x torch.Tensor Yes Input features tensor of shape [batch, time, channels]
lens torch.Tensor No Relative lengths of input sequences
in_channels int No Number of input feature channels (default: 40)
tdnn_blocks int No Number of TDNN layers (default: 5)
tdnn_channels list[int] No Output channels per TDNN layer (default: [512, 512, 512, 512, 1500])
tdnn_kernel_sizes list[int] No Kernel size per TDNN layer (default: [5, 3, 3, 1, 1])
tdnn_dilations list[int] No Dilation per TDNN layer (default: [1, 2, 3, 1, 1])

Outputs

Name Type Description
x-vectors torch.Tensor Fixed-length embeddings of shape [batch, 1, lin_neurons]
output torch.Tensor Log-softmax class probabilities from Classifier (shape [batch, 1, out_neurons])

Usage Examples

import torch
from custom_model import Xvector, Classifier

# Create X-vector extractor
compute_xvect = Xvector(in_channels=40, tdnn_blocks=5)

# Extract embeddings from input features
input_feats = torch.rand([5, 10, 40])
xvectors = compute_xvect(input_feats)
print(xvectors.shape)  # torch.Size([5, 1, 512])

# Create classifier on top of embeddings
classify = Classifier(input_shape=xvectors.shape, out_neurons=10)
output = classify(xvectors)
print(output.shape)  # torch.Size([5, 1, 10])

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment