MetaPath2Vec
¶
MetaPath2Vec(
edge_index_dict: Mapping[EdgeType, array],
embedding_dim: int,
metapath: Sequence[EdgeType],
walk_length: int,
context_size: int,
walks_per_node: int = 1,
num_negative_samples: int = 1,
num_nodes_dict: Optional[Mapping[NodeType, int]] = None,
use_gpu: bool = True,
)
Bases: Module
The MetaPath2Vec model from the "metapath2vec: Scalable Representation Learning for Heterogeneous Networks" paper.
Random walks that follow a given meta-path (a sequence of edge types) are sampled in a heterogeneous graph, and node embeddings are learned with a skip-gram objective and negative sampling. Negative samples respect node types: each position of a negative walk gets a random node of the type the meta-path puts at that position.
All node types visited by the meta-path share one embedding table, so
they are embedded in the same space. Use model(node_type) to get the
embeddings of one node type.
Note
This feature is experimental and requires mlx_cluster>=0.1.2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index_dict
|
Mapping[EdgeType, array]
|
|
required |
embedding_dim
|
int
|
The size of each embedding vector. |
required |
metapath
|
Sequence[EdgeType]
|
The meta-path, a list of |
required |
walk_length
|
int
|
Number of steps per random walk, so every walk has
|
required |
context_size
|
int
|
Size of the context windows cut out of each walk; the
first node of a window is paired with the other
|
required |
walks_per_node
|
int
|
Number of walks to start from each node of type
|
1
|
num_negative_samples
|
int
|
Number of negative walks per positive walk.
(default: |
1
|
num_nodes_dict
|
Optional[Mapping[NodeType, int]]
|
Optional number of nodes per node type, e.g.
|
None
|
use_gpu
|
bool
|
Whether to sample the random walks on the GPU.
(default: |
True
|
Example:
metapath = [
("author", "writes", "paper"),
("paper", "published_in", "venue"),
("venue", "publishes", "paper"),
("paper", "written_by", "author"),
]
model = MetaPath2Vec(
edge_index_dict,
embedding_dim=128,
metapath=metapath,
walk_length=20,
context_size=7,
)
optimizer = optim.Adam(learning_rate=0.01)
loss_and_grad = nn.value_and_grad(model, model.loss)
for pos, neg in model.dataloader(batch_size=128):
loss, grads = loss_and_grad(pos, neg)
optimizer.update(model, grads)
author_embeddings = model("author")
Methods:
| Name | Description |
|---|---|
__call__ |
Returns the embeddings of the nodes of type |
pos_sample |
Samples positive context windows from meta-path random walks. |
neg_sample |
Samples negative context windows. |
dataloader |
Yields |
loss |
Skip-gram loss with negative sampling. |
Attributes:
| Name | Type | Description |
|---|---|---|
node_types |
List[NodeType]
|
Node types with an embedding, in embedding-table order. |
num_nodes_dict |
Dict[NodeType, int]
|
Number of embedded nodes per node type. |
Source code in mlx_graphs/algorithms/metapath2vec.py
node_types
property
¶
node_types: List[NodeType]
Node types with an embedding, in embedding-table order.
num_nodes_dict
property
¶
Number of embedded nodes per node type.
__call__
¶
__call__(node_type: NodeType, batch: Optional[array] = None) -> array
Returns the embeddings of the nodes of type node_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type
|
NodeType
|
The node type. |
required |
batch
|
Optional[array]
|
Optional local node ids of that type. All nodes of the type are returned if omitted. |
None
|
Source code in mlx_graphs/algorithms/metapath2vec.py
pos_sample
¶
Samples positive context windows from meta-path random walks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
array
|
Local ids of start nodes, of type |
required |
Returns:
| Type | Description |
|---|---|
array
|
|
array
|
that run past the end of a walk that hit a dead end (a node with |
array
|
no neighbor of the next edge type) are dropped. |
Source code in mlx_graphs/algorithms/metapath2vec.py
neg_sample
¶
Samples negative context windows.
Each negative walk starts at a node of batch and continues with
random nodes of the type the meta-path puts at each position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
array
|
Local ids of start nodes, of type |
required |
Returns:
| Type | Description |
|---|---|
array
|
|
Source code in mlx_graphs/algorithms/metapath2vec.py
dataloader
¶
Yields (pos, neg) samples for mini-batches of start nodes.
Every node of type metapath[0][0] is used as a start node once per
pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of start nodes per mini-batch. |
required |
shuffle
|
bool
|
Whether to shuffle the start nodes. (default: |
True
|
Source code in mlx_graphs/algorithms/metapath2vec.py
loss
¶
Skip-gram loss with negative sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_rw
|
array
|
Positive windows from |
required |
neg_rw
|
array
|
Negative windows from |
required |
Source code in mlx_graphs/algorithms/metapath2vec.py
from mlx_graphs.algorithms import MetaPath2Vec · mlx_graphs/algorithms/metapath2vec.py