lbg#
- diffsptk.LBG#
alias of
LindeBuzoGrayAlgorithm
- class diffsptk.LindeBuzoGrayAlgorithm(order, codebook_size, *, min_data_per_cluster=1, n_iter=100, eps=1e-05, perturb_factor=1e-05, init='mean', metric='none', batch_size=None, seed=None, verbose=False)[source]#
See this page for details. Note that the forward method is not differentiable.
- Parameters:
- orderint >= 0
Order of vector, \(M\).
- codebook_sizeint >= 1
Target codebook size, \(K\).
- min_data_per_clusterint >= 1
Minimum number of data points in a cluster.
- n_iterint >= 1
Number of iterations.
- epsfloat >= 0
Convergence threshold.
- perturb_factorfloat > 0
Perturbation factor.
- init[‘none’, ‘mean’] or torch.Tensor [shape=(1~K, M+1)]
Initialization type.
- metric[‘none, ‘aic’, ‘bic’]
Metric used as a reference for model selection.
- batch_sizeint >= 1 or None
Batch size.
- seedint or None
Random seed.
- verbosebool or int
If 1, show distance at each iteration; if 2, show progress bar.
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, return_indices=False)[source]#
Design a codebook.
- Parameters:
- xTensor [shape=(T, M+1)] or DataLoader
Input vectors or dataloder yielding input vectors.
- return_indicesbool
If True, return indices.
- Returns:
- codebookTensor [shape=(K, M+1)]
Codebook.
- indicesTensor [shape=(T,)] (optional)
Codebook indices.
- distanceTensor [scalar]
Distance.
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)[source]#
Transform input vectors using the codebook.
- Parameters:
- xTensor [shape=(T, M+1)]
Input vectors.
- Returns:
- xqTensor [shape=(T, M+1)]
Quantized vectors.
- indicesTensor [shape=(T,)]
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)