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 43 44 45 46 47 48 49 50 51
|
from typing import Optional
import torch
import torch.nn.functional as F
from torch import Tensor
from torch.nn import BatchNorm1d, Linear, ReLU, Sequential
from torch_geometric.nn import GINConv, global_mean_pool
class GIN(torch.nn.Module):
def __init__(self, in_channels: int, hidden_channels: int,
out_channels: int, num_layers: int):
super().__init__()
self.convs = torch.nn.ModuleList()
for _ in range(num_layers):
mlp = Sequential(
Linear(in_channels, hidden_channels),
BatchNorm1d(hidden_channels),
ReLU(),
Linear(hidden_channels, hidden_channels),
)
self.convs.append(GINConv(mlp))
in_channels = hidden_channels
self.lin1 = Linear(hidden_channels, hidden_channels)
self.lin2 = Linear(hidden_channels, out_channels)
def forward(
self,
x: Tensor,
edge_index: Tensor,
batch: Optional[Tensor] = None,
) -> Tensor:
for conv in self.convs:
x = conv(x, edge_index).relu()
x = global_mean_pool(x, batch)
x = self.lin1(x).relu()
x = F.dropout(x, p=0.5, training=self.training)
x = self.lin2(x)
return x
model = GIN(32, 64, 16, num_layers=3)
model = torch.jit.script(model)
model.save('model.pt')
|