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:
BaseTransformApplies 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.0is used as the padding value and can be configured by settingnode_pad_valueandedge_pad_value.In case of applying
Padto aDataobject, thenode_pad_valuevalue (oredge_pad_value) can be either:an int, float or object of
UniformPaddingclass for cases when all attributes are going to be padded with the same value;an object of
AttrNamePaddingclass for cases when padding is going to differ based on attribute names.
In case of applying
Padto aHeteroDataobject, thenode_pad_valuevalue (oredge_pad_value) can be either:an int, float or object of
UniformPaddingclass for cases when all attributes of all node (or edge) stores are going to be padded with the same value;an object of
AttrNamePaddingclass for cases when padding is going to differ based on attribute names (but not based on node or edge types);an object of class
NodeTypePaddingorEdgeTypePaddingfor 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 usingAttrNamePaddingobjects 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_nodesis 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_nodesis 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
Padtransform for anHeteroDataobject. The object is padded to have10nodes of typev0,20nodes of typev1and30nodes of typev2. It is padded to have80edges of type('v0', 'e0', 'v1'). All the attributes of thev0nodes are padded using a value of3.0. Thexattribute of thev1node type is padded using a value of-1.0, and the other attributes of this node type are padded using a value of0.5. All the attributes of node types other thanv0andv1are padded using a value of1.0. All the attributes of the('v0', 'e0', 'v1')edge type are padded usin a value of3.5. Theedge_attrattributes 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 of5.5. All the attributes of edge types other than these two are padded using a value of1.5.Example:
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) Theedge_indextensor 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_maskandtest_maskattributes (default:False).add_pad_mask (bool, optional) – If set to
True, will attach node-levelpad_node_maskand edge-levelpad_edge_maskattributes to the output which indicates which elements in the data are real (represented byTrue) and which were added as a result of padding (represented byFalse). (default:False)exclude_keys ([str], optional) – Keys to be removed from the input data object. (default:
None)