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 settingnode_pad_value
andedge_pad_value
.In case of applying
Pad
to aData
object, thenode_pad_value
value (oredge_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 aHeteroData
object, thenode_pad_value
value (oredge_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
orEdgeTypePadding
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 usingAttrNamePadding
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 anHeteroData
object. The object is padded to have10
nodes of typev0
,20
nodes of typev1
and30
nodes of typev2
. It is padded to have80
edges of type('v0', 'e0', 'v1')
. All the attributes of thev0
nodes are padded using a value of3.0
. Thex
attribute of thev1
node 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 thanv0
andv1
are padded using a value of1.0
. All the attributes of the('v0', 'e0', 'v1')
edge type are padded using a value of3.5
. Theedge_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 of5.5
. All the attributes of edge types other than these two are padded using a value of1.5
.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_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
andtest_mask
attributes (default:False
).add_pad_mask (bool, optional) – If set to
True
, will attach node-levelpad_node_mask
and edge-levelpad_edge_mask
attributes 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
)