File size: 1,097 Bytes
340a9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.data import Data

class MedicalGNN(nn.Module):
    def __init__(self, in_channels: int, hidden_channels: int = 256):
        super().__init__()
        self.conv1 = GCNConv(in_channels, hidden_channels)
        self.conv2 = GCNConv(hidden_channels, in_channels)
        self.dropout = nn.Dropout(p=0.3)

    def forward(self, x: torch.tensor, edge_index: torch.tensor) -> torch.tensor:
        h = self.conv1(x, edge_index)
        h = F.relu(h)
        h = self.dropout(h)
        h = self.conv2(h, edge_index)
        return h
    
def get_structural_embeddings(pyg_data: Data, in_channels: int, hidden_channels: int = 256) -> torch.tensor:
    model = MedicalGNN(in_channels = in_channels, hidden_channels = hidden_channels)
    model.eval()
    with torch.no_grad():
        structural_embs = model(pyg_data.x, pyg_data.edge_index)
        
    print(f"GNN message passing finish: shape={structural_embs.shape}")
    return structural_embs