Centroid
- class torchhd.models.Centroid(in_features: int, out_features: int, device=None, dtype=None, requires_grad=False)[source]
Implements the centroid classification model using class prototypes.
- Parameters:
in_features (int) – Size of each input sample.
out_features (int) – Size of the output, typically the number of classes.
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().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
Examples:
>>> m = Centroid(20, 30) >>> input = torch.randn(128, 20) >>> output = m(input) >>> output.size() torch.Size([128, 30])
- add(input: Tensor, target: Tensor, lr: float = 1.0) None[source]
Adds the input vectors scaled by the lr to the target prototype vectors.
- add_adapt(input: Tensor, target: Tensor, lr: float = 1.0) None[source]
Only updates the prototype vectors on wrongly predicted inputs.
Implements the iterative training method as described in AdaptHD: Adaptive Efficient Training for Brain-Inspired Hyperdimensional Computing.
Subtracts the input from the mispredicted class prototype scaled by the learning rate and adds the input to the target prototype scaled by the learning rate.
- add_online(input: Tensor, target: Tensor, lr: float = 1.0) None[source]
Only updates the prototype vectors on wrongly predicted inputs.
Implements the iterative training method as described in OnlineHD: Robust, Efficient, and Single-Pass Online Learning Using Hyperdimensional System.
Adds the input to the mispredicted class prototype scaled by \(\epsilon - 1\) and adds the input to the target prototype scaled by \(1 - \delta\), where \(\epsilon\) is the cosine similarity of the input with the mispredicted class prototype and \(\delta\) is the cosine similarity of the input with the target class prototype.
- extra_repr() str[source]
Return the extra representation of the module.
To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.
- forward(input: Tensor, dot: bool = False) Tensor[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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.