deepsnap.hetero_graph¶
Contents
DeepSNAP Heterogeneous Graph¶
-
class
HeteroGraph
(G=None, **kwargs)[source]¶ Bases:
deepsnap.graph.Graph
A plain python object modeling a heterogeneous graph with various attributes (Node types in
HeteroGraph
require string type).- Parameters
G (
networkx.classes.graph
) – A NetworkX graph object which contains features and labels for each node type and edge type.**kwargs – Keyworded argument list with keys such as
"node_feature"
,"node_label"
and corresponding attributes.
-
property
edge_types
¶ Return a list of edge types in the heterogeneous graph.
-
get_num_dims
(key, obj_type, as_label: bool = False) → int[source]¶ Returns the number of dimensions for one graph/node/edge property for specified types.
-
get_num_labels
(key, obj_type)[source]¶ Gets the lables for a specified key.
- Parameters
key (str) – The chosen property.
- Returns
Unique lables (in tensor format).
- Return type
torch.Tensor
-
property
message_types
¶ Return a list of message types (src_node_type, edge_type, end_node_type) in the graph.
-
static
negative_sampling
(edge_index: Dict[str, None._VariableFunctionsClass.tensor], num_nodes: Optional[Dict[str, int]] = None, num_neg_samples: Optional[Dict[str, int]] = None)[source]¶ Samples random negative edges for a heterogeneous graph given by
edge_index
.- Parameters
- Returns
The
edge_index
tensor for negative edges.- Return type
torch.LongTensor
-
property
node_types
¶ Return a list of node types in the heterogeneous graph.
-
num_edge_features
(message_type: Optional[Union[tuple, List[tuple]]] = None)[source]¶ Return the feature dimension for specified message type(s).
-
num_edge_labels
(message_type: Optional[Union[tuple, List[tuple]]] = None)[source]¶ Return the number of labels for a message type or for a list of message types.
-
num_edges
(message_type: Optional[Union[tuple, List[tuple]]] = None)[source]¶ Return the number of edges for a message type or for a list of message types.
-
num_node_features
(node_type: Optional[Union[str, List[str]]] = None)[source]¶ Return the node feature dimension for one node type or for a list of node types.
-
num_node_labels
(node_type: Optional[Union[str, List[str]]] = None)[source]¶ Return number of node labels for one node type or for a list of node types.
-
num_nodes
(node_type: Optional[Union[str, List[str]]] = None)[source]¶ Return number of nodes for one node type or for a list of node types.
-
resample_disjoint
(split_types, message_ratio)[source]¶ Resample splits of the message passing and supervision edges in the disjoint mode.
Note
If
apply_transform()
(on the message passing graph) was used before this resampling, it needs to be re-applied after resampling, to update some of the (supervision) edges that were in the objectives.
-
split
(task: str = 'node', split_types: Optional[Union[str, List[str], tuple, List[tuple]]] = None, split_ratio: Optional[List[float]] = None, edge_split_mode: str = 'exact', shuffle: bool = True)[source]¶ Split current heterogeneous graph object to a list of heterogeneous graph objects.
- Parameters
task (str) – One of node, edge or link_pred.
split_types (str or list) – Types splitted on. Default is None which will split all the types for the specified task.
split_ratio (list) – A list of ratios such as [train_ratio, validation_ratio, test_ratio].
edge_split_mode (str) – “exact” or “approximate”.
shuffle (bool) – Whether to shuffle data for the splitting.
- Returns
A list of
HeteroGraph
objects.- Return type
-
split_link_pred
(split_types: List[tuple], split_ratio: Union[float, List[float]], edge_split_mode: str = 'exact', shuffle: bool = True)[source]¶ Split the heterogeneous graph into len(split_ratio) heterogeneous graphs for the link prediction task. Internally this function splits the edge indices, and the model will only compute loss for the node embeddings in each splitted graph. This is only used for the transductive link prediction task. In this task, different parts of the graph are observed in train / val / test. If during training, we might further split the training graph for the message edges and supervision edges.
Note
This functon will be called twice.