Skip to content

Audio, speech, music, and audio-language systems · Text-to-speech and voice generation · AUD024 · Model

VITS

A supervised end-to-end text-to-waveform model combining a conditional variational autoencoder, normalizing flows, adversarial waveform learning, and stochastic duration prediction.

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

A supervised end-to-end text-to-waveform model combining a conditional variational autoencoder, normalizing flows, adversarial waveform learning, and stochastic duration prediction. It jointly models multiple plausible speech realizations instead of relying only on pointwise mel regression.

Question / hypotheses

Can text and optional voice/style context be mapped to intelligible, appropriate speech? This entry defines a modeling or training objective, not an inherent null/alternative significance test. Evaluate any claimed improvement with an independently designed comparison protocol.

Architecture / computational structure

Posterior encoder + text prior/flow → waveform generator; stochastic duration branch. The formula above identifies the central operation; the full network also requires compatible preprocessing, shapes, masking, and output reconstruction.

Assumptions

Adversarial training can be unstable and acoustically convincing output can still mispronounce words. Training-time posterior access must not leak into inference evaluation. Voice and language coverage are checkpoint-specific.

Formula / computational definition

A schematic objective is L=λrLmel+λkLKL+λaLadv+λfLfeature+λdLduration\mathcal L=\lambda_r\mathcal L_{mel}+\lambda_k\mathcal L_{KL}+\lambda_a\mathcal L_{adv}+\lambda_f\mathcal L_{feature}+\lambda_d\mathcal L_{duration}. Posterior latents depend on training audio; the text-conditioned prior and flow support generation at inference.

Parameters and notation

Learned parameters: Posterior/prior/flow, duration, generator and discriminator weights. Hyperparameters and configuration: Configure latent dimension, flow layers, segment length, duration noise, speaker conditioning where supported, and discriminator/loss balance.

Tuning effects: Duration and speaking-rate controls affect timing; sampling/guidance affects diversity and stability. Tune acoustic and vocoder settings separately, then evaluate their combined output.

Fitting, fine-tuning, and adaptation

Balance posterior/prior, duration, generator, and discriminator updates rather than optimizing one loss in isolation. Adapt speaker embeddings or selected modules first; excessive discriminator adaptation can overfit a tiny speaker corpus. Tune only on a held-out development partition; report the frozen/trainable modules, objective, optimizer schedule, stopping rule, and source of initialization.

How to use and calibrate

Use listening quality, transcript consistency, duration variability, speaker similarity, and real-time factor. Examine mode diversity without treating random variation as inherently desirable. Select the operating point on development data and lock it before final evaluation. Most generation scores are not calibrated probabilities; a lower training loss alone does not establish useful output.

Example application

Synthesize multiple plausible readings of the same sentence. Begin with a small held-out diagnostic set, inspect failures, and then expand to the intended recording conditions and data scale.

Interpretation / pitfalls

Watch out

The component code below isolates one mathematical or structural feature. Successful execution demonstrates that calculation, not full-model performance or reproduction. Python: the authors’ jaywalnut310/vits repository is a direct reference. R: reticulate to its trained components or custom torch for a limited educational reconstruction. The example combines representative losses; it is not a complete ELBO or an operational VITS training loop.

Python and R code examples (dependencies/validation)

Scope: deterministic numerical component only; load the shared setup above. Python dependency: NumPy; executed successfully. R dependency: base R; not executed because Rscript was unavailable. Full pretrained model routes are described separately above.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — deterministic numerical component only; load the shared setup above.

Shared setup for this chapter
"""Shared helpers for educational audio objective/architecture components.
This file and all AUD snippets use NumPy only. They do not load trained networks.
"""
import itertools
import numpy as np

def ce(p, y):
    p=np.asarray(p, float); y=np.asarray(y, int)
    return float(-np.log(np.clip(p[np.arange(len(y)),y],1e-12,1)).mean())

def bce(p,y):
    p=np.clip(np.asarray(p,float),1e-12,1-1e-12); y=np.asarray(y,float)
    return float(-(y*np.log(p)+(1-y)*np.log(1-p)).mean())

def mse(a,b): return float(np.mean((np.asarray(a)-np.asarray(b))**2))
def l1(a,b): return float(np.mean(np.abs(np.asarray(a)-np.asarray(b))))
def softmax(a):
    a=np.asarray(a,float); a=a-a.max(axis=-1,keepdims=True)
    z=np.exp(a); return z/z.sum(axis=-1,keepdims=True)
def contrast(a,b,tau=.2):
    a=np.asarray(a,float); b=np.asarray(b,float)
    a=a/np.linalg.norm(a,axis=1,keepdims=True); b=b/np.linalg.norm(b,axis=1,keepdims=True)
    s=a@b.T/tau; y=np.arange(len(a))
    return (ce(softmax(s),y)+ce(softmax(s.T),y))/2

def collapse(path):
    merged=[v for i,v in enumerate(path) if i==0 or v!=path[i-1]]
    return tuple(v for v in merged if v!=0)
def ctc_prob(p,target):
    p=np.asarray(p,float)
    return sum(np.prod([p[t,j] for t,j in enumerate(path)])
               for path in itertools.product(range(p.shape[1]), repeat=len(p))
               if collapse(path)==tuple(target))
def pit_mse(estimate,reference):
    estimate=np.asarray(estimate); reference=np.asarray(reference)
    return min(mse(estimate,reference[list(p)]) for p in itertools.permutations(range(len(reference))))
def cosine(a,b):
    a=np.asarray(a,float); b=np.asarray(b,float)
    return float(a@b/(np.linalg.norm(a)*np.linalg.norm(b)))

# Shared deterministic fixtures; token labels are zero-based in Python.
P=np.array([[.1,.7,.2],[.2,.2,.6],[.6,.2,.2]])
y=np.array([1,2,0])
A=np.array([[1.,0.],[0.,1.],[1.,1.]])
B=np.array([[.9,.1],[.1,.9],[.8,1.]])
s=np.sin(np.arange(32)*.4)
x=s+.1*np.cos(np.arange(32)*.9)
# AUD024: educational component, not the full neural model
losses=np.array([mse(A,B),.2,.3,.1,.4]); weights=np.array([1.,.1,1.,2.,.5])
result=float(losses@weights) # illustrative, not full VITS loss
print(result)
R

Component demo, not a trained model — deterministic numerical component only; load the shared setup above.

Shared setup for this chapter
# Shared educational helpers. These do not load trained neural networks.
# Base R only; labels/indexes are one-based, with blank token 1 for CTC.
ce <- function(p,y) -mean(log(pmax(pmin(p[cbind(seq_along(y),y)],1),1e-12)))
bce <- function(p,y) {
 p <- pmax(pmin(p,1-1e-12),1e-12)
 -mean(y*log(p)+(1-y)*log(1-p))
}
mse <- function(a,b) mean((a-b)^2)
l1 <- function(a,b) mean(abs(a-b))
softmax <- function(a) {
 if (is.null(dim(a))) a <- matrix(a,nrow=1)
 z <- exp(a-apply(a,1,max)); z/rowSums(z)
}
contrast <- function(a,b,tau=.2) {
 a <- a/sqrt(rowSums(a*a)); b <- b/sqrt(rowSums(b*b))
 z <- a %*% t(b)/tau; y <- seq_len(nrow(a))
 (ce(softmax(z),y)+ce(softmax(t(z)),y))/2
}
collapse <- function(path) {
 merged <- path[c(TRUE,diff(path)!=0)]
 merged[merged!=1]
}
ctc_prob <- function(p,target) {
 paths <- expand.grid(rep(list(seq_len(ncol(p))),nrow(p)))
 total <- 0
 for (i in seq_len(nrow(paths))) {
  path <- as.integer(unlist(paths[i,],use.names=FALSE))
  if (identical(collapse(path),as.integer(target))) {
   total <- total+prod(p[cbind(seq_len(nrow(p)),path)])
  }
 }
 total
}
pit_mse <- function(estimate,reference) {
 stopifnot(nrow(reference)==2) # This teaching helper handles exactly two sources.
 min(mse(estimate,reference),mse(estimate,reference[2:1,,drop=FALSE]))
}
cosine <- function(a,b) sum(a*b)/sqrt(sum(a*a)*sum(b*b))
P <- matrix(c(.1,.7,.2,.2,.2,.6,.6,.2,.2),nrow=3,byrow=TRUE)
y <- c(2L,3L,1L)
A <- matrix(c(1,0,0,1,1,1),nrow=3,byrow=TRUE)
B <- matrix(c(.9,.1,.1,.9,.8,1),nrow=3,byrow=TRUE)
s <- sin((0:31)*.4)
x <- s+.1*cos((0:31)*.9)
# AUD024: educational component; requires shared R setup
losses <- c(mse(A,B),.2,.3,.1,.4); weights <- c(1,.1,1,2,.5)
result <- sum(losses*weights)
print(result)

Sources

From the CodeSplash reference, entry AUD024.