Skip to content

Fix graph neighbors #65

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 25, 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
22 changes: 11 additions & 11 deletions torchhd/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,10 @@ class Graph:

Args:
dimensions (int): number of dimensions of the graph.
directed (bool): decides if the graph will be directed or not.
directed (bool, optional): specify if the graph is directed or not. Default: ``False``.
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): decides if the graph will be directed or not.

Examples::

Expand All @@ -817,7 +814,7 @@ def __init__(self, input: Tensor, *, directed=False):
...

def __init__(self, dim_or_input: int, **kwargs):
self.directed = kwargs.get("directed", False)
self.is_directed = kwargs.get("directed", False)
if torch.is_tensor(dim_or_input):
self.value = dim_or_input
else:
Expand Down Expand Up @@ -861,28 +858,31 @@ def encode_edge(self, node1: Tensor, node2: Tensor) -> Tensor:
tensor([-1., 1., -1., ..., 1., -1., -1.])

"""
if self.directed:
return functional.bind(node1, node2)
else:
if self.is_directed:
return functional.bind(node1, functional.permute(node2))
else:
return functional.bind(node1, node2)

def node_neighbors(self, input: Tensor, outgoing=True) -> Tensor:
"""Returns the multiset of node neighbors of the input node.

Args:
input (Tensor): Hypervector representing the node.
outgoing (bool, optional): if ``True``, returns the neighboring nodes that ``input`` has an edge to. If ``False``, returns the neighboring nodes that ``input`` has an edge from. This only has effect for directed graphs. Default: ``True``.

Examples::

>>> G.node_neighbors(letters_hv[0])
tensor([ 1., 1., 1., ..., -1., -1., 1.])

"""
if self.directed:
if self.is_directed:
if outgoing:
return functional.permute(functional.bind(self.value, input), shifts=-1)
permuted_neighbors = functional.bind(self.value, input)
return functional.permute(permuted_neighbors, shifts=-1)
else:
return functional.bind(self.value, functional.permute(input, shifts=1))
permuted_node = functional.permute(input, shifts=1)
return functional.bind(self.value, permuted_node)
else:
return functional.bind(self.value, input)

Expand Down