ItemSetDict

class dgl.graphbolt.ItemSetDict(itemsets: Dict[str, ItemSet])[source]

Bases: object

Dictionary wrapper of ItemSet.

This class aims to assemble existing itemsets with different tags, for example, seed_nodes of different node types in a graph.

Parameters:

itemsets (Dict[str, ItemSet]) –

Examples

>>> import torch
>>> from dgl import graphbolt as gb
  1. Each itemset is a single tensor: seed nodes.

>>> node_ids_user = torch.arange(0, 5)
>>> node_ids_item = torch.arange(5, 10)
>>> item_set = gb.ItemSetDict({
...     "user": gb.ItemSet(node_ids_user, names="seeds"),
...     "item": gb.ItemSet(node_ids_item, names="seeds")})
>>> list(item_set)
[{"user": tensor(0)}, {"user": tensor(1)}, {"user": tensor(2)},
 {"user": tensor(3)}, {"user": tensor(4)}, {"item": tensor(5)},
 {"item": tensor(6)}, {"item": tensor(7)}, {"item": tensor(8)},
 {"item": tensor(9)}}]
>>> item_set[:]
{"user": tensor([0, 1, 2, 3, 4]), "item": tensor([5, 6, 7, 8, 9])}
>>> item_set.names
('seeds',)

2. Each itemset is a tuple of tensors with same shape: seed nodes and labels.

>>> node_ids_user = torch.arange(0, 2)
>>> labels_user = torch.arange(0, 2)
>>> node_ids_item = torch.arange(2, 5)
>>> labels_item = torch.arange(2, 5)
>>> item_set = gb.ItemSetDict({
...     "user": gb.ItemSet(
...         (node_ids_user, labels_user),
...         names=("seeds", "labels")),
...     "item": gb.ItemSet(
...         (node_ids_item, labels_item),
...         names=("seeds", "labels"))})
>>> list(item_set)
[{"user": (tensor(0), tensor(0))}, {"user": (tensor(1), tensor(1))},
 {"item": (tensor(2), tensor(2))}, {"item": (tensor(3), tensor(3))},
 {"item": (tensor(4), tensor(4))}}]
>>> item_set[:]
{"user": (tensor([0, 1]), tensor([0, 1])),
 "item": (tensor([2, 3, 4]), tensor([2, 3, 4]))}
>>> item_set.names
('seeds', 'labels')

3. Each itemset is a tuple of tensors with different shape: seeds and labels.

>>> seeds_like = torch.arange(0, 4).reshape(-1, 2)
>>> labels_like = torch.tensor([1, 0])
>>> seeds_follow = torch.arange(0, 6).reshape(-1, 2)
>>> labels_follow = torch.tensor([1, 1, 0])
>>> item_set = gb.ItemSetDict({
...     "user:like:item": gb.ItemSet(
...         (seeds_like, labels_like),
...         names=("seeds", "labels")),
...     "user:follow:user": gb.ItemSet(
...         (seeds_follow, labels_follow),
...         names=("seeds", "labels"))})
>>> list(item_set)
[{'user:like:item': (tensor([0, 1]), tensor(1))},
 {'user:like:item': (tensor([2, 3]), tensor(0))},
 {'user:follow:user': (tensor([0, 1]), tensor(1))},
 {'user:follow:user': (tensor([2, 3]), tensor(1))},
 {'user:follow:user': (tensor([4, 5]), tensor(0))}]
>>> item_set[:]
{'user:like:item': (tensor([[0, 1], [2, 3]]),
                    tensor([1, 0])),
 'user:follow:user': (tensor([[0, 1], [2, 3], [4, 5]]),
                      tensor([1, 1, 0]))}
>>> item_set.names
('seeds', 'labels')

4. Each itemset is a tuple of tensors with different shape: hyperlink and labels.

>>> first_seeds = torch.arange(0, 6).reshape(-1, 3)
>>> first_labels = torch.tensor([1, 0])
>>> second_seeds = torch.arange(0, 2).reshape(-1, 1)
>>> second_labels = torch.tensor([1, 0])
>>> item_set = gb.ItemSetDict({
...     "query:user:item": gb.ItemSet(
...         (first_seeds, first_labels),
...         names=("seeds", "labels")),
...     "user": gb.ItemSet(
...         (second_seeds, second_labels),
...         names=("seeds", "labels"))})
>>> list(item_set)
[{'query:user:item': (tensor([0, 1, 2]), tensor(1))},
 {'query:user:item': (tensor([3, 4, 5]), tensor(0))},
 {'user': (tensor([0]), tensor(1))},
 {'user': (tensor([1]), tensor(0))}]
>>> item_set[:]
{'query:user:item': (tensor([[0, 1, 2], [3, 4, 5]]),
                    tensor([1, 0])),
 'user': (tensor([[0], [1]]),tensor([1, 0]))}
>>> item_set.names
('seeds', 'labels')
property names: Tuple[str]

Return the names of the items.