Graph
- class torchhd.structures.Graph(dimensions: int, vsa: Literal['BSC', 'MAP', 'HRR', 'FHRR', 'BSBC', 'VTB', 'MCR', 'CGR'] = 'MAP', *, directed=False, device=None, dtype=None)[source]
- class torchhd.structures.Graph(input: VSATensor, *, directed=False)
Hypervector-based graph data structure.
Creates an empty sequence of dim dimensions or from an input tensor.
- Parameters:
dimensions (int) – number of dimensions of the graph.
vsa – (
VSAOptions, optional): specifies the hypervector type and operations used (Default:"MAP").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: ifNone, uses a global default (seetorch.set_default_tensor_type()).device (
torch.device, optional) – the desired device of returned tensor. Default: ifNone, uses the current device for the default tensor type (see torch.set_default_tensor_type()).devicewill be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.input (VSATensor) – tensor representing a graph hypervector.
Examples:
>>> G = structures.Graph(10000, directed=True)
- add_edge(node1: VSATensor, node2: VSATensor) None[source]
Adds an edge to the graph.
If directed the direction goes from the first node to the second one.
- Parameters:
Examples:
>>> letters = list(string.ascii_lowercase) >>> letters_hv = torchhd.random(len(letters), 10000) >>> G.add_edge(letters_hv[0], letters_hv[1])
- contains(input: VSATensor) Tensor[source]
Returns the normalized dot similarity of the input vector against the graph.
- Parameters:
input (Tensor) – Hypervector to compare against the multiset.
Examples:
>>> e = G.encode_edge(letters_hv[0], letters_hv[1]) >>> G.contains(e) tensor(1.)
- encode_edge(node1: VSATensor, node2: VSATensor) VSATensor[source]
Returns the encoding of an edge.
If directed the direction goes from the first node to the second one.
- Parameters:
Examples:
>>> letters = list(string.ascii_lowercase) >>> letters_hv = torchhd.random(len(letters), 10000) >>> G.encode_edge(letters_hv[0], letters_hv[1]) tensor([-1., 1., -1., ..., 1., -1., -1.])
- classmethod from_edges(input: VSATensor, directed=False)[source]
Creates a graph from a VSATensor
See:
graph().- Parameters:
input (VSATensor) – tensor containing pairs of node hypervectors that share an edge.
directed (bool, optional) – specify if the graph is directed or not. Default:
False.
- Examples::
>>> edges = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 3]]) >>> node_embedding = embeddings.Random(4, 10000) >>> edges_hv = node_embedding(edges) >>> graph = structures.Graph.from_edges(edges_hv)
- node_neighbors(input: VSATensor, outgoing=True) VSATensor[source]
Returns the multiset of node neighbors of the input node.
- Parameters:
input (VSATensor) – Hypervector representing the node.
outgoing (bool, optional) – if
True, returns the neighboring nodes thatinputhas an edge to. IfFalse, returns the neighboring nodes thatinputhas 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.])