IntRVFL

class torchhd.models.IntRVFL(in_features: int, dimensions: int, out_features: int, kappa: int | None = None, device=None, dtype=None, requires_grad=False)[source]

Class implementing integer random vector functional link network (intRVFL) model as described in Density Encoding Enables Resource-Efficient Randomly Connected Neural Networks.

Parameters:
  • in_features (int) – Size of each input sample.

  • dimensions (int) – The number of hidden dimensions to use.

  • out_features (int) – The number of output features, typically the number of classes.

  • kappa (int, optional) – Parameter of the clipping function limiting the range of values; used as the part of transforming input data.

  • 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().

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Shape:
  • Input: \((*, d)\) where \(*\) means any number of dimensions including none and d = in_features.

  • Output: \((*, n)\) where all but the last dimension are the same shape as the input and n = out_features.

weight

the trainable weights, or class prototypes, of the module of shape \((n, d)\). The values are initialized as all zeros.

Type:

torch.Tensor

fit_ridge_regression(samples: Tensor, labels: Tensor, alpha: float | None = 1) None[source]

Compute the weights (readout matrix) with ridge_regression().

It is a common way to form classifiers wihtin randomized neural networks see, e.g., Randomness in Neural Networks: An Overview.

Parameters:
  • samples (Tensor) – The feature vectors.

  • labels (LongTensor) – The targets vector, typically the class of each sample.

  • alpha (float, optional) – Scalar for the variance of the samples. Default is 1.

Shapes:
  • Samples: \((n, f)\)

  • Labels: \((n, c)\)

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.