Expand description
§bhtsne
bhtsne contains the implementations of both a parallel, exact, version of the t-SNE algorithm
and a parallel, approximate, version leveraging the Barnes-Hut algorithm.
The implementation supports custom data types and custom defined metrics. See tSNE for more
details.
This crate also includes load_csv, a commodity function to parse data, record by record,
from a csv file.
§Example
ⓘ
use bhtsne;
const N: usize = 150; // Number of vectors to embed.
const D: usize = 4; // The dimensionality of the
// original space.
const THETA: f32 = 0.5; // Parameter used by the Barnes-Hut algorithm.
// Small values improve accuracy but increase complexity.
const PERPLEXITY: f32 = 10.0; // Perplexity of the conditional distribution.
const EPOCHS: usize = 2000; // Number of fitting iterations.
// Loads the data from a csv file skipping the first row,
// treating it as headers and skipping the 5th column,
// treating it as a class label.
// Do note that you can also switch to f64s for higher precision.
let data: Vec<f32> = bhtsne::load_csv("iris.csv", true, Some(&[4]), |float| {
float.parse().unwrap()
})?;
let samples: Vec<&[f32]> = data.chunks(D).collect();
// Executes the Barnes-Hut approximation of the algorithm and writes the embedding to the
// specified csv file.
bhtsne::tSNE::<f32, &[f32], 2>::new(&samples)
.perplexity(PERPLEXITY)
.epochs(EPOCHS)
.barnes_hut(THETA, |sample_a, sample_b| {
sample_a
.iter()
.zip(sample_b.iter())
.map(|(a, b)| (a - b).powi(2))
.sum::<f32>()
.sqrt()
})
.write_csv("iris_embedding.csv")?;Structs§
- Dim
- Public re-exports.
Carrier type the per-
DMortonimplementations attach to, since a trait needs a type to dispatch on whileDstays a const generic on the arena. - Neighbor
- A sample’s nearest neighbor for
tSNE::barnes_hut_with_neighbors: its index and the distance (not a similarity) to it. - Sparse
Affinities - A precomputed sparse, symmetric affinity graph in the CSR-like layout
barnes_hutuses internally. Obtain viatSNE::affinitiesand inject withtSNE::with_affinities. - Spectral
Params - Public re-exports.
Tunable parameters of the spectral embedding initialization, accepted by
tSNE::spectral_init_withandtSNE::spectral_embedding_with. The defaults resolve the leading eigenvectors of a 70k point MNIST affinity graph to a fraction of a degree, so tuning is only warranted to trade accuracy for speed or to handle unusually structured graphs. - tSNE
- t-distributed stochastic neighbor embedding. Provides a parallel implementation of both the exact version of the algorithm and the tree accelerated one leveraging space partitioning trees.
Traits§
- FftNum
- Public re-exports. Generic floating point number, implemented for f32 and f64
- Morton
- Public re-exports.
Per-dimension Z-order codec, implemented for
Dim<2>,Dim<3>,Dim<4>,Dim<5>,Dim<6>, andDim<7>. - Spectral
Block - Public re-exports.
Fixed-width row storage of the spectral solver, in the spirit of the
Mortonassociated types: each dimensionality names its concrete[T; D + 8]row type, giving the solver compile-time block widths on stable Rust. The fused kernel unrolls and vectorizes over these rows for about twice the throughput of a dynamic-width loop.
Functions§
- load_
csv - Loads data from a csv file.
Type Aliases§
- Epoch
Callback - Boxed closure invoked at the end of each fitting epoch with the epoch index and a
snapshot of the current embedding. See
tSNE::epoch_callback.