|
| 1 | +from gns.graph_network import * |
| 2 | +import torch |
| 3 | +from torch_geometric.data import Data |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def interaction_network_data(): |
| 9 | + model = InteractionNetwork( |
| 10 | + nnode_in=2, |
| 11 | + nnode_out=2, |
| 12 | + nedge_in=2, |
| 13 | + nedge_out=2, |
| 14 | + nmlp_layers=2, |
| 15 | + mlp_hidden_dim=2, |
| 16 | + ) |
| 17 | + edge_index = torch.tensor([[0, 1], [1, 0]], dtype=torch.long) |
| 18 | + x = torch.tensor([[1, 2], [3, 4]], dtype=torch.float) # node features |
| 19 | + edge_attr = torch.tensor([[1, 1], [2, 2]], dtype=torch.float) # edge features |
| 20 | + |
| 21 | + return model, x, edge_index, edge_attr |
| 22 | + |
| 23 | + |
| 24 | +def test_edge_update(interaction_network_data): |
| 25 | + """Test if edge features are updated and finite and are not simply doubled""" |
| 26 | + model, x, edge_index, edge_attr = interaction_network_data |
| 27 | + old_edge_attr = edge_attr.clone() # Save the old edge features |
| 28 | + |
| 29 | + # One message passing step |
| 30 | + _, updated_edge_attr = model(x=x, edge_index=edge_index, edge_features=edge_attr) |
| 31 | + |
| 32 | + # Check if edge features shape is correct |
| 33 | + assert ( |
| 34 | + edge_attr.shape == old_edge_attr.shape |
| 35 | + ), f"Edge features shape is not preserved, changed from {old_edge_attr.shape} to {edge_attr.shape}" |
| 36 | + # Check if edge features are updated |
| 37 | + assert not torch.equal( |
| 38 | + updated_edge_attr, old_edge_attr * 2 |
| 39 | + ), "Edge features are simply doubled" |
| 40 | + assert not torch.equal( |
| 41 | + updated_edge_attr, old_edge_attr |
| 42 | + ), "Edge features are not updated" |
| 43 | + # Check if edge features are finite |
| 44 | + assert torch.all(torch.isfinite(edge_attr)), "Edge features are not finite" |
| 45 | + |
| 46 | + |
| 47 | +def test_gradients_computed(interaction_network_data): |
| 48 | + """Test if gradients are computed and finite""" |
| 49 | + model, x, edge_index, edge_attr = interaction_network_data |
| 50 | + x.requires_grad = True |
| 51 | + edge_attr.requires_grad = True |
| 52 | + |
| 53 | + # First pass |
| 54 | + aggr, updated_edge_features = model( |
| 55 | + x=x, edge_index=edge_index, edge_features=edge_attr |
| 56 | + ) |
| 57 | + updated_node_features = x + aggr |
| 58 | + # Second pass |
| 59 | + aggr, updated_edge_features = model( |
| 60 | + x=updated_node_features, |
| 61 | + edge_index=edge_index, |
| 62 | + edge_features=updated_edge_features, |
| 63 | + ) |
| 64 | + updated_node_features = updated_node_features + aggr |
| 65 | + # Compute loss |
| 66 | + loss = (updated_edge_features).sum() |
| 67 | + loss.backward() |
| 68 | + |
| 69 | + # Check if gradients are computed |
| 70 | + assert x.grad is not None, "Gradients for node features are not computed" |
| 71 | + assert edge_attr.grad is not None, "Gradients for edge features are not computed" |
| 72 | + # Check if gradients are finite |
| 73 | + assert torch.all( |
| 74 | + torch.isfinite(x.grad) |
| 75 | + ), "Gradients for node features are not finite" |
| 76 | + assert torch.all( |
| 77 | + torch.isfinite(edge_attr.grad) |
| 78 | + ), "Gradients for edge features are not finite" |
0 commit comments