torch_geometric.transforms.Pad

class Pad(max_num_nodes: Union[int, Dict[str, int]], max_num_edges: Optional[Union[int, Dict[Tuple[str, str, str], int]]] = None, node_pad_value: Union[int, float, Padding] = 0.0, edge_pad_value: Union[int, float, Padding] = 0.0, mask_pad_value: bool = False, add_pad_mask: bool = False, exclude_keys: Optional[List[str]] = None)[source]

Bases: BaseTransform

Applies padding to enforce consistent tensor shapes (functional name: pad).

This transform will pad node and edge features up to a maximum allowed size in the node or edge feature dimension. By default 0.0 is used as the padding value and can be configured by setting node_pad_value and edge_pad_value.

In case of applying Pad to a Data object, the node_pad_value value (or edge_pad_value) can be either:

  • an int, float or object of UniformPadding class for cases when all attributes are going to be padded with the same value;

  • an object of AttrNamePadding class for cases when padding is going to differ based on attribute names.

In case of applying Pad to a HeteroData object, the node_pad_value value (or edge_pad_value) can be either:

  • an int, float or object of UniformPadding class for cases when all attributes of all node (or edge) stores are going to be padded with the same value;

  • an object of AttrNamePadding class for cases when padding is going to differ based on attribute names (but not based on node or edge types);

  • an object of class NodeTypePadding or EdgeTypePadding for cases when padding values are going to differ based on node or edge types. Padding values can also differ based on attribute names for a given node or edge type by using AttrNamePadding objects as values of its values argument.

Note that in order to allow for consistent padding across all graphs in a dataset, below conditions must be met:

  • if max_num_nodes is a single value, it must be greater than or equal to the maximum number of nodes of any graph in the dataset;

  • if max_num_nodes is a dictionary, value for every node type must be greater than or equal to the maximum number of this type nodes of any graph in the dataset.

Example below shows how to create a Pad transform for an HeteroData object. The object is padded to have 10 nodes of type v0, 20 nodes of type v1 and 30 nodes of type v2. It is padded to have 80 edges of type ('v0', 'e0', 'v1'). All the attributes of the v0 nodes are padded using a value of 3.0. The x attribute of the v1 node type is padded using a value of -1.0, and the other attributes of this node type are padded using a value of 0.5. All the attributes of node types other than v0 and v1 are padded using a value of 1.0. All the attributes of the ('v0', 'e0', 'v1') edge type are padded usin a value of 3.5. The edge_attr attributes of the ('v1', 'e0', 'v0') edge type are padded using a value of -1.5, and any other attributes of this edge type are padded using a value of 5.5. All the attributes of edge types other than these two are padded using a value of 1.5.

Example: .. code-block:

num_nodes = {'v0': 10, 'v1': 20, 'v2':30}
num_edges = {('v0', 'e0', 'v1'): 80}

node_padding = NodeTypePadding({
    'v0': 3.0,
    'v1': AttrNamePadding({'x': -1.0}, default=0.5),
}, default=1.0)

edge_padding = EdgeTypePadding({
    ('v0', 'e0', 'v1'): 3.5,
    ('v1', 'e0', 'v0'): AttrNamePadding({'edge_attr': -1.5},
                                        default=5.5),
}, default=1.5)

transform = Pad(num_nodes, num_edges, node_padding, edge_padding)
Parameters:
  • max_num_nodes (int or dict) – The number of nodes after padding. In heterogeneous graphs, may also take in a dictionary denoting the number of nodes for specific node types.

  • max_num_edges (int or dict, optional) – The number of edges after padding. In heterogeneous graphs, may also take in a dictionary denoting the number of edges for specific edge types. (default: None)

  • node_pad_value (int or float or Padding, optional) – The fill value to use for node features. (default: 0.0)

  • edge_pad_value (int or float or Padding, optional) – The fill value to use for edge features. (default: 0.0) The edge_index tensor is padded with with the index of the first padded node (which represents a set of self-loops on the padded node). (default: 0.0)

  • mask_pad_value (bool, optional) – The fill value to use for train_mask, val_mask and test_mask attributes (default: False).

  • add_pad_mask (bool, optional) – If set to True, will attach node-level pad_node_mask and edge-level pad_edge_mask attributes to the output which indicates which elements in the data are real (represented by True) and which were added as a result of padding (represented by False). (default: False)

  • exclude_keys ([str], optional) – Keys to be removed from the input data object. (default: None)