Skip to content

torch_geometric API documentation

Requires the optional dependency: pip install gsxform[pyg].

GraphScattering

GraphScattering(transform)

Bases: Module

Run a scattering transform on torch_geometric's sparse batch layout.

Wraps any ScatteringTransform so it takes the usual PyG inputs: node features [N_total, F], edge_index, and the batch vector. Graphs of different sizes are padded to a common node count and masked internally.

Parameters:

Name Type Description Default
transform ScatteringTransform

Scattering transform to apply, e.g. Diffusion(4, 3)

required
Source code in gsxform/pyg.py
31
32
33
34
35
36
37
38
39
40
def __init__(self, transform: ScatteringTransform) -> None:
    """Initialize the adapter.

    Parameters
    ----------
    transform: ScatteringTransform
        Scattering transform to apply, e.g. `Diffusion(4, 3)`
    """
    super().__init__()
    self.transform = transform

forward

forward(x, edge_index, batch=None, edge_weight=None)

Compute scattering features for a batch of graphs.

Parameters:

Name Type Description Default
x Tensor

Node features, shaped [N_total, F]

required
edge_index Tensor

Edges, shaped [2, E]

required
batch Tensor | None

Graph assignment of each node, shaped [N_total] and sorted, as produced by PyG's DataLoader. Defaults to None

None
edge_weight Tensor | None

Weight of each edge, shaped [E]. Defaults to None

None

Returns:

Type Description
Tensor

[B, F * n_coefficients] for graph output, or [N_total, F * n_coefficients] for node output, feature-major. With moment aggregation, each coefficient contributes one column per moment.

Source code in gsxform/pyg.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def forward(
    self,
    x: torch.Tensor,
    edge_index: torch.Tensor,
    batch: torch.Tensor | None = None,
    edge_weight: torch.Tensor | None = None,
) -> torch.Tensor:
    """Compute scattering features for a batch of graphs.

    Parameters
    ----------
    x: torch.Tensor
        Node features, shaped [N_total, F]
    edge_index: torch.Tensor
        Edges, shaped [2, E]
    batch: torch.Tensor, optional
        Graph assignment of each node, shaped [N_total] and sorted, as
        produced by PyG's `DataLoader`. Defaults to None
    edge_weight: torch.Tensor, optional
        Weight of each edge, shaped [E]. Defaults to None

    Returns
    -------
    torch.Tensor
        [B, F * n_coefficients] for graph output, or
        [N_total, F * n_coefficients] for node output, feature-major. With
        moment aggregation, each coefficient contributes one column per
        moment.
    """
    batch_size = 1 if batch is None else int(batch.max()) + 1

    x_dense, mask = to_dense_batch(x, batch, batch_size=batch_size)
    W_adj = to_dense_adj(
        edge_index,
        batch,
        edge_weight,
        max_num_nodes=x_dense.shape[1],
        batch_size=batch_size,
    ).to(x.dtype)

    out = self.transform(rearrange(x_dense, "b n f -> b f n"), W_adj, mask)

    if self.transform.output == "node":
        out = rearrange(out, "b f c n -> b n (f c)")
        nodes: torch.Tensor = out[mask]
        return nodes

    graphs: torch.Tensor = rearrange(out, "b f c -> b (f c)")
    return graphs

ScatteringFeatures

ScatteringFeatures(transform, attr='scattering')

Bases: BaseTransform

Precompute scattering features as a torch_geometric dataset transform.

Stores the scattering representation of each graph on the Data object, so a fixed transform is computed once per graph rather than every epoch,

Graph output is stored shaped [1, D], so DataLoader batches it to [B, D]; node output is stored shaped [N, D].

Parameters:

Name Type Description Default
transform ScatteringTransform

Scattering transform to apply, e.g. Diffusion(4, 3)

required
attr str

Name of the attribute the features are stored under. Defaults to "scattering"

'scattering'
Source code in gsxform/pyg.py
104
105
106
107
108
109
110
111
112
113
114
115
116
def __init__(self, transform: ScatteringTransform, attr: str = "scattering"):
    """Initialize the dataset transform.

    Parameters
    ----------
    transform: ScatteringTransform
        Scattering transform to apply, e.g. `Diffusion(4, 3)`
    attr: str
        Name of the attribute the features are stored under. Defaults to
        "scattering"
    """
    self.scattering = GraphScattering(transform)
    self.attr = attr

forward

forward(data)

Add scattering features to one graph.

Parameters:

Name Type Description Default
data Data

Graph with node features x and edge_index, and optionally edge_weight

required

Returns:

Type Description
Data

The same graph, with the features stored under attr

Source code in gsxform/pyg.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def forward(self, data: Data) -> Data:
    """Add scattering features to one graph.

    Parameters
    ----------
    data: Data
        Graph with node features `x` and `edge_index`, and optionally
        `edge_weight`

    Returns
    -------
    Data
        The same graph, with the features stored under `attr`
    """
    if data.x is None:
        raise ValueError(
            "ScatteringFeatures needs node features; for featureless graphs "
            "add some first, e.g. torch_geometric.transforms.Constant()"
        )

    with torch.no_grad():
        data[self.attr] = self.scattering(
            data.x,
            data.edge_index,
            edge_weight=getattr(data, "edge_weight", None),
        )

    return data