Module kmeans

Module kmeans 

Source
Expand description

Kmeans clustering API’s

Example:


use hipvs::cluster::kmeans;
use hipvs::{ManagedTensor, Resources, Result};

use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::RandomExt;

fn kmeans_example() -> Result<()> {
    let res = Resources::new()?;

    // Create a new random dataset to index
    let n_datapoints = 65536;
    let n_features = 512;
    let n_clusters = 8;
    let dataset =
        ndarray::Array::<f32, _>::random((n_datapoints, n_features), Uniform::new(0., 1.0));
    let dataset = ManagedTensor::from(&dataset).to_device(&res)?;

    let centroids_host = ndarray::Array::<f32, _>::zeros((n_clusters, n_features));
    let mut centroids = ManagedTensor::from(&centroids_host).to_device(&res)?;

    // find the centroids with the kmeans index
    let kmeans_params = kmeans::Params::new()?.set_n_clusters(n_clusters as i32);
    let (inertia, n_iter) = kmeans::fit(&res, &kmeans_params, &dataset, &None, &mut centroids)?;
    Ok(())
}

Structs§

Params

Functions§

cluster_cost
Compute cluster cost given an input matrix and existing centroids
fit
Find clusters with the k-means algorithm
predict
Predict clusters with the k-means algorithm