Auraloss is a collection of audio-focused loss functions implemented in PyTorch, designed for tasks like audio synthesis, source separation, and speech enhancement. It provides specialized losses such as Mel-spectrogram, multi-resolution STFT, and perceptual losses. The current stable version is 0.4.0, and new features and improvements are added periodically, with releases typically following significant development milestones.
pip install auralossVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate and use the MultiResolutionSTFTLoss, a common and powerful loss function in auraloss. It generates dummy audio tensors and calculates the loss between them, showcasing the basic API for most loss functions in the library. Ensure PyTorch is installed and CUDA is available for GPU acceleration.
Reshape your tensors to `(B, C, S)` using `tensor.unsqueeze(1)` for mono audio or `tensor.view(B, C, S)` for multi-channel audio where `C` is the channel dimension.
Move all tensors and the loss module to the target device: `input_audio = input_audio.to(device)`, `target_audio = target_audio.to(device)`, `loss_fn = loss_fn.to(device)`.
Ensure your audio tensors are `torch.float32` or `torch.float64`. You can cast them using `tensor.to(dtype=torch.float32)`.
Update your imports: `from auraloss.freq import MultiResolutionSTFTLoss` for the multi-res version, or `from auraloss.freq import MelSpectrogramLoss`. The original `STFTLoss` was superseded by `MultiResolutionSTFTLoss` for improved performance and robustness.
Ensure all tensors and the loss module are moved to the same device: `loss_fn = loss_fn.to(device)`, `input_audio = input_audio.to(device)`, `target_audio = target_audio.to(device)`.
For mono audio, add a channel dimension using `tensor.unsqueeze(1)`: `input_audio = input_audio.unsqueeze(1)`.
You need to import and instantiate a specific loss class first, then call its instance: `from auraloss.freq import MultiResolutionSTFTLoss; mr_loss = MultiResolutionSTFTLoss(); loss = mr_loss(input, target)`.