Edge Classification using Graph Neural Networks

31 views Asked by At

I am working on a fraudulent transactions detection in SWIFT network using GGN.My graph has banking codes (SWIFT BIC codes) as nodes and the edges represent transactions. I also added an attribute label = 0 for fraudulent transaction and 1 for non-fraudulent transaction. Every edge has 4 attributes including amount, reason, currency and timestamp. Below are the shapes of my tensors:

  • Node Features Tensor Shape: torch.Size([1, 210, 6])
  • Edge Features Tensor Shape: torch.Size([200, 4])
  • Edge Index Tensor Shape: torch.Size([2, 200])
  • Adjacency Matrix Tensor Shape: torch.Size([1, 210, 210])
  • Labels Tensor Shape: torch.Size([200, 1])

Below is the code to GGN:

class GCNEdgeClassifier(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(GCNEdgeClassifier, self).__init__()
        self.conv1 = GCNConv(input_dim, hidden_dim)
        self.conv2 = GCNConv(hidden_dim, output_dim)
        
    def forward(self, x, edge_index):
        # x: edge features (attributes)
        # edge_index: edge indexes
        
        # Apply first graph convolutional layer
        print("X ", x.shape)
        print("edge_index ",edge_index.shape)
        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = F.dropout(x, p=0.5, training=self.training)
        
        # Apply second graph convolutional layer
        x = self.conv2(x, edge_index)
        
        # Perform edge classification
        # Here we can use a linear layer, sigmoid, or any other activation
        x = torch.sigmoid(x)
        
        return x
# Define the model
input_dim = 4  # Number of edge features
hidden_dim = 64  # Hidden dimension size
output_dim = 1  # Output dimension for edge classification (binary)
model = GCNEdgeClassifier(input_dim, hidden_dim, output_dim)

# Forward pass
predictions = model(edge_features_tensor, edge_indexes_tensor)

# Loss calculation (assuming binary cross-entropy loss)
loss_fn = nn.BCELoss()
labels_tensor = labels_tensor.float()  # Convert labels to float for BCELoss
loss = loss_fn(predictions, labels_tensor)

# Backpropagation and parameter optimization (if training)
loss.backward()
# optimizer.step()  # Update parameters if using an optimizer

I am using as input_dim 4 since I have 4 features in my edge attributes. I keep on getting this error: RuntimeError: index 202 is out of bounds for dimension 0 with size 200

To note that I have 200 edges, thus 200 labels and 200 rows of edge attributes but in my edge_indexes I have reference to 210 nodes, since I have 210 nodes.

I am very new to GGN, however I know that in edge classification we use edge indexes and edge attributes.

Thank you very much for your assistance.

0

There are 0 answers