Spaces:
Sleeping
Sleeping
File size: 1,427 Bytes
f018cee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import torch
import torch.nn.functional as F
from torch_geometric.nn import SAGEConv
class ClinicalTwinGNN(torch.nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels_recurrence=2):
super(ClinicalTwinGNN, self).__init__()
# Encoder: GraphSAGE
self.conv1 = SAGEConv(in_channels, hidden_channels)
self.conv2 = SAGEConv(hidden_channels, hidden_channels)
# Dual Heads
# Head 1: Recurrence (Binary Classification)
self.recurrence_head = torch.nn.Linear(hidden_channels, out_channels_recurrence)
# Head 2: Survival (Regression/Probability - 0 to 1)
self.survival_head = torch.nn.Sequential(
torch.nn.Linear(hidden_channels, hidden_channels // 2),
torch.nn.ReLU(),
torch.nn.Linear(hidden_channels // 2, 1),
torch.nn.Sigmoid()
)
def forward(self, x, edge_index, edge_weight=None):
# Feature aggregation
x = self.conv1(x, edge_index)
x = F.relu(x)
x = F.dropout(x, p=0.2, training=self.training)
x = self.conv2(x, edge_index)
x = F.relu(x)
# Latent representation for explanations
self.latent = x
# Heads
recurrence_out = self.recurrence_head(x)
survival_out = self.survival_head(x)
return recurrence_out, survival_out
|