Registry / ai-ml / auraloss

auraloss

JSON →
library0.4.0pypypi✓ verified 85d ago

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 auraloss
INSTALL
IMPORT
SIG · AURALOSS
A
auraloss
ai-mlpythonv0.4.0
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
2/4 runs
py 3.11
✕ build_error
2/4 runs
py 3.12
✕ build_error
2/4 runs
py 3.13
✕ build_error
2/4 runs
py 3.9
✕ build_error
✕ timeout
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MultiResolutionSTFTLoss
from auraloss.freq import MultiResolutionSTFTLoss
MelSpectrogramLoss
from auraloss.freq import MelSpectrogramLoss
PerceptualLoss
from auraloss.perceptual import PerceptualLoss
SpectralConvergenceLoss
from auraloss.freq import SpectralConvergenceLoss

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.

import torch from auraloss.freq import MultiResolutionSTFTLoss device = 'cuda' if torch.cuda.is_available() else 'cpu' # Dummy input/target tensors (e.g., 10 seconds of mono audio at 16kHz) # Batch size B, Channels C, Samples S input_audio = torch.randn(2, 1, 160000, device=device) target_audio = torch.randn(2, 1, 160000, device=device) # Initialize Multi-Resolution STFT Loss # The paper recommends a set of default parameters for MR-STFT Loss # consisting of 3 STFT magnitudes, with varying window sizes and hop sizes. # auraloss.freq.MultiResolutionSTFTLoss provides these defaults. mr_stft_loss = MultiResolutionSTFTLoss().to(device) # Compute the loss loss = mr_stft_loss(input_audio, target_audio) print(f"Computed MR-STFT Loss: {loss.item()}")
Debug
Known issues
gotchaAll auraloss functions expect input and target tensors to be 3-dimensional (Batch, Channels, Samples). A common mistake is to pass 2D (Batch, Samples) or 1D (Samples) tensors.
fix
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.
affects: >=0.1.0
gotchaEnsure input and target tensors are on the same device (CPU/GPU) as the loss function instance. Mismatched devices will lead to `RuntimeError: Expected all tensors to be on the same device`.
fix
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)`.
affects: >=0.1.0
gotchaThe STFT-based losses (e.g., MultiResolutionSTFTLoss, MelSpectrogramLoss) rely on `torchaudio`'s STFT implementation, which might have specific requirements for tensor dtypes (typically `torch.float32` or `torch.float64`). Using other dtypes like `torch.float16` might cause issues or unexpected behavior.
fix
Ensure your audio tensors are `torch.float32` or `torch.float64`. You can cast them using `tensor.to(dtype=torch.float32)`.
affects: >=0.1.0
breakingPrior to v0.3.0, some loss functions like `STFTLoss` and `MelSTFTLoss` were directly in `auraloss.loss.STFTLoss` or `auraloss.loss.MelSTFTLoss`. They were later refactored into `auraloss.freq` and renamed.
fix
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.
affects: <0.3.0
Errors
Common errors & fixes
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0!
Input audio tensors or target audio tensors are on a different device (CPU/GPU) than the initialized auraloss module.
fix
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)`.
ValueError: Expected input to be a 3D tensor, got 2D tensor
auraloss functions expect input audio to be in (Batch, Channels, Samples) format, but received a 2D tensor (e.g., Batch, Samples).
fix
For mono audio, add a channel dimension using `tensor.unsqueeze(1)`: `input_audio = input_audio.unsqueeze(1)`.
TypeError: 'module' object is not callable
Attempting to call the auraloss module directly (e.g., `auraloss.freq(input, target)`) instead of an instantiated loss class.
fix
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)`.
Upgrade
Version history
0.4.0latest on PyPI · released Apr 21, 2023
Audit
Dependencies
torchrequiredCore deep learning framework
torchaudiorequiredAudio processing utilities, particularly for STFT/Mel-spectrogram computations
Agent activity
40 hits · last 30 days
node
34
OpenAI (training)
1
Resources
auraloss — pip install auraloss · libregistry