APNet2GeneratorLoss<T>
Models & Types · AiDotNet.LossFunctions
APNet / APNet2’s generator objective: amplitude-spectrum loss, anti-wrapping phase loss and reconstructed-STFT loss.
Inheritance: LossFunctionBase<T> → APNet2GeneratorLoss<T>
For Beginners
A sound can be described by how loud each frequency is (amplitude) and where each frequency’s wave starts (phase). Phase wraps around like a clock face, so 11 o’clock and 1 o’clock are two hours apart, not ten. This objective measures phase errors the short way round the clock, which is what lets the model learn phase at all.
How It Works
Implements L_G = lambda_A * L_A + lambda_P * L_P + lambda_S * L_S from Ai & Ling 2023
(APNet, arXiv:2305.07952, Eq. 37), carried forward unchanged by Du et al. 2023 (APNet2,
arXiv:2311.11545). The paper’s coefficients are lambda_A = 45, lambda_P = 100 and
lambda_S = 20.
Why this exists. Without it the vocoder trained under a generic squared error, which was
applied to phase directly. Phase is an angle: a prediction of -pi + e against a target of
pi - e is nearly correct but scores as though it were maximally wrong, and the gradient
pushes the prediction the long way round. Avoiding exactly that is the paper’s central
contribution, and the anti-wrapping function below is what does it:
AW(x) = x - 2*pi*round(x / (2*pi)), which maps any angular error into
(-pi, pi] before it is penalised.
What is not included. The paper’s fourth term L_W (mel-spectrogram loss, feature
matching, and MPD/MRD hinge GAN losses) needs a discriminator pair and an adversarial training
loop, which this vocoder has no path for; it is deliberately absent rather than approximated.
The STFT-consistency half of L_S is also absent for a concrete reason: it requires
STFT(ISTFT(S)), and IEngine.STFT returns its magnitude and phase through
out parameters, placing it outside the autodiff graph. The L1 real/imaginary half of
L_S is implemented in full.
Example
using AiDotNet.LossFunctions;
using AiDotNet.Tensors.LinearAlgebra;
var loss = new APNet2GeneratorLoss<float>();
var predicted = new Vector<float>(new[] { 0.9f, 0.2f, 0.7f });
var actual = new Vector<float>(new[] { 1.0f, 0.0f, 1.0f });
float value = loss.CalculateLoss(predicted, actual);
Console.WriteLine($"APNet2GeneratorLoss = {value:F4}");
Constructors
| Constructor | Summary |
|---|---|
APNet2GeneratorLoss(double lambdaAmplitude, double lambdaPhase, double lambdaStft, double lambdaMel, int fftSize, int hopSize, int sampleRate, int melChannels, double phaseEpsilon) |
Creates the generator objective. |
Properties
| Property | Summary |
|---|---|
double LambdaAmplitude { get; } |
Weight of the amplitude-spectrum loss. |
double LambdaMel { get; } |
Weight of the mel-spectrogram loss. |
double LambdaPhase { get; } |
Weight of the anti-wrapping phase loss. |
double LambdaStft { get; } |
Weight of the reconstructed-STFT loss. |
Methods
| Method | Summary |
|---|---|
T CalculateLoss(Vector<T> predicted, Vector<T> actual) |
|
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target) |