vq#

class diffsptk.VectorQuantization(order: int, codebook_size: int, **kwargs)[source]#

See this page for details.

Parameters:
orderint >= 0

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

codebook_sizeint >= 1

The codebook size, \(K\).

**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, codebook: Tensor | None = None, **kwargs) tuple[Tensor, Tensor, Tensor][source]#

Perform vector quantization.

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

The input vectors.

codebookTensor [shape=(K, M+1)]

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=(…,)]

The codebook indices.

lossTensor [scalar]

The commitment loss.

Examples

>>> x = diffsptk.nrand(4)
>>> x
tensor([ 0.7947,  0.1007,  1.2290, -0.5019,  1.5552])
>>> vq = diffsptk.VectorQuantization(4, 2).eval()
>>> xq, _, _ = vq(x)
>>> xq
tensor([0.3620, 0.2736, 0.7098, 0.7106, 0.6494]

See also

ivq lbg