Skip to content

Add embeddings documentation #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions torchhd/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Identity(nn.Embedding):
[1., 1., 1.]])

"""

def __init__(self, num_embeddings, embedding_dim, requires_grad=False, **kwargs):
super(Identity, self).__init__(num_embeddings, embedding_dim, **kwargs)
self.weight.requires_grad = requires_grad
Expand Down Expand Up @@ -74,8 +73,13 @@ class Random(nn.Embedding):
[ 1., 1., 1.]])

"""

def __init__(self, num_embeddings, embedding_dim, requires_grad=False, **kwargs):
def __init__(
self,
num_embeddings,
embedding_dim,
requires_grad=False,
**kwargs
):
super(Random, self).__init__(num_embeddings, embedding_dim, **kwargs)
self.weight.requires_grad = requires_grad

Expand Down Expand Up @@ -117,7 +121,6 @@ class Level(nn.Embedding):
[ 1., -1., 1., -1., -1., -1., 1., 1., -1., 1.]])

"""

def __init__(
self,
num_embeddings,
Expand Down Expand Up @@ -185,7 +188,6 @@ class Circular(nn.Embedding):
[ 1., -1., -1., -1., 1., 1., 1., 1., 1., -1.]])

"""

def __init__(
self,
num_embeddings,
Expand Down Expand Up @@ -233,9 +235,9 @@ def forward(self, input: torch.Tensor) -> torch.Tensor:

class Projection(nn.Module):
r"""Embedding using a random projection matrix.

Implemented based on `A Theoretical Perspective on Hyperdimensional Computing <https://arxiv.org/abs/2010.07426>`_.
:math:`\Phi x` where :math:`\Phi \in \mathbb{R}^{d \times m}` is a matrix whose rows are uniformly sampled at random from the surface of an :math:`m`-dimensional unit sphere.
:math:`\Phi x` where :math:`\Phi \in \mathbb{R}^{d \times m}` is a matrix whose rows are uniformly sampled at random from the surface of an :math:`m`-dimensional unit sphere.
This encoding ensures that similarities in the input space are preserved in the hyperspace.

Args:
Expand Down