NeuralHD

class torchhd.classifiers.NeuralHD(n_features: int, n_dimensions: int, n_classes: int, *, regen_freq: int = 20, regen_rate: float = 0.04, epochs: int = 120, lr: float = 0.37, device: device | None = None, dtype: dtype | None = None)[source]

Implements Scalable edge-based hyperdimensional learning system with brain-like neural adaptation.

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.

  • regen_freq (int, optional) – The frequency in epochs at which to regenerate hidden dimensions.

  • regen_rate (int, optional) – The fraction of hidden dimensions to regenerate.

  • epochs (int, optional) – The number of iteration over the training data.

  • lr (float, optional) – The learning rate.

  • device (torch.device, optional) – the desired device of the weights. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will 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: if None, uses torch.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