| 1 |
"""Tests for module gnn""" |
| 2 |
|
| 3 |
|
| 4 |
> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
try: |
| 11 |
import torch_geometric |
| 12 |
import torch |
| 13 |
from torch_geometric.nn import Linear |
| 14 |
from torch_geometric.data import Data as GraphData |
| 15 |
from torch_geometric.loader import DataLoader |
| 16 |
import torch.nn as nn |
| 17 |
from ot.gnn import TFGWPooling, TWPooling |
| 18 |
|
| 19 |
except ImportError: |
| 20 |
torch_geometric = False |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.skipif(not torch_geometric, reason="pytorch_geometric not installed") |
| 24 |
def test_TFGW_optim(): |
| 25 |
|
| 26 |
|
| 27 |
class pooling_TFGW(nn.Module): |
| 28 |
""" |
| 29 |
Pooling architecture using the TFGW layer. |
| 30 |
""" |
| 31 |
|
| 32 |
def __init__(self, n_features, n_templates, n_template_nodes): |
| 33 |
""" |
| 34 |
Pooling architecture using the TFGW layer. |
| 35 |
""" |
| 36 |
super().__init__() |
| 37 |
|
| 38 |
self.n_features = n_features |
| 39 |
self.n_templates = n_templates |
| 40 |
self.n_template_nodes = n_template_nodes |
| 41 |
|
| 42 |
self.TFGW = TFGWPooling( |
| 43 |
self.n_templates, self.n_template_nodes, self.n_features |
| 44 |
) |
| 45 |
|
| 46 |
self.linear = Linear(self.n_templates, 1) |
| 47 |
|
| 48 |
def forward(self, x, edge_index): |
| 49 |
x = self.TFGW(x, edge_index) |
| 50 |
|
| 51 |
x = self.linear(x) |
| 52 |
|
| 53 |
return x |
| 54 |
|
| 55 |
torch.manual_seed(0) |
| 56 |
|
| 57 |
n_templates = 3 |
| 58 |
n_template_nodes = 3 |
| 59 |
n_nodes = 10 |
| 60 |
n_features = 3 |
| 61 |
n_epochs = 3 |
| 62 |
|
| 63 |
C1 = torch.randint(0, 2, size=(n_nodes, n_nodes)) |
| 64 |
C2 = torch.randint(0, 2, size=(n_nodes, n_nodes)) |
| 65 |
|
| 66 |
edge_index1 = torch.stack(torch.where(C1 == 1)) |
| 67 |
edge_index2 = torch.stack(torch.where(C2 == 1)) |
| 68 |
|
| 69 |
x1 = torch.rand(n_nodes, n_features) |
| 70 |
x2 = torch.rand(n_nodes, n_features) |
| 71 |
|
| 72 |
graph1 = GraphData(x=x1, edge_index=edge_index1, y=torch.tensor([0.0])) |
| 73 |
graph2 = GraphData(x=x2, edge_index=edge_index2, y=torch.tensor([1.0])) |
| 74 |
|
| 75 |
dataset = DataLoader([graph1, graph2], batch_size=1) |
| 76 |
|
| 77 |
model_FGW = pooling_TFGW(n_features, n_templates, n_template_nodes) |
| 78 |
|
| 79 |
optimizer = torch.optim.Adam(model_FGW.parameters(), lr=0.01) |
| 80 |
criterion = torch.nn.CrossEntropyLoss() |
| 81 |
|
| 82 |
model_FGW.train() |
| 83 |
|
| 84 |
for i in range(n_epochs): |
| 85 |
for data in dataset: |
| 86 |
out = model_FGW(data.x, data.edge_index) |
| 87 |
loss = criterion(out, data.y) |
| 88 |
loss.backward() |
| 89 |
optimizer.step() |
| 90 |
optimizer.zero_grad() |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.skipif(not torch_geometric, reason="pytorch_geometric not installed") |
| 94 |
def test_TFGW_variants(): |
| 95 |
|
| 96 |
|
| 97 |
class GNN_pooling(nn.Module): |
| 98 |
""" |
| 99 |
Pooling architecture using the TW layer. |
| 100 |
""" |
| 101 |
|
| 102 |
def __init__(self, n_features, n_templates, n_template_nodes, pooling_layer): |
| 103 |
""" |
| 104 |
Pooling architecture using the TW layer. |
| 105 |
""" |
| 106 |
super().__init__() |
| 107 |
|
| 108 |
self.n_features = n_features |
| 109 |
self.n_templates = n_templates |
| 110 |
self.n_template_nodes = n_template_nodes |
| 111 |
|
| 112 |
self.TFGW = pooling_layer |
| 113 |
|
| 114 |
self.linear = Linear(self.n_templates, 1) |
| 115 |
|
| 116 |
def forward(self, x, edge_index, batch=None): |
| 117 |
x = self.TFGW(x, edge_index, batch=batch) |
| 118 |
|
| 119 |
x = self.linear(x) |
| 120 |
|
| 121 |
return x |
| 122 |
|
| 123 |
n_templates = 3 |
| 124 |
n_template_nodes = 3 |
| 125 |
n_nodes = 10 |
| 126 |
n_features = 3 |
| 127 |
|
| 128 |
torch.manual_seed(0) |
| 129 |
|
| 130 |
C1 = torch.randint(0, 2, size=(n_nodes, n_nodes)) |
| 131 |
edge_index1 = torch.stack(torch.where(C1 == 1)) |
| 132 |
x1 = torch.rand(n_nodes, n_features) |
| 133 |
graph1 = GraphData(x=x1, edge_index=edge_index1, y=torch.tensor([0.0])) |
| 134 |
batch1 = torch.tensor([1] * n_nodes) |
| 135 |
batch1[: n_nodes // 2] = 0 |
| 136 |
|
| 137 |
criterion = torch.nn.CrossEntropyLoss() |
| 138 |
|
| 139 |
for train_node_weights in [True, False]: |
| 140 |
for alpha in [None, 0, 0.5]: |
| 141 |
for multi_alpha in [True, False]: |
| 142 |
model = GNN_pooling( |
| 143 |
n_features, |
| 144 |
n_templates, |
| 145 |
n_template_nodes, |
| 146 |
pooling_layer=TFGWPooling( |
| 147 |
n_templates, |
| 148 |
n_template_nodes, |
| 149 |
n_features, |
| 150 |
alpha=alpha, |
| 151 |
multi_alpha=multi_alpha, |
| 152 |
train_node_weights=train_node_weights, |
| 153 |
), |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
out1 = model(graph1.x, graph1.edge_index) |
| 158 |
loss = criterion(out1, graph1.y) |
| 159 |
loss.backward() |
| 160 |
|
| 161 |
|
| 162 |
out1 = model(graph1.x, graph1.edge_index, batch1) |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.skipif(not torch_geometric, reason="pytorch_geometric not installed") |
| 166 |
def test_TW_variants(): |
| 167 |
|
| 168 |
|
| 169 |
class GNN_pooling(nn.Module): |
| 170 |
""" |
| 171 |
Pooling architecture using the TW layer. |
| 172 |
""" |
| 173 |
|
| 174 |
def __init__(self, n_features, n_templates, n_template_nodes, pooling_layer): |
| 175 |
""" |
| 176 |
Pooling architecture using the TW layer. |
| 177 |
""" |
| 178 |
super().__init__() |
| 179 |
|
| 180 |
self.n_features = n_features |
| 181 |
self.n_templates = n_templates |
| 182 |
self.n_template_nodes = n_template_nodes |
| 183 |
|
| 184 |
self.TFGW = pooling_layer |
| 185 |
|
| 186 |
self.linear = Linear(self.n_templates, 1) |
| 187 |
|
| 188 |
def forward(self, x, edge_index, batch=None): |
| 189 |
x = self.TFGW(x, edge_index, batch=batch) |
| 190 |
|
| 191 |
x = self.linear(x) |
| 192 |
|
| 193 |
return x |
| 194 |
|
| 195 |
n_templates = 3 |
| 196 |
n_template_nodes = 3 |
| 197 |
n_nodes = 10 |
| 198 |
n_features = 3 |
| 199 |
|
| 200 |
torch.manual_seed(0) |
| 201 |
|
| 202 |
C1 = torch.randint(0, 2, size=(n_nodes, n_nodes)) |
| 203 |
edge_index1 = torch.stack(torch.where(C1 == 1)) |
| 204 |
x1 = torch.rand(n_nodes, n_features) |
| 205 |
graph1 = GraphData(x=x1, edge_index=edge_index1, y=torch.tensor([0.0])) |
| 206 |
batch1 = torch.tensor([1] * n_nodes) |
| 207 |
batch1[: n_nodes // 2] = 0 |
| 208 |
|
| 209 |
criterion = torch.nn.CrossEntropyLoss() |
| 210 |
|
| 211 |
for train_node_weights in [True, False]: |
| 212 |
model = GNN_pooling( |
| 213 |
n_features, |
| 214 |
n_templates, |
| 215 |
n_template_nodes, |
| 216 |
pooling_layer=TWPooling( |
| 217 |
n_templates, |
| 218 |
n_template_nodes, |
| 219 |
n_features, |
| 220 |
train_node_weights=train_node_weights, |
| 221 |
), |
| 222 |
) |
| 223 |
|
| 224 |
out1 = model(graph1.x, graph1.edge_index) |
| 225 |
loss = criterion(out1, graph1.y) |
| 226 |
loss.backward() |
| 227 |
|
| 228 |
|
| 229 |
out1 = model(graph1.x, graph1.edge_index, batch1) |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.skipif(not torch_geometric, reason="pytorch_geometric not installed") |
| 233 |
def test_TW(): |
| 234 |
|
| 235 |
|
| 236 |
class pooling_TW(nn.Module): |
| 237 |
""" |
| 238 |
Pooling architecture using the TW layer. |
| 239 |
""" |
| 240 |
|
| 241 |
def __init__(self, n_features, n_templates, n_template_nodes): |
| 242 |
""" |
| 243 |
Pooling architecture using the TW layer. |
| 244 |
""" |
| 245 |
super().__init__() |
| 246 |
|
| 247 |
self.n_features = n_features |
| 248 |
self.n_templates = n_templates |
| 249 |
self.n_template_nodes = n_template_nodes |
| 250 |
|
| 251 |
self.TFGW = TWPooling( |
| 252 |
self.n_templates, self.n_template_nodes, self.n_features |
| 253 |
) |
| 254 |
|
| 255 |
self.linear = Linear(self.n_templates, 1) |
| 256 |
|
| 257 |
def forward(self, x, edge_index): |
| 258 |
x = self.TFGW(x, edge_index) |
| 259 |
|
| 260 |
x = self.linear(x) |
| 261 |
|
| 262 |
return x |
| 263 |
|
| 264 |
torch.manual_seed(0) |
| 265 |
|
| 266 |
n_templates = 3 |
| 267 |
n_template_nodes = 3 |
| 268 |
n_nodes = 10 |
| 269 |
n_features = 3 |
| 270 |
n_epochs = 3 |
| 271 |
|
| 272 |
C1 = torch.randint(0, 2, size=(n_nodes, n_nodes)) |
| 273 |
C2 = torch.randint(0, 2, size=(n_nodes, n_nodes)) |
| 274 |
|
| 275 |
edge_index1 = torch.stack(torch.where(C1 == 1)) |
| 276 |
edge_index2 = torch.stack(torch.where(C2 == 1)) |
| 277 |
|
| 278 |
x1 = torch.rand(n_nodes, n_features) |
| 279 |
x2 = torch.rand(n_nodes, n_features) |
| 280 |
|
| 281 |
graph1 = GraphData(x=x1, edge_index=edge_index1, y=torch.tensor([0.0])) |
| 282 |
graph2 = GraphData(x=x2, edge_index=edge_index2, y=torch.tensor([1.0])) |
| 283 |
|
| 284 |
dataset = DataLoader([graph1, graph2], batch_size=1) |
| 285 |
|
| 286 |
model_W = pooling_TW(n_features, n_templates, n_template_nodes) |
| 287 |
|
| 288 |
optimizer = torch.optim.Adam(model_W.parameters(), lr=0.01) |
| 289 |
criterion = torch.nn.CrossEntropyLoss() |
| 290 |
|
| 291 |
model_W.train() |
| 292 |
|
| 293 |
for i in range(n_epochs): |
| 294 |
for data in dataset: |
| 295 |
out = model_W(data.x, data.edge_index) |
| 296 |
loss = criterion(out, data.y) |
| 297 |
loss.backward() |
| 298 |
optimizer.step() |
| 299 |
optimizer.zero_grad() |