msvq#

class diffsptk.MultiStageVectorQuantization(order: int, codebook_size: int, n_stage: int, device: device | None = None, **kwargs)[source]#

See this page for details.

Parameters:
orderint >= 0

The order of the input vector, \(M\).

codebook_sizeint >= 1

The codebook size, \(K\).

n_stageint >= 1

The number of stages (quantizers), \(Q\).

devicetorch.device or None

The device of this module.

**kwargsadditional keyword arguments

See this page for details.

References

[1]

A. v. d. Oord et al., “Neural discrete representation learning,” Advances in Neural Information Processing Systems, pp. 6309-6318, 2017.

forward(x: Tensor, codebooks: Tensor | None = None, **kwargs) tuple[Tensor, Tensor, Tensor][source]#

Perform residual vector quantization.

Parameters:
xTensor [shape=(…, M+1)]

The input vectors.

codebooksTensor [shape=(Q, K, M+1)] or None

The external codebook. If None, use the internal codebook.

**kwargsadditional keyword arguments

See this page for details.

Returns:
xqTensor [shape=(…, M+1)]

The quantized vectors.

indicesTensor [shape=(…, Q)]

The codebook indices.

lossesTensor [shape=(Q,)]

The commitment losses.

Examples

>>> import diffsptk
>>> msvq = diffsptk.MultiStageVectorQuantization(4, 3, 2).eval()
>>> x = diffsptk.nrand(4)
>>> x.shape
torch.Size([5])
>>> xq, indices, losses = msvq(x)
>>> xq.shape
torch.Size([5])
>>> indices.shape
torch.Size([2])
>>> losses.shape
torch.Size([2])

See also

vq imsvq