Skip to content

Fix sequence replace and from_tensor #61

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 1 commit into from
May 24, 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
74 changes: 28 additions & 46 deletions torchhd/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,10 @@ class Multiset:

@overload
def __init__(self, dimensions: int, *, device=None, dtype=None):
# """Creates an empty multiset of dimensions

# Args:
# dim (int): number of dimensions of the multiset.
# Examples::

# >>> M = structures.Multiset(dimensions=10000)
# """
...

@overload
def __init__(self, input: Tensor, *, size=0):
# """Creates an empty multiset of dimensions

# Args:
# input (Tensor): tensor representing a multiset.
# Examples::

# >>> letters = list(string.ascii_lowercase)
# >>> letters_hv = functional.random_hv(len(letters), 10000)
# >>> M = structures.Multiset(input = letters_hv[0])
# """
...

def __init__(self, dim_or_input: Any, **kwargs):
Expand Down Expand Up @@ -467,15 +449,15 @@ class Sequence:

Args:
input (Tensor): tensor representing a sequence.
length (int, optional): the length of the sequence provided as input. Default: ``0``.
size (int, optional): the length of the sequence provided as input. Default: ``0``.

Examples::

>>> S = structures.Sequence(10000)

>>> letters = list(string.ascii_lowercase)
>>> letters_hv = functional.random_hv(len(letters), 10000)
>>> S = structures.Sequence(letters_hv[0], length=1)
>>> S = structures.Sequence(letters_hv[0], size=1)

"""

Expand All @@ -484,11 +466,11 @@ def __init__(self, dimensions: int, *, device=None, dtype=None):
...

@overload
def __init__(self, input: Tensor, *, length=0):
def __init__(self, input: Tensor, *, size=0):
...

def __init__(self, dim_or_input: int, **kwargs):
self.length = kwargs.get("length", 0)
self.size = kwargs.get("size", 0)
if torch.is_tensor(dim_or_input):
self.value = dim_or_input
else:
Expand All @@ -511,7 +493,7 @@ def append(self, input: Tensor) -> None:
"""
rotated_value = functional.permute(self.value, shifts=1)
self.value = functional.bundle(input, rotated_value)
self.length += 1
self.size += 1

def appendleft(self, input: Tensor) -> None:
"""Appends the input tensor to the left of the sequence.
Expand All @@ -526,7 +508,7 @@ def appendleft(self, input: Tensor) -> None:
"""
rotated_input = functional.permute(input, shifts=len(self))
self.value = functional.bundle(self.value, rotated_input)
self.length += 1
self.size += 1

def pop(self, input: Tensor) -> None:
"""Pops the input tensor from the right of the sequence.
Expand All @@ -539,7 +521,7 @@ def pop(self, input: Tensor) -> None:
>>> S.pop(letters_hv[0])

"""
self.length -= 1
self.size -= 1
self.value = functional.bundle(self.value, -input)
self.value = functional.permute(self.value, shifts=-1)

Expand All @@ -554,7 +536,7 @@ def popleft(self, input: Tensor) -> None:
>>> S.popleft(letters_hv[1])

"""
self.length -= 1
self.size -= 1
rotated_input = functional.permute(input, shifts=len(self))
self.value = functional.bundle(self.value, -rotated_input)

Expand All @@ -571,10 +553,10 @@ def replace(self, index: int, old: Tensor, new: Tensor) -> None:
>>> S.replace(0, letters_hv[0], letters_hv[1])

"""
rotated_old = functional.permute(old, shifts=-self.length + index + 1)
rotated_old = functional.permute(old, shifts=self.size - index - 1)
self.value = functional.bundle(self.value, -rotated_old)

rotated_new = functional.permute(new, shifts=-self.length + index + 1)
rotated_new = functional.permute(new, shifts=self.size - index - 1)
self.value = functional.bundle(self.value, rotated_new)

def concat(self, seq: "Sequence") -> "Sequence":
Expand All @@ -591,7 +573,7 @@ def concat(self, seq: "Sequence") -> "Sequence":
"""
value = functional.permute(self.value, shifts=len(seq))
value = functional.bundle(value, seq.value)
return Sequence(value, length=len(self) + len(seq))
return Sequence(value, size=len(self) + len(seq))

def __getitem__(self, index: int) -> Tensor:
"""Gets the approximate value from given index.
Expand All @@ -605,7 +587,7 @@ def __getitem__(self, index: int) -> Tensor:
tensor([ 1., -1., 1., ..., -1., 1., -1.])

"""
return functional.permute(self.value, shifts=-self.length + index + 1)
return functional.permute(self.value, shifts=-self.size + index + 1)

def __len__(self) -> int:
"""Returns the length of the sequence.
Expand All @@ -616,7 +598,7 @@ def __len__(self) -> int:
0

"""
return self.length
return self.size

def clear(self) -> None:
"""Empties the sequence.
Expand All @@ -627,7 +609,7 @@ def clear(self) -> None:

"""
self.value.fill_(0.0)
self.length = 0
self.size = 0

@classmethod
def from_tensor(cls, input: Tensor):
Expand Down Expand Up @@ -659,7 +641,7 @@ class DistinctSequence:

Args:
input (Tensor): tensor representing a distinct sequence.
length (int, optional): the size of the distinct sequence provided as input. Default: ``0``.
size (int, optional): the length of the distinct sequence provided as input. Default: ``0``.

Examples::

Expand All @@ -671,11 +653,11 @@ def __init__(self, dimensions: int, *, device=None, dtype=None):
...

@overload
def __init__(self, input: Tensor, *, length=0):
def __init__(self, input: Tensor, *, size=0):
...

def __init__(self, dim_or_input: int, **kwargs):
self.length = kwargs.get("length", 0)
self.size = kwargs.get("size", 0)
if torch.is_tensor(dim_or_input):
self.value = dim_or_input
else:
Expand All @@ -698,7 +680,7 @@ def append(self, input: Tensor) -> None:
"""
rotated_value = functional.permute(self.value, shifts=1)
self.value = functional.bind(input, rotated_value)
self.length += 1
self.size += 1

def appendleft(self, input: Tensor) -> None:
"""Appends the input tensor to the left of the sequence.
Expand All @@ -713,7 +695,7 @@ def appendleft(self, input: Tensor) -> None:
"""
rotated_input = functional.permute(input, shifts=len(self))
self.value = functional.bind(self.value, rotated_input)
self.length += 1
self.size += 1

def pop(self, input: Tensor) -> None:
"""Pops the input tensor from the right of the sequence.
Expand All @@ -726,7 +708,7 @@ def pop(self, input: Tensor) -> None:
>>> DS.pop(letters_hv[0])

"""
self.length -= 1
self.size -= 1
self.value = functional.bind(self.value, input)
self.value = functional.permute(self.value, shifts=-1)

Expand All @@ -741,7 +723,7 @@ def popleft(self, input: Tensor) -> None:
>>> DS.popleft(letters_hv[1])

"""
self.length -= 1
self.size -= 1
rotated_input = functional.permute(input, shifts=len(self))
self.value = functional.bind(self.value, rotated_input)

Expand All @@ -759,22 +741,22 @@ def replace(self, index: int, old: Tensor, new: Tensor) -> None:
>>> DS.concat(DS1)

"""
rotated_old = functional.permute(old, shifts=-self.length + index + 1)
rotated_old = functional.permute(old, shifts=self.size - index - 1)
self.value = functional.bind(self.value, rotated_old)

rotated_new = functional.permute(new, shifts=-self.length + index + 1)
rotated_new = functional.permute(new, shifts=self.size - index - 1)
self.value = functional.bind(self.value, rotated_new)

def __len__(self) -> int:
"""Returns the size of the sequence.
"""Returns the length of the sequence.

Examples::

>>> len(DS)
0

"""
return self.length
return self.size

def clear(self) -> None:
"""Empties the sequence.
Expand All @@ -785,7 +767,7 @@ def clear(self) -> None:

"""
self.value.fill_(0.0)
self.length = 0
self.size = 0

@classmethod
def from_tensor(cls, input: Tensor):
Expand All @@ -812,13 +794,13 @@ class Graph:

Args:
dimensions (int): number of dimensions of the graph.
directed (bool): decidies if the graph will be directed or not.
directed (bool): decides if the graph will be directed or not.
dtype (``torch.dtype``, optional): the desired data type of returned tensor. Default: if ``None``, uses a global default (see ``torch.set_default_tensor_type()``).
device (``torch.device``, optional): the desired device of returned tensor. 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.

Args:
input (Tensor): tensor representing a graph hypervector.
directed (bool): decidies if the graph will be directed or not.
directed (bool): decides if the graph will be directed or not.

Examples::

Expand Down