torch_geometric.nn.models.GPSENodeEncoder

class GPSENodeEncoder(dim_emb: int, dim_pe_in: int, dim_pe_out: int, dim_in: Optional[int] = None, expand_x=False, norm_type='batchnorm', model_type='mlp', n_layers=2, dropout_be=0.5, dropout_ae=0.2)[source]

Bases: Module

A helper linear/MLP encoder that takes the GPSE encodings (based on the “Graph Positional and Structural Encoder” paper) precomputed as batch.pestat_GPSE in the input graphs, maps them to a desired dimension defined by dim_pe_out and appends them to node features.

Let’s say we have a graph dataset with 64 original node features, and we have generated GPSE encodings of dimension 32, i.e. data.pestat_GPSE = 32. Additionally, we want to use a GNN with an inner dimension of 128. To do so, we can map the 32-dimensional GPSE encodings to a higher dimension of 64, and then append them to the x attribute of the Data objects to obtain a 128-dimensional node feature representation. GPSENodeEncoder handles both this mapping and concatenation to x, the outputs of which can be used as input to a GNN:

encoder = GPSENodeEncoder(dim_emb=128, dim_pe_in=32, dim_pe_out=64,
                          expand_x=False)
gnn = GNN(...)

for batch in loader:
    x = encoder(batch.x, batch.pestat_GPSE)
    batch = gnn(x, batch.edge_index)
Parameters:
  • dim_emb (int) – Size of final node embedding.

  • dim_pe_in (int) – Original dimension of batch.pestat_GPSE.

  • dim_pe_out (int) – Desired dimension of GPSE after the encoder.

  • dim_in (int, optional) – Original dimension of input node features, required only if expand_x is set to True. (default: None)

  • expand_x (bool, optional) – Expand node features x from dim_in to (dim_emb - dim_pe_out)

  • norm_type (str, optional) – Type of normalization to apply. (default: batchnorm)

  • model_type (str, optional) – Type of encoder, either mlp or linear. (default: mlp)

  • n_layers (int, optional) – Number of MLP layers if model_type is mlp. (default: 2)

  • dropout_be (float, optional) – Dropout ratio of inputs to encoder, i.e. before encoding. (default: 0.5)

  • dropout_ae (float, optional) – Dropout ratio of outputs, i.e. after encoding. (default: 0.2)

forward(x, pos_enc)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.