speechbrain.utils.kmeans module

Utilities for training kmeans model.

Author
  • Pooneh Mousavi 2023

Summary

Functions:

accumulate_and_extract_features

Extract features (output of SSL model) and acculamte them on cpu to be used for clustering.

fetch_kmeans_model

Return a k-means clustering model with specified parameters.

save_model

Save a Kmeans model .

train

Train a Kmeans model .

Reference

speechbrain.utils.kmeans.accumulate_and_extract_features(batch, features_list, ssl_model, ssl_layer_num, device)[source]

Extract features (output of SSL model) and acculamte them on cpu to be used for clustering.

Parameters:
  • batch (tensor) – Single batch of data.

  • features_list (list) – accumulate features list.

  • ssl_model – SSL-model used to extract features used for clustering.

  • ssl_layer_num (int) – specify output of which layer of the ssl_model should be used.

  • device – CPU or GPU.

speechbrain.utils.kmeans.fetch_kmeans_model(n_clusters, init, max_iter, batch_size, tol, max_no_improvement, n_init, reassignment_ratio, random_state, checkpoint_path)[source]

Return a k-means clustering model with specified parameters.

Parameters:
  • n_clusters (MiniBatchKMeans) – The number of clusters to form as well as the number of centroids to generate.

  • init (int) – Method for initialization: {‘k-means++’’, ‘’random’’}

  • max_iter (int) – Maximum number of iterations over the complete dataset before stopping independently of any early stopping criterion heuristics.

  • batch_size (int) – Size of the mini batches.

  • tol (float) – Control early stopping based on the relative center changes as measured by a smoothed, variance-normalized of the mean center squared position changes.

  • max_no_improvement (int) – Control early stopping based on the consecutive number of mini batches that does not yield an improvement on the smoothed inertia.

  • n_init (int) – Number of random initializations that are tried

  • reassignment_ratio (float) – Control the fraction of the maximum number of counts for a center to be reassigned.

  • random_state (int) – Determines random number generation for centroid initialization and random reassignment.

  • compute_labels (bool) – Compute label assignment and inertia for the complete dataset once the minibatch optimization has converged in fit.

  • init_size (int) – Number of samples to randomly sample for speeding up the initialization.

  • checkpoint_path (str) – Path to saved model.

Returns:

a k-means clustering model with specified parameters.

Return type:

MiniBatchKMeans

speechbrain.utils.kmeans.train(model, train_set, ssl_model, ssl_layer_num, kmeans_batch_size=1000, device='cpu')[source]

Train a Kmeans model .

Parameters:
  • model (MiniBatchKMeans) – The initial kmeans model for training.

  • train_set (Dataloader) – Batches of tarining data.

  • ssl_model – SSL-model used to extract features used for clustering.

  • ssl_layer_num (int) – Specify output of which layer of the ssl_model should be used.

  • device – CPU or GPU.

  • kmeans_batch_size (int) – Size of the mini batches.

speechbrain.utils.kmeans.save_model(model, checkpoint_path)[source]

Save a Kmeans model .

Parameters:
  • model (MiniBatchKMeans) – The kmeans model to be saved.

  • checkpoint_path (str)) – Path to save the model..