| 1 |
|
| 2 |
""" |
| 3 |
Template Fused Gromov Wasserstein |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
> |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import torch |
| 12 |
import torch.nn as nn |
| 13 |
from ._utils import ( |
| 14 |
TFGW_template_initialization, |
| 15 |
FGW_distance_to_templates, |
| 16 |
wasserstein_distance_to_templates, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class TFGWPooling(nn.Module): |
| 21 |
r""" |
| 22 |
Template Fused Gromov-Wasserstein (TFGW) layer. This layer is a pooling layer for graph neural networks. |
| 23 |
Computes the fused Gromov-Wasserstein distances between the graph and a set of templates. |
| 24 |
|
| 25 |
|
| 26 |
.. math:: |
| 27 |
TFGW_{ \overline{ \mathcal{G} }, \alpha }(C,F,h)=[ FGW_{\alpha}(C,F,h,\overline{C}_k,\overline{F}_k,\overline{h}_k)]_{k=1}^{K} |
| 28 |
|
| 29 |
where : |
| 30 |
|
| 31 |
- :math:`\mathcal{G}=\{(\overline{C}_k,\overline{F}_k,\overline{h}_k) \}_{k \in \{1,...,K \}} \}` is the set of :math:`K` templates characterized by their adjacency matrices :math:`\overline{C}_k`, their feature matrices :math:`\overline{F}_k` and their node weights :math:`\overline{h}_k`. |
| 32 |
- :math:`C`, :math:`F` and :math:`h` are respectively the adjacency matrix, the feature matrix and the node weights of the graph. |
| 33 |
- :math:`\alpha` is the trade-off parameter between features and structure for the Fused Gromov-Wasserstein distance. |
| 34 |
|
| 35 |
|
| 36 |
Parameters |
| 37 |
---------- |
| 38 |
n_features : int |
| 39 |
Feature dimension of the nodes. |
| 40 |
n_tplt : int |
| 41 |
Number of graph templates. |
| 42 |
n_tplt_nodes : int |
| 43 |
Number of nodes in each template. |
| 44 |
alpha : float, optional |
| 45 |
FGW trade-off parameter (0 < alpha < 1). If None alpha is trained, else it is fixed at the given value. |
| 46 |
Weights features (alpha=0) and structure (alpha=1). |
| 47 |
train_node_weights : bool, optional |
| 48 |
If True, the templates node weights are learned. |
| 49 |
Else, they are uniform. |
| 50 |
multi_alpha: bool, optional |
| 51 |
If True, the alpha parameter is a vector of size n_tplt. |
| 52 |
feature_init_mean: float, optional |
| 53 |
Mean of the random normal law to initialize the template features. |
| 54 |
feature_init_std: float, optional |
| 55 |
Standard deviation of the random normal law to initialize the template features. |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
References |
| 60 |
---------- |
| 61 |
.. [53] Cédric Vincent-Cuaz, Rémi Flamary, Marco Corneli, Titouan Vayer, Nicolas Courty. |
| 62 |
"Template based graph neural network with optimal transport distances" |
| 63 |
""" |
| 64 |
|
| 65 |
def __init__( |
| 66 |
self, |
| 67 |
n_features, |
| 68 |
n_tplt=2, |
| 69 |
n_tplt_nodes=2, |
| 70 |
alpha=None, |
| 71 |
train_node_weights=True, |
| 72 |
multi_alpha=False, |
| 73 |
feature_init_mean=0.0, |
| 74 |
feature_init_std=1.0, |
| 75 |
): |
| 76 |
r""" |
| 77 |
Template Fused Gromov-Wasserstein (TFGW) layer. This layer is a pooling layer for graph neural networks. |
| 78 |
Computes the fused Gromov-Wasserstein distances between the graph and a set of templates. |
| 79 |
|
| 80 |
|
| 81 |
.. math:: |
| 82 |
TFGW_{\overline{\mathcal{G}},\alpha}(C,F,h)=[FGW_{\alpha}(C,F,h,\overline{C}_k,\overline{F}_k,\overline{h}_k)]_{k=1}^{K} |
| 83 |
|
| 84 |
where : |
| 85 |
|
| 86 |
- :math:`\mathcal{G}=\{(\overline{C}_k,\overline{F}_k,\overline{h}_k) \}_{k \in \{1,...,K \}} }` is the set of :math:`K` templates charactersised by their adjacency matrices :math:`\overline{C}_k`, their feature matrices :math:`\overline{F}_k` and their node weights :math:`\overline{h}_k`. |
| 87 |
- :math:`C`, :math:`F` and :math:`h` are respectively the adjacency matrix, the feature matrix and the node weights of the graph. |
| 88 |
- :math:`\alpha` is the trade-off parameter between features and structure for the Fused Gromov-Wasserstein distance. |
| 89 |
|
| 90 |
|
| 91 |
Parameters |
| 92 |
---------- |
| 93 |
n_features : int |
| 94 |
Feature dimension of the nodes. |
| 95 |
n_tplt : int |
| 96 |
Number of graph templates. |
| 97 |
n_tplt_nodes : int |
| 98 |
Number of nodes in each template. |
| 99 |
alpha : float, optional |
| 100 |
FGW trade-off parameter (0 < alpha < 1). If None alpha is trained, else it is fixed at the given value. |
| 101 |
Weights features (alpha=0) and structure (alpha=1). |
| 102 |
train_node_weights : bool, optional |
| 103 |
If True, the templates node weights are learned. |
| 104 |
Else, they are uniform. |
| 105 |
multi_alpha: bool, optional |
| 106 |
If True, the alpha parameter is a vector of size n_tplt. |
| 107 |
feature_init_mean: float, optional |
| 108 |
Mean of the random normal law to initialize the template features. |
| 109 |
feature_init_std: float, optional |
| 110 |
Standard deviation of the random normal law to initialize the template features. |
| 111 |
|
| 112 |
|
| 113 |
References |
| 114 |
---------- |
| 115 |
.. [53] Cédric Vincent-Cuaz, Rémi Flamary, Marco Corneli, Titouan Vayer, Nicolas Courty. |
| 116 |
"Template based graph neural network with optimal transport distances" |
| 117 |
|
| 118 |
""" |
| 119 |
super().__init__() |
| 120 |
|
| 121 |
self.n_tplt = n_tplt |
| 122 |
self.n_tplt_nodes = n_tplt_nodes |
| 123 |
self.n_features = n_features |
| 124 |
self.multi_alpha = multi_alpha |
| 125 |
self.feature_init_mean = feature_init_mean |
| 126 |
self.feature_init_std = feature_init_std |
| 127 |
|
| 128 |
tplt_adjacencies, tplt_features, self.q0 = TFGW_template_initialization( |
| 129 |
self.n_tplt, |
| 130 |
self.n_tplt_nodes, |
| 131 |
self.n_features, |
| 132 |
self.feature_init_mean, |
| 133 |
self.feature_init_std, |
| 134 |
) |
| 135 |
self.tplt_adjacencies = nn.Parameter(tplt_adjacencies) |
| 136 |
self.tplt_features = nn.Parameter(tplt_features) |
| 137 |
|
| 138 |
self.softmax = nn.Softmax(dim=1) |
| 139 |
|
| 140 |
if train_node_weights: |
| 141 |
self.q0 = nn.Parameter(self.q0) |
| 142 |
|
| 143 |
if alpha is None: |
| 144 |
if multi_alpha: |
| 145 |
self.alpha0 = torch.Tensor([0] * self.n_tplt) |
| 146 |
else: |
| 147 |
self.alpha0 = torch.Tensor([0]) |
| 148 |
self.alpha0 = nn.Parameter(self.alpha0) |
| 149 |
else: |
| 150 |
if multi_alpha: |
| 151 |
self.alpha0 = torch.Tensor([alpha] * self.n_tplt) |
| 152 |
else: |
| 153 |
self.alpha0 = torch.Tensor([alpha]) |
| 154 |
self.alpha0 = torch.logit(self.alpha0) |
| 155 |
|
| 156 |
def forward(self, x, edge_index, batch=None): |
| 157 |
""" |
| 158 |
Parameters |
| 159 |
---------- |
| 160 |
x : torch.Tensor |
| 161 |
Node features. |
| 162 |
edge_index : torch.Tensor |
| 163 |
Edge indices. |
| 164 |
batch : torch.Tensor, optional |
| 165 |
Batch vector which assigns each node to its graph. |
| 166 |
""" |
| 167 |
alpha = torch.sigmoid(self.alpha0) |
| 168 |
q = self.softmax(self.q0) |
| 169 |
x = FGW_distance_to_templates( |
| 170 |
edge_index, |
| 171 |
self.tplt_adjacencies, |
| 172 |
x, |
| 173 |
self.tplt_features, |
| 174 |
q, |
| 175 |
alpha, |
| 176 |
self.multi_alpha, |
| 177 |
batch, |
| 178 |
) |
| 179 |
return x |
| 180 |
|
| 181 |
|
| 182 |
class TWPooling(nn.Module): |
| 183 |
r""" |
| 184 |
Template Wasserstein (TW) layer, also known as OT-GNN layer. This layer is a pooling layer for graph neural networks. |
| 185 |
Computes the Wasserstein distances between the features of the graph features and a set of templates. |
| 186 |
|
| 187 |
.. math:: |
| 188 |
TW_{\overline{\mathcal{G}}}(C,F,h)=[W(F,h,\overline{F}_k,\overline{h}_k)]_{k=1}^{K} |
| 189 |
|
| 190 |
where : |
| 191 |
|
| 192 |
- :math:`\mathcal{G}=\{(\overline{F}_k,\overline{h}_k) \}_{k \in \{1,...,K \}} \}` is the set of :math:`K` templates characterized by their feature matrices :math:`\overline{F}_k` and their node weights :math:`\overline{h}_k`. |
| 193 |
- :math:`F` and :math:`h` are respectively the feature matrix and the node weights of the graph. |
| 194 |
|
| 195 |
Parameters |
| 196 |
---------- |
| 197 |
n_features : int |
| 198 |
Feature dimension of the nodes. |
| 199 |
n_tplt : int |
| 200 |
Number of graph templates. |
| 201 |
n_tplt_nodes : int |
| 202 |
Number of nodes in each template. |
| 203 |
train_node_weights : bool, optional |
| 204 |
If True, the templates node weights are learned. |
| 205 |
Else, they are uniform. |
| 206 |
feature_init_mean: float, optional |
| 207 |
Mean of the random normal law to initialize the template features. |
| 208 |
feature_init_std: float, optional |
| 209 |
Standard deviation of the random normal law to initialize the template features. |
| 210 |
|
| 211 |
References |
| 212 |
---------- |
| 213 |
.. [54] Bécigneul, G., Ganea, O. E., Chen, B., Barzilay, R., & Jaakkola, T. S. (2020). [Optimal transport graph neural networks] |
| 214 |
|
| 215 |
""" |
| 216 |
|
| 217 |
def __init__( |
| 218 |
self, |
| 219 |
n_features, |
| 220 |
n_tplt=2, |
| 221 |
n_tplt_nodes=2, |
| 222 |
train_node_weights=True, |
| 223 |
feature_init_mean=0.0, |
| 224 |
feature_init_std=1.0, |
| 225 |
): |
| 226 |
r""" |
| 227 |
Template Wasserstein (TW) layer, also known as OT-GNN layer. This layer is a pooling layer for graph neural networks. |
| 228 |
Computes the Wasserstein distances between the features of the graph features and a set of templates. |
| 229 |
|
| 230 |
.. math:: |
| 231 |
TW_{\overline{\mathcal{G}}}(C,F,h)=[W(F,h,\overline{F}_k,\overline{h}_k)]_{k=1}^{K} |
| 232 |
|
| 233 |
where : |
| 234 |
|
| 235 |
- :math:`\mathcal{G}=\{(\overline{F}_k,\overline{h}_k) \}_{k \in \llbracket 1;K \rrbracket }` is the set of :math:`K` templates characterized by their feature matrices :math:`\overline{F}_k` and their node weights :math:`\overline{h}_k`. |
| 236 |
- :math:`F` and :math:`h` are respectively the feature matrix and the node weights of the graph. |
| 237 |
|
| 238 |
Parameters |
| 239 |
---------- |
| 240 |
n_features : int |
| 241 |
Feature dimension of the nodes. |
| 242 |
n_tplt : int |
| 243 |
Number of graph templates. |
| 244 |
n_tplt_nodes : int |
| 245 |
Number of nodes in each template. |
| 246 |
train_node_weights : bool, optional |
| 247 |
If True, the templates node weights are learned. |
| 248 |
Else, they are uniform. |
| 249 |
feature_init_mean: float, optional |
| 250 |
Mean of the random normal law to initialize the template features. |
| 251 |
feature_init_std: float, optional |
| 252 |
Standard deviation of the random normal law to initialize the template features. |
| 253 |
|
| 254 |
References |
| 255 |
---------- |
| 256 |
.. [54] Bécigneul, G., Ganea, O. E., Chen, B., Barzilay, R., & Jaakkola, T. S. (2020). [Optimal transport graph neural networks] |
| 257 |
""" |
| 258 |
super().__init__() |
| 259 |
|
| 260 |
self.n_tplt = n_tplt |
| 261 |
self.n_tplt_nodes = n_tplt_nodes |
| 262 |
self.n_features = n_features |
| 263 |
self.feature_init_mean = feature_init_mean |
| 264 |
self.feature_init_std = feature_init_std |
| 265 |
|
| 266 |
_, tplt_features, self.q0 = TFGW_template_initialization( |
| 267 |
self.n_tplt, |
| 268 |
self.n_tplt_nodes, |
| 269 |
self.n_features, |
| 270 |
self.feature_init_mean, |
| 271 |
self.feature_init_std, |
| 272 |
) |
| 273 |
|
| 274 |
self.tplt_features = nn.Parameter(tplt_features) |
| 275 |
self.softmax = nn.Softmax(dim=1) |
| 276 |
|
| 277 |
if train_node_weights: |
| 278 |
self.q0 = nn.Parameter(self.q0) |
| 279 |
|
| 280 |
def forward(self, x, edge_index=None, batch=None): |
| 281 |
""" |
| 282 |
Parameters |
| 283 |
---------- |
| 284 |
x : torch.Tensor |
| 285 |
Node features. |
| 286 |
edge_index : torch.Tensor |
| 287 |
Edge indices. |
| 288 |
batch : torch.Tensor, optional |
| 289 |
Batch vector which assigns each node to its graph. |
| 290 |
""" |
| 291 |
q = self.softmax(self.q0) |
| 292 |
x = wasserstein_distance_to_templates(x, self.tplt_features, q, batch) |
| 293 |
return x |