Created
May 28, 2019 14:49
-
-
Save khuangaf/b4437feac0f03fd4870a28a3f0dca9a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
embed_dim = 128 | |
from torch_geometric.nn import GraphConv, TopKPooling, GatedGraphConv | |
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp | |
import torch.nn.functional as F | |
class Net(torch.nn.Module): | |
def __init__(self): | |
super(Net, self).__init__() | |
self.conv1 = GraphConv(embed_dim, 128, aggr='max') | |
self.pool1 = TopKPooling(128, ratio=0.8) | |
self.conv2 = GraphConv(128, 128, aggr='max') | |
self.pool2 = TopKPooling(128, ratio=0.8) | |
self.conv3 = GraphConv(128, 128, aggr='max') | |
self.pool3 = TopKPooling(128, ratio=0.8) | |
self.item_embedding = torch.nn.Embedding(num_embeddings=df.item_id.max() +1, embedding_dim=embed_dim) | |
self.lin1 = torch.nn.Linear(256, 128) | |
self.lin2 = torch.nn.Linear(128, 64) | |
self.lin3 = torch.nn.Linear(64, 1) | |
self.bn1 = torch.nn.BatchNorm1d(128) | |
self.bn2 = torch.nn.BatchNorm1d(64) | |
self.act1 = torch.nn.ReLU() | |
self.act2 = torch.nn.ReLU() | |
def forward(self, data): | |
x, edge_index, batch = data.x, data.edge_index, data.batch | |
x = self.item_embedding(x) | |
x = x.squeeze(1) | |
x = F.relu(self.conv1(x, edge_index)) | |
x, edge_index, _, batch, _ = self.pool1(x, edge_index, None, batch) | |
x1 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1) | |
x = F.relu(self.conv2(x, edge_index)) | |
x, edge_index, _, batch, _ = self.pool2(x, edge_index, None, batch) | |
x2 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1) | |
x = F.relu(self.conv3(x, edge_index)) | |
x, edge_index, _, batch, _ = self.pool3(x, edge_index, None, batch) | |
x3 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1) | |
x = x1 + x2 + x3 | |
x = self.lin1(x) | |
x = self.act1(x) | |
x = self.lin2(x) | |
x = self.act2(x) | |
x = F.dropout(x, p=0.5, training=self.training) | |
x = torch.sigmoid(self.lin3(x)).squeeze(1) | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment