Classifier

class torchhd.classifiers.Classifier(n_features: int, n_dimensions: int, n_classes: int, *, device: device | None = None, dtype: dtype | None = None)[source]

Base class for all classifiers

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.

  • 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[source]

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[source]

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[source]

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