SparseHD
- class torchhd.classifiers.SparseHD(n_features: int, n_dimensions: int, n_classes: int, *, n_levels: int = 100, min_level: int = -1, max_level: int = 1, epochs: int = 120, lr: float = 0.035, sparsity: float = 0.1, sparsity_type: Literal['dimension', 'class'] = 'dimension', device: device | None = None, dtype: dtype | None = None)[source]
Implements SparseHD: Algorithm-Hardware Co-optimization for Efficient High-Dimensional Computing.
- Parameters:
n_features (int) – Size of each input sample.
n_dimensions (int) – The number of hidden dimensions to use.
n_classes (int) – The number of classes.
n_levels (int, optional) – The number of discretized levels for the level-hypervectors.
min_level (int, optional) – The lower-bound of the range represented by the level-hypervectors.
max_level (int, optional) – The upper-bound of the range represented by the level-hypervectors.
epochs (int, optional) – The number of iteration over the training data.
lr (float, optional) – The learning rate.
sparsity (float, optional) – The fraction of weights to be zero.
sparsity_type (str, optional) – The way in which to apply the sparsity, per hidden dimension, or per class.
device (
torch.device, optional) – the desired device of the weights. Default: ifNone, uses the current device for the default tensor type (seetorch.set_default_tensor_type()).devicewill be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.dtype (
torch.dtype, optional) – the desired data type of the weights. Default: ifNone, usestorch.get_default_dtype().
- __call__(samples: Tensor) Tensor
Evaluate the logits of the classifier for the given samples.
- Parameters:
samples (Tensor) – Batch of samples to be classified.
- Returns:
Logits of each sample for each class.
- Return type:
Tensor
- accuracy(data_loader: Iterable[Tuple[Tensor, LongTensor]]) float
Accuracy in predicting the labels of the samples.
- Parameters:
data_loader (DataLoader) – Iterable of tuples containing a batch of samples and labels.
- Returns:
The accuracy of predicting the true labels.
- Return type:
float
- fit(data_loader: Iterable[Tuple[Tensor, LongTensor]])[source]
Fits the classifier to the provided data.
- Parameters:
data_loader (DataLoader) – Iterable of tuples containing a batch of samples and labels.
- Returns:
self
- predict(samples: Tensor) LongTensor
Predict the class of each given sample.
- Parameters:
samples (Tensor) – Batch of samples to be classified.
- Returns:
Index of the predicted class for each sample.
- Return type:
LongTensor