1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
torch_geometric.nn
==================
.. contents:: Contents
:local:
.. autoclass:: torch_geometric.nn.sequential.Sequential
.. currentmodule:: torch_geometric.nn.dense
{% for name in torch_geometric.nn.dense.lin_classes %}
.. autoclass:: {{ name }}
:members:
{% endfor %}
Convolutional Layers
--------------------
.. currentmodule:: torch_geometric.nn.conv
.. autosummary::
:nosignatures:
:toctree: ../generated
:template: autosummary/nn.rst
{% for name in torch_geometric.nn.conv.classes %}
{{ name }}
{% endfor %}
Aggregation Operators
---------------------
.. currentmodule:: torch_geometric.nn.aggr
Aggregation functions play an important role in the message passing framework and the readout functions of Graph Neural Networks.
Specifically, many works in the literature (`Hamilton et al. (2017) <https://arxiv.org/abs/1706.02216>`__, `Xu et al. (2018) <https://arxiv.org/abs/1810.00826>`__, `Corso et al. (2020) <https://arxiv.org/abs/2004.05718>`__, `Li et al. (2020) <https://arxiv.org/abs/2006.07739>`__, `Tailor et al. (2021) <https://arxiv.org/abs/2104.01481>`__) demonstrate that the choice of aggregation functions contributes significantly to the representational power and performance of the model.
For example, **mean aggregation** captures the distribution (or proportions) of elements, **max aggregation** proves to be advantageous to identify representative elements, and **sum aggregation** enables the learning of structural graph properties (`Xu et al. (2018) <https://arxiv.org/abs/1810.00826>`__).
Recent works also show that using **multiple aggregations** (`Corso et al. (2020) <https://arxiv.org/abs/2004.05718>`__, `Tailor et al. (2021) <https://arxiv.org/abs/2104.01481>`__) and **learnable aggregations** (`Li et al. (2020) <https://arxiv.org/abs/2006.07739>`__) can potentially provide substantial improvements.
Another line of research studies optimization-based and implicitly-defined aggregations (`Bartunov et al. (2022) <https://arxiv.org/abs/2202.12795>`__).
Furthermore, an interesting discussion concerns the trade-off between representational power (usually gained through learnable functions implemented as neural networks) and the formal property of permutation invariance (`Buterez et al. (2022) <https://arxiv.org/abs/2211.04952>`__).
To facilitate further experimentation and unify the concepts of aggregation within GNNs across both :class:`~torch_geometric.nn.conv.MessagePassing` and global readouts, we have made the concept of :class:`~torch_geometric.nn.aggr.Aggregation` a first-class principle in :pyg:`PyG`.
As of now, :pyg:`PyG` provides support for various aggregations --- from rather simple ones (*e.g.*, :obj:`mean`, :obj:`max`, :obj:`sum`), to advanced ones (*e.g.*, :obj:`median`, :obj:`var`, :obj:`std`), learnable ones (*e.g.*, :class:`~torch_geometric.nn.aggr.SoftmaxAggregation`, :class:`~torch_geometric.nn.aggr.PowerMeanAggregation`, :class:`~torch_geometric.nn.aggr.SetTransformerAggregation`), and exotic ones (*e.g.*, :class:`~torch_geometric.nn.aggr.MLPAggregation`, :class:`~torch_geometric.nn.aggr.LSTMAggregation`, :class:`~torch_geometric.nn.aggr.SortAggregation`, :class:`~torch_geometric.nn.aggr.EquilibriumAggregation`):
.. code-block:: python
from torch_geometric.nn import aggr
# Simple aggregations:
mean_aggr = aggr.MeanAggregation()
max_aggr = aggr.MaxAggregation()
# Advanced aggregations:
median_aggr = aggr.MedianAggregation()
# Learnable aggregations:
softmax_aggr = aggr.SoftmaxAggregation(learn=True)
powermean_aggr = aggr.PowerMeanAggregation(learn=True)
# Exotic aggregations:
lstm_aggr = aggr.LSTMAggregation(in_channels=..., out_channels=...)
sort_aggr = aggr.SortAggregation(k=4)
We can then easily apply these aggregations over a batch of sets of potentially varying size.
For this, an :obj:`index` vector defines the mapping from input elements to their location in the output:
.. code-block:: python
# Feature matrix holding 1000 elements with 64 features each:
x = torch.randn(1000, 64)
# Randomly assign elements to 100 sets:
index = torch.randint(0, 100, (1000, ))
output = mean_aggr(x, index) # Output shape: [100, 64]
Notably, all aggregations share the same set of forward arguments, as described in detail in the :class:`torch_geometric.nn.aggr.Aggregation` base class.
Each of the provided aggregations can be used within :class:`~torch_geometric.nn.conv.MessagePassing` as well as for hierachical/global pooling to obtain graph-level representations:
.. code-block:: python
import torch
from torch_geometric.nn import MessagePassing
class MyConv(MessagePassing):
def __init__(self, ...):
# Use a learnable softmax neighborhood aggregation:
super().__init__(aggr=aggr.SoftmaxAggregation(learn=True))
def forward(self, x, edge_index):
....
class MyGNN(torch.nn.Module)
def __init__(self, ...):
super().__init__()
self.conv = MyConv(...)
# Use a global sort aggregation:
self.global_pool = aggr.SortAggregation(k=4)
self.classifier = torch.nn.Linear(...)
def foward(self, x, edge_index, batch):
x = self.conv(x, edge_index).relu()
x = self.global_pool(x, batch)
x = self.classifier(x)
return x
In addition, the aggregation package of :pyg:`PyG` introduces two new concepts:
First, aggregations can be **resolved from pure strings** via a lookup table, following the design principles of the `class-resolver <https://github.com/cthoyt/class-resolver>`__ library, *e.g.*, by simply passing in :obj:`"median"` to the :class:`~torch_geometric.nn.conv.MessagePassing` module.
This will automatically resolve to the :obj:`~torch_geometric.nn.aggr.MedianAggregation` class:
.. code-block:: python
class MyConv(MessagePassing):
def __init__(self, ...):
super().__init__(aggr="median")
Secondly, **multiple aggregations** can be combined and stacked via the :class:`~torch_geometric.nn.aggr.MultiAggregation` module in order to enhance the representational power of GNNs (`Corso et al. (2020) <https://arxiv.org/abs/2004.05718>`__, `Tailor et al. (2021) <https://arxiv.org/abs/2104.01481>`__):
.. code-block:: python
class MyConv(MessagePassing):
def __init__(self, ...):
# Combines a set of aggregations and concatenates their results,
# i.e. its output will be `[num_nodes, 3 * out_channels]` here.
# Note that the interface also supports automatic resolution.
super().__init__(aggr=aggr.MultiAggregation(
['mean', 'std', aggr.SoftmaxAggregation(learn=True)]))
Importantly, :class:`~torch_geometric.nn.aggr.MultiAggregation` provides various options to combine the outputs of its underlying aggegations (*e.g.*, using concatenation, summation, attention, ...) via its :obj:`mode` argument.
The default :obj:`mode` performs concatenation (:obj:`"cat"`).
For combining via attention, we need to additionally specify the :obj:`in_channels` :obj:`out_channels`, and :obj:`num_heads`:
.. code-block:: python
multi_aggr = aggr.MultiAggregation(
aggrs=['mean', 'std'],
mode='attn',
mode_kwargs=dict(in_channels=64, out_channels=64, num_heads=4),
)
If aggregations are given as a list, they will be automatically resolved to a :class:`~torch_geometric.nn.aggr.MultiAggregation`, *e.g.*, :obj:`aggr=['mean', 'std', 'median']`.
Finally, we added full support for customization of aggregations into the :class:`~torch_geometric.nn.conv.SAGEConv` layer --- simply override its :obj:`aggr` argument and **utilize the power of aggregation within your GNN**.
.. note::
You can read more about the :class:`torch_geometric.nn.aggr` package in this `blog post <https://medium.com/@pytorch_geometric/a-principled-approach-to-aggregations-983c086b10b3>`__.
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.aggr.classes %}
{{ name }}
{% endfor %}
Normalization Layers
--------------------
.. currentmodule:: torch_geometric.nn.norm
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.norm.classes %}
{{ name }}
{% endfor %}
Pooling Layers
--------------
.. currentmodule:: torch_geometric.nn.pool
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.pool.classes %}
{{ name }}
{% endfor %}
Unpooling Layers
----------------
.. currentmodule:: torch_geometric.nn.unpool
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.unpool.classes %}
{{ name }}
{% endfor %}
Models
------
.. currentmodule:: torch_geometric.nn.models
.. autosummary::
:nosignatures:
:toctree: ../generated
:template: autosummary/nn.rst
{% for name in torch_geometric.nn.models.classes %}
{{ name }}
{% endfor %}
KGE Models
----------
.. currentmodule:: torch_geometric.nn.kge
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.kge.classes %}
{{ name }}
{% endfor %}
Encodings
---------
.. automodule:: torch_geometric.nn.encoding
:members:
:undoc-members:
:exclude-members: training
Functional
----------
.. py:currentmodule:: torch_geometric.nn.functional
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.functional.classes %}
{{ name }}
{% endfor %}
Dense Convolutional Layers
--------------------------
.. currentmodule:: torch_geometric.nn.dense
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.dense.conv_classes %}
{{ name }}
{% endfor %}
Dense Pooling Layers
--------------------
.. currentmodule:: torch_geometric.nn.dense
.. autosummary::
:nosignatures:
:toctree: ../generated
{% for name in torch_geometric.nn.dense.pool_classes %}
{{ name }}
{% endfor %}
Model Transformations
---------------------
.. autoclass:: torch_geometric.nn.fx.Transformer
:members:
:undoc-members:
:exclude-members: graph, find_by_target, find_by_name
.. autofunction:: torch_geometric.nn.to_hetero_transformer.to_hetero
.. autofunction:: torch_geometric.nn.to_hetero_with_bases_transformer.to_hetero_with_bases
DataParallel Layers
-------------------
.. automodule:: torch_geometric.nn.data_parallel
:members:
Model Hub
---------
.. automodule:: torch_geometric.nn.model_hub
:members:
Model Summary
-------------
.. automodule:: torch_geometric.nn.summary
:members:
|