()
| 231 | |
| 232 | @pytest.mark.skipif(not torch_geometric, reason="pytorch_geometric not installed") |
| 233 | def test_TW(): |
| 234 | # Test the TW layer by passing two graphs through the layer and doing backpropagation. |
| 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 |
nothing calls this directly
no test coverage detected