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, device: device | None = None)[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.
- devicetorch.device or None
The device of this module.
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
>>> import diffsptk >>> import torch >>> lbg = diffsptk.LBG(1, 2) >>> x = torch.tensor([ ... [-0.5, 0.3], [0.0, 0.7], [0.2, -0.1], [3.4, 2.0], [-2.8, 1.0], ... [2.9, -3.0], [2.2, -2.5], [1.5, -1.6], [1.8, 0.5], [1.3, 0.0], ... ]) >>> codebook, indices, distance = lbg(x, return_indices=True) >>> codebook tensor([[ 1.6250, 0.8000], [ 0.5833, -0.9833]]) >>> indices tensor([1, 0, 1, 0, 1, 1, 1, 1, 0, 0]) >>> distance tensor(4.2804)