deepsnap.hetero_graph

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.

Parameters
  • key (str) – The chosen property.

  • obj_type (str) – Node or message type.

  • as_label (bool) – If as_label, treat the tensor as labels.

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
  • edge_index (LongTensor) – The indices for edges.

  • num_nodes (dict, optional) – A dictionary of number of nodes.

  • num_neg_samples (dict, optional) – The number of negative samples to return. If set to None, will try to return a negative edge for every positive edge.

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).

Parameters

message_type (tuple or list) – Specified message type(s).

Returns

The feature dimension for specified message type(s).

Return type

int or dict

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.

Parameters

message_type (tuple or list) – Specified message type(s).

Returns

Number of labels for specified message type(s).

Return type

int or dict

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.

Parameters

message_type (tuple or list) – Specified message type(s).

Returns

The number of edges for a message type or for a list of message types.

Return type

int or dict

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.

Parameters

node_type (str or list) – Specified node type(s).

Returns

The node feature dimension for specified node type(s).

Return type

int or dict

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.

Parameters

node_type (str or list) – Specified node type(s).

Returns

Number of node labels for specified node type(s).

Return type

int or dict

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.

Parameters

node_type (str or list) – Specified node type(s).

Returns

The number of nodes for a node type or for list of node types.

Return type

int or dict

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.

Parameters
  • split_types (list) – Message types that will be splitted on.

  • message_ratio (float or list) – Split ratios.

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

list

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.

Parameters
  • split_types (list) – List of message types that will be splitted on.

  • split_ratio (float or list) – The ratio or list of ratios.

  • edge_split_mode (str) – “exact” or “approximate”.

  • shuffle (bool) – Whether to shuffle for the splitting.

Returns

A list of HeteroGraph objects.

Return type

list