lbg#

diffsptk.LBG#

alias of LindeBuzoGrayAlgorithm

class diffsptk.LindeBuzoGrayAlgorithm(order: int, codebook_size: int, *, min_data_per_cluster: int = 1, n_iter: int = 100, eps: float = 1e-05, perturb_factor: float = 1e-05, init: str = 'mean', metric: str = 'none', batch_size: int | None = None, seed: int | None = None, verbose: bool | int = False)[source]#

See this page for details. Note that the forward method is not differentiable.

Parameters:
orderint >= 0

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

codebook_sizeint >= 1

The target codebook size, \(K\).

min_data_per_clusterint >= 1

The minimum number of data points required in each cluster.

n_iterint >= 1

The number of iterations.

epsfloat >= 0

The convergence threshold.

perturb_factorfloat > 0

The perturbation factor.

init[‘none’, ‘mean’] or torch.Tensor [shape=(1~K, M+1)]

The initialization method for the codebook.

metric[‘none, ‘aic’, ‘bic’]

The metric used for model selection.

batch_sizeint >= 1 or None

The batch size.

seedint or None

The random seed.

verbosebool or int

If 1, shows the distance at each iteration; if 2, shows progress bars.

References

[1]

Y. Linde et al., “An algorithm for vector quantizer design,” IEEE Transactions on Communications, vol. 28, no. 1, pp. 84-95, 1980.

forward(x: Tensor | DataLoader, return_indices: bool = False) list[Tensor][source]#

Design a codebook using the Linde-Buzo-Gray algorithm.

Parameters:
xTensor [shape=(T, M+1)] or DataLoader

The input vectors or a DataLoader that yields the input vectors.

return_indicesbool

If True, return the codebook indices.

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

The generated codebook.

indicesTensor [shape=(T,)] (optional)

The codebook indices.

distanceTensor [scalar]

The distance between the input vectors and the codebook.

Examples

>>> x = diffsptk.nrand(10, 0)
>>> lbg = diffsptk.LBG(0, 2)
>>> codebook, indices, distance = lbg(x, return_indices=True)
>>> codebook
tensor([[-0.5277],
        [ 0.6747]])
>>> indices
tensor([0, 0, 0, 1, 0, 1, 1, 1, 1, 0])
>>> distance
tensor(0.2331)
transform(x: Tensor) tuple[Tensor, Tensor][source]#

Transform the input vectors using the codebook.

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

The input vectors.

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

The quantized vectors.

indicesTensor [shape=(T,)]

The codebook indices.

Examples

>>> lbg = diffsptk.LBG(0, 2)
>>> torch.save(lbg.state_dict(), "lbg.pt")
>>> lbg.load_state_dict(torch.load("lbg.pt"))
>>> x = diffsptk.nrand(10, 0)
>>> xq, indices = lbg.transform(x)

See also

vq ivq gmm