Skip to content

Add test for create random permute function #131

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 6 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
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: 21 additions & 11 deletions torchhd/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,10 @@ def permute(input: VSATensor, *, shifts=1) -> VSATensor:
return input.permute(shifts)


def create_random_permute(dim: int) -> Callable[[VSATensor, int], VSATensor]:
class create_random_permute(torch.nn.Module):
forward_indices: LongTensor
backward_indices: LongTensor

r"""Creates random permutation functions.

Args:
Expand All @@ -741,21 +744,28 @@ def create_random_permute(dim: int) -> Callable[[VSATensor, int], VSATensor]:

"""

forward = torch.randperm(dim)
backward = torch.empty_like(forward)
backward[forward] = torch.arange(dim)
def __init__(self, dim: int) -> None:
super().__init__()

forward = torch.randperm(dim)
backward = torch.empty_like(forward)
backward[forward] = torch.arange(dim)

self.register_buffer("forward_indices", forward)
self.register_buffer("backward_indices", backward)

def permute(input: VSATensor, shifts: int = 1) -> VSATensor:
def __call__(self, input: VSATensor, shifts: int = 1) -> VSATensor:
y = input

if shifts > 0:
for _ in range(shifts):
y = y[..., forward]
for _ in range(abs(shifts)):
y = y[..., self.forward_indices]

elif shifts < 0:
for _ in range(shifts):
y = y[..., backward]
return y
for _ in range(abs(shifts)):
y = y[..., self.backward_indices]

return permute
return y.clone()


def inverse(input: VSATensor) -> VSATensor:
Expand Down
4 changes: 2 additions & 2 deletions torchhd/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def node_neighbors(self, input: VSATensor, outgoing=True) -> VSATensor:
return functional.bind(self.value, input.inverse())

def contains(self, input: VSATensor) -> Tensor:
"""Returns the cosine similarity of the input vector against the graph.
"""Returns the normalized dot similarity of the input vector against the graph.

Args:
input (Tensor): Hypervector to compare against the multiset.
Expand All @@ -955,7 +955,7 @@ def contains(self, input: VSATensor) -> Tensor:
>>> G.contains(e)
tensor(1.)
"""
return functional.cosine_similarity(input, self.value)
return functional.dot_similarity(input, self.value) / self.value.size(-1)

def clear(self) -> None:
"""Empties the graph.
Expand Down
4 changes: 2 additions & 2 deletions torchhd/tests/basis_hv/test_circular_hv.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_device(self, dtype):

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hv = functional.circular(3, 52, device=device, dtype=dtype)
assert hv.device == device
assert hv.device.type == device.type

@pytest.mark.parametrize("dtype", torch_dtypes)
@pytest.mark.parametrize("vsa", vsa_tensors)
Expand All @@ -148,7 +148,7 @@ def test_device(self, dtype, vsa):

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hv = functional.circular(3, 52, vsa, device=device, dtype=dtype)
assert hv.device == device
assert hv.device.type == device.type

def test_uses_default_dtype(self):
hv = functional.circular(3, 52, "BSC")
Expand Down
2 changes: 1 addition & 1 deletion torchhd/tests/basis_hv/test_empty_hv.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_device(self, dtype, vsa):

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hv = functional.empty(3, 52, vsa, device=device, dtype=dtype)
assert hv.device == device
assert hv.device.type == device.type

def test_uses_default_dtype(self):
hv = functional.empty(3, 52, "BSC")
Expand Down
2 changes: 1 addition & 1 deletion torchhd/tests/basis_hv/test_identity_hv.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_device(self, dtype, vsa):

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hv = functional.identity(3, 52, vsa, device=device, dtype=dtype)
assert hv.device == device
assert hv.device.type == device.type

def test_uses_default_dtype(self):
hv = functional.identity(3, 52, "BSC")
Expand Down
4 changes: 2 additions & 2 deletions torchhd/tests/basis_hv/test_level_hv.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from ..utils import torch_dtypes, supported_dtype, vsa_tensors

seed = 2147483644
seed = 2147483643


class Testlevel:
Expand Down Expand Up @@ -129,7 +129,7 @@ def test_device(self, dtype, vsa):

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hv = functional.level(3, 52, vsa, device=device, dtype=dtype)
assert hv.device == device
assert hv.device.type == device.type

def test_uses_default_dtype(self):
hv = functional.level(3, 52, "BSC")
Expand Down
2 changes: 1 addition & 1 deletion torchhd/tests/basis_hv/test_random_hv.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_device(self, dtype, vsa):

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hv = functional.random(3, 52, vsa, device=device, dtype=dtype)
assert hv.device == device
assert hv.device.type == device.type

def test_uses_default_dtype(self):
hv = functional.random(3, 52, "BSC")
Expand Down
4 changes: 2 additions & 2 deletions torchhd/tests/structures/test_distinct_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
class TestBindSequence:
def test_creation_dim(self):
S = structures.BindSequence(10000)
assert torch.equal(S.value, torch.ones(10000))
assert torch.allclose(S.value, torch.ones(10000))

def test_creation_tensor(self):
generator = torch.Generator()
generator.manual_seed(seed)
hv = functional.random(len(letters), 10000, generator=generator)

S = structures.BindSequence(hv[0])
assert torch.equal(S.value, hv[0])
assert torch.allclose(S.value, hv[0])

def test_generator(self):
generator = torch.Generator()
Expand Down
91 changes: 68 additions & 23 deletions torchhd/tests/structures/test_fsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import string

from torchhd import structures, functional
from torchhd import MAPTensor

seed = 2147483644
seed1 = 2147483643
Expand All @@ -35,7 +36,7 @@
class TestFSA:
def test_creation_dim(self):
F = structures.FiniteStateAutomata(10000)
assert torch.equal(F.value, torch.zeros(10000))
assert torch.allclose(F.value, torch.zeros(10000))

def test_generator(self):
generator = torch.Generator()
Expand All @@ -49,37 +50,81 @@ def test_generator(self):
assert (hv1 == hv2).min().item()

def test_add_transition(self):
generator = torch.Generator()
generator1 = torch.Generator()
generator.manual_seed(seed)
generator1.manual_seed(seed1)
tokens = functional.random(10, 10, generator=generator)
actions = functional.random(10, 10, generator=generator1)
tokens = MAPTensor(
[
[1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0],
[1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0],
[-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0],
[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0],
[1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0],
[1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0],
[-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0],
[-1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0],
[-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0],
]
)
states = MAPTensor(
[
[1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0],
[-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0],
[1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0],
[-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0],
[1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0],
[1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0],
[1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0],
]
)

F = structures.FiniteStateAutomata(10)

F.add_transition(tokens[0], actions[1], actions[2])
assert torch.equal(
F.add_transition(tokens[0], states[1], states[2])
assert torch.allclose(
F.value,
torch.tensor([1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, 1.0]),
MAPTensor([-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0]),
)
F.add_transition(tokens[1], actions[1], actions[3])
assert torch.equal(
F.value, torch.tensor([0.0, 0.0, -2.0, 2.0, 0.0, 2.0, 0.0, -2.0, -2.0, 0.0])
F.add_transition(tokens[1], states[1], states[3])
assert torch.allclose(
F.value, MAPTensor([-2.0, 0.0, 2.0, 0.0, -2.0, 2.0, 0.0, 0.0, 0.0, -2.0])
)
F.add_transition(tokens[2], actions[1], actions[3])
assert torch.equal(
F.add_transition(tokens[2], states[1], states[3])
assert torch.allclose(
F.value,
torch.tensor([1.0, 1.0, -3.0, 1.0, 1.0, 3.0, -1.0, -1.0, -1.0, 1.0]),
MAPTensor([-1.0, -1.0, 1.0, -1.0, -3.0, 1.0, -1.0, 1.0, -1.0, -1.0]),
)

def test_transition(self):
generator = torch.Generator()
generator1 = torch.Generator()
generator.manual_seed(seed)
generator1.manual_seed(seed1)
tokens = functional.random(10, 10, generator=generator)
states = functional.random(10, 10, generator=generator1)
tokens = MAPTensor(
[
[1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0],
[1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0],
[-1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0],
[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0],
[1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0],
[1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0],
[-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0],
[-1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0],
[-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0],
]
)
states = MAPTensor(
[
[1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0],
[-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0],
[1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0],
[-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0],
[1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0],
[1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0],
[1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0],
]
)

F = structures.FiniteStateAutomata(10)

Expand Down Expand Up @@ -121,6 +166,6 @@ def test_clear(self):
F.add_transition(tokens[2], states[1], states[5])

F.clear()
assert torch.equal(
assert torch.allclose(
F.value, torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
)
34 changes: 17 additions & 17 deletions torchhd/tests/structures/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@
from torchhd import structures, functional
from torchhd.tensors.map import MAPTensor

seed = 2147483644
seed = 2147483645
letters = list(string.ascii_lowercase)


class TestGraph:
def test_creation_dim(self):
G = structures.Graph(10000, directed=True)
assert torch.equal(G.value, torch.zeros(10000))
assert torch.allclose(G.value, torch.zeros(10000))

def test_creation_tensor(self):
generator = torch.Generator()
generator.manual_seed(seed)
hv = functional.random(len(letters), 10000, generator=generator)
g = functional.bind(hv[0], hv[1])
G = structures.Graph(g)
assert torch.equal(G.value, g)
assert torch.allclose(G.value, g)

def test_generator(self):
generator = torch.Generator()
Expand All @@ -68,22 +68,22 @@ def test_add_edge(self):
).as_subclass(MAPTensor)

G.add_edge(hv[0], hv[1])
assert torch.equal(
assert torch.allclose(
G.value, torch.tensor([-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0])
)
G.add_edge(hv[2], hv[3])
assert torch.equal(
assert torch.allclose(
G.value, torch.tensor([-2.0, -2.0, 0.0, 2.0, -2.0, 0.0, 2.0, -2.0])
)

GD = structures.Graph(8, directed=True)

GD.add_edge(hv[0], hv[1])
assert torch.equal(
assert torch.allclose(
GD.value, torch.tensor([-1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0])
)
GD.add_edge(hv[2], hv[3])
assert torch.equal(
assert torch.allclose(
GD.value, torch.tensor([0.0, 0.0, 0.0, -2.0, 0.0, -2.0, 2.0, -2.0])
)

Expand All @@ -99,23 +99,23 @@ def test_encode_edge(self):
).as_subclass(MAPTensor)

e1 = G.encode_edge(hv[0], hv[1])
assert torch.equal(
assert torch.allclose(
e1, torch.tensor([-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0])
)
e2 = G.encode_edge(hv[2], hv[3])
assert torch.equal(
assert torch.allclose(
e2, torch.tensor([-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0])
)

GD = structures.Graph(8, directed=True)

e1 = GD.encode_edge(hv[0], hv[1])
assert torch.equal(
assert torch.allclose(
e1, torch.tensor([-1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0])
)
e2 = GD.encode_edge(hv[2], hv[3])
print(e2)
assert torch.equal(
assert torch.allclose(
e2, torch.tensor([1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0])
)

Expand Down Expand Up @@ -157,8 +157,8 @@ def test_node_neighbors(self):
def test_contains(self):
generator = torch.Generator()
generator.manual_seed(seed)
hv = functional.random(4, 8, generator=generator)
G = structures.Graph(8)
hv = functional.random(4, 1000, generator=generator)
G = structures.Graph(1000)

e1 = G.encode_edge(hv[0], hv[1])
e2 = G.encode_edge(hv[0], hv[2])
Expand All @@ -172,7 +172,7 @@ def test_contains(self):
assert G.contains(e2) > torch.tensor([0.6])
assert G.contains(e3) < torch.tensor(0.6)

GD = structures.Graph(8, directed=True)
GD = structures.Graph(1000, directed=True)

ee1 = GD.encode_edge(hv[0], hv[1])
ee2 = GD.encode_edge(hv[0], hv[2])
Expand Down Expand Up @@ -200,16 +200,16 @@ def test_clear(self):

G.clear()

assert torch.equal(
assert torch.allclose(
G.value, torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
)

def test_from_edges(self):
generator = torch.Generator()
generator.manual_seed(seed)

hv = functional.random(4, 8, generator=generator)
edges = torch.empty(2, 3, 8).as_subclass(MAPTensor)
hv = functional.random(4, 1000, generator=generator)
edges = torch.empty(2, 3, 1000).as_subclass(MAPTensor)
edges[0, 0] = hv[0]
edges[1, 0] = hv[1]
edges[0, 1] = hv[0]
Expand Down
Loading