Spaces:
Runtime error
Runtime error
Download models.py from AntonyManojXavier/TestSpace: direct link, hf CLI and curl.
- Browser
- Download file 2.07 kB
-
https://huggingface.co/spaces/AntonyManojXavier/TestSpace/resolve/main/models.py
- Command line
-
hf download hf://spaces/AntonyManojXavier/TestSpace/models.py
-
curl -L -o models.py https://huggingface.co/spaces/AntonyManojXavier/TestSpace/resolve/main/models.py
2.07 kB
| import torch | |
| import torch.nn as nn | |
| from dgl.nn.pytorch import GraphConv, SumPooling | |
| from dgl.nn.pytorch import EdgeWeightNorm | |
| class ModifiedGCN(nn.Module): | |
| def __init__(self, input_dim, edge_dim, output_dim): | |
| super(ModifiedGCN, self).__init__() | |
| hidden_dims = [32, 32, 32, 16] | |
| self.conv1 = GraphConv(input_dim, hidden_dims[0], allow_zero_in_degree=True) | |
| self.bn1 = nn.BatchNorm1d(hidden_dims[0]) | |
| self.conv2 = GraphConv(hidden_dims[0], hidden_dims[1], allow_zero_in_degree=True) | |
| self.bn2 = nn.BatchNorm1d(hidden_dims[1]) | |
| self.conv3 = GraphConv(hidden_dims[1], hidden_dims[2], allow_zero_in_degree=True) | |
| self.bn3 = nn.BatchNorm1d(hidden_dims[2]) | |
| self.conv4 = GraphConv(hidden_dims[2], hidden_dims[3], allow_zero_in_degree=True) | |
| self.bn4 = nn.BatchNorm1d(hidden_dims[3]) | |
| self.pool = SumPooling() | |
| self.classify = nn.Linear(hidden_dims[3] + edge_dim, output_dim) # Concatenate edge features | |
| # Define activations for each layer | |
| self.elu1 = nn.ELU() | |
| self.elu2 = nn.ELU() | |
| self.elu3 = nn.ELU() | |
| self.elu4 = nn.ELU() | |
| self.edge_weight_norm = EdgeWeightNorm() | |
| def forward(self, g, features, edge_features, edge_weights): | |
| edge_weights = torch.abs(edge_weights) # Ensure all edge weights are positive | |
| edge_weights = self.edge_weight_norm(g, edge_weights) | |
| x = self.elu1(self.bn1(self.conv1(g, features, edge_weight=edge_weights))) | |
| x = self.elu2(self.bn2(self.conv2(g, x, edge_weight=edge_weights))) | |
| x = self.elu3(self.bn3(self.conv3(g, x, edge_weight=edge_weights))) | |
| x = self.elu4(self.bn4(self.conv4(g, x, edge_weight=edge_weights))) | |
| x = self.pool(g, x) | |
| # Concatenate pooled node features with edge features | |
| edge_mean = torch.mean(edge_features, dim=0).unsqueeze(0) # Add an extra dimension | |
| edge_mean = edge_mean.repeat(x.shape[0], 1) # Repeat to match the batch size | |
| x = torch.cat([x, edge_mean], dim=1) | |
| return self.classify(x) |