Skip to content

Fix wrong quantization example #102

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
Dec 20, 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
32 changes: 22 additions & 10 deletions torchhd/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,9 @@ def negative(input: VSA_Model) -> VSA_Model:
def soft_quantize(input: Tensor):
"""Applies the hyperbolic tanh function to all elements of the input tensor.

.. warning::
This function does not take the VSA model class into account.

Args:
input (Tensor): input tensor.

Expand All @@ -717,12 +720,15 @@ def soft_quantize(input: Tensor):

Examples::

>>> x = functional.random_hv(2, 3)
>>> y = functional.bundle(x[0], x[1])
>>> x = torchhd.random_hv(2, 6)
>>> x
tensor([[ 1., 1., -1., 1., 1., 1.],
[ 1., -1., -1., -1., 1., -1.]])
>>> y = torchhd.bundle(x[0], x[1])
>>> y
tensor([0., 2., 0.])
>>> functional.soft_quantize(y)
tensor([0.0000, 0.9640, 0.0000])
tensor([ 2., 0., -2., 0., 2., 0.])
>>> torchhd.soft_quantize(y)
tensor([ 0.9640, 0.0000, -0.9640, 0.0000, 0.9640, 0.0000])

"""
return torch.tanh(input)
Expand All @@ -731,6 +737,9 @@ def soft_quantize(input: Tensor):
def hard_quantize(input: Tensor):
"""Applies binary quantization to all elements of the input tensor.

.. warning::
This function does not take the VSA model class into account.

Args:
input (Tensor): input tensor

Expand All @@ -740,12 +749,15 @@ def hard_quantize(input: Tensor):

Examples::

>>> x = functional.random_hv(2, 3)
>>> y = functional.bundle(x[0], x[1])
>>> x = torchhd.random_hv(2, 6)
>>> x
tensor([[ 1., 1., -1., 1., 1., 1.],
[ 1., -1., -1., -1., 1., -1.]])
>>> y = torchhd.bundle(x[0], x[1])
>>> y
tensor([ 0., -2., -2.])
>>> functional.hard_quantize(y)
tensor([ 1., -1., -1.])
tensor([ 2., 0., -2., 0., 2., 0.])
>>> torchhd.hard_quantize(y)
tensor([ 1., -1., -1., -1., 1., -1.])

"""
# Make sure that the output tensor has the same dtype and device
Expand Down