def __init__(self, in_channels, hidden_channels, num_layers, lamb=5, bias=True): super(SignedGCN, self).__init__() self.in_channels = in_channels self.hidden_channels = hidden_channels self.num_layers = num_layers self.lamb = lamb self.conv1 = SignedConv(in_channels, hidden_channels // 2, first_aggr=True) self.convs = torch.nn.ModuleList() for i in range(num_layers - 1): self.convs.append( SignedConv(hidden_channels // 2, hidden_channels // 2, first_aggr=False)) self.lin = torch.nn.Linear(2 * hidden_channels, 3) self.reset_parameters()
def __init__(self, num_features, n_classes, num_hidden, num_hidden_layers, dropout, activation, first_aggr='add', bias=True): super(PSigned, self).__init__() # dropout if dropout: self.dropout = nn.Dropout(p=dropout) else: self.dropout = nn.Dropout(p=0.) #activation self.activation = activation # input layer self.conv_input = SignedConv(num_features, num_hidden, first_aggr=first_aggr, bias=bias) # Hidden layers self.layers = nn.ModuleList() for _ in range(num_hidden_layers): self.layers.append( SignedConv(num_hidden, num_hidden, first_aggr=first_aggr, bias=bias)) # output layer self.conv_output = SignedConv(num_hidden, n_classes, first_aggr=first_aggr, bias=bias)
def test_signed_conv(): in_channels, out_channels = (16, 32) pos_ei = torch.tensor([[0, 0, 0, 1, 2, 3], [1, 2, 3, 0, 0, 0]]) neg_ei = torch.tensor([[0, 0, 0, 1, 2, 3], [1, 2, 3, 0, 0, 0]]) num_nodes = pos_ei.max().item() + 1 x = torch.randn((num_nodes, in_channels)) conv = SignedConv(in_channels, out_channels, first_aggr=True) assert conv.__repr__() == 'SignedConv(16, 32, first_aggr=True)' out1 = conv(x, pos_ei, neg_ei) assert out1.size() == (num_nodes, 2 * out_channels) jit_conv = conv.jittable(x=x, pos_edge_index=pos_ei, neg_edge_index=neg_ei) jit_conv = torch.jit.script(jit_conv) assert jit_conv(x, pos_ei, neg_ei).tolist() == out1.tolist() conv = SignedConv(out_channels, out_channels, first_aggr=False) assert conv.__repr__() == 'SignedConv(32, 32, first_aggr=False)' out2 = conv(out1, pos_ei, neg_ei) assert out2.size() == (num_nodes, 2 * out_channels) jit_conv = conv.jittable(x=out1, pos_edge_index=pos_ei, neg_edge_index=neg_ei) jit_conv = torch.jit.script(jit_conv) assert jit_conv(out1, pos_ei, neg_ei).tolist() == out2.tolist()
def test_signed_conv(): in_channels, out_channels = (16, 32) pos_ei = torch.tensor([[0, 0, 0, 1, 2, 3], [1, 2, 3, 0, 0, 0]]) neg_ei = torch.tensor([[0, 0, 0, 1, 2, 3], [1, 2, 3, 0, 0, 0]]) num_nodes = pos_ei.max().item() + 1 x = torch.randn((num_nodes, in_channels)) conv = SignedConv(in_channels, out_channels, first_aggr=True) assert conv.__repr__() == 'SignedConv(16, 32, first_aggr=True)' x = conv(x, pos_ei, neg_ei) assert x.size() == (num_nodes, 2 * out_channels) conv = SignedConv(out_channels, out_channels, first_aggr=False) assert conv.__repr__() == 'SignedConv(32, 32, first_aggr=False)' assert conv(x, pos_ei, neg_ei).size() == (num_nodes, 2 * out_channels)
class SignedGCN(torch.nn.Module): def __init__(self, in_channels, hidden_channels, num_layers, lamb, bias=True): super(SignedGCN, self).__init__() self.in_channels = in_channels self.conv1 = SignedConv(in_channels, hidden_channels // 2, first_aggr=True) self.convs = torch.nn.ModuleList() for i in range(num_layers - 1): self.convs.append( SignedConv(hidden_channels // 2, hidden_channels // 2, first_aggr=False)) self.lin = torch.nn.Linear(2 * hidden_channels, 1) self.reset_parameters() def reset_parameters(self): self.conv1.reset_parameters() for conv in self.convs: conv.reset_parameters() self.lin.reset_parameters() def create_spectral_features(self, pos_edge_index, neg_edge_index): edge_index = torch.cat([pos_edge_index, neg_edge_index], dim=1) N = edge_index.max().item() + 1 edge_index = edge_index.to(torch.device('cpu')).detach().numpy() pos_val = torch.full((pos_edge_index.size(1), ), 1, dtype=torch.float) neg_val = torch.full((neg_edge_index.size(1), ), -1, dtype=torch.float) val = torch.cat([pos_val, neg_val], dim=0) val = val.to(torch.device('cpu')).detach().numpy() # Borrowed from: # https://github.com/benedekrozemberczki/SGCN/blob/master/src/utils.py A = scipy.sparse.coo_matrix((val, edge_index), shape=(N, N)) svd = TruncatedSVD(n_components=self.in_channels, n_iter=128) svd.fit(A) x = svd.components_.T return torch.from_numpy(x).to(torch.float).to(pos_edge_index.device) def forward(self, x, pos_edge_index, neg_edge_index): """""" x = F.relu(self.conv1(x, pos_edge_index, neg_edge_index)) for conv in self.convs: x = F.relu(conv(x, pos_edge_index, neg_edge_index)) return x def discriminate(self, z, edge_index, sigmoid=True): value = torch.cat([z[edge_index[0]], z[edge_index[1]]], dim=1) value = self.lin(value).view(-1) return torch.sigmoid(value) if sigmoid else value def loss(self, z, pos_edge_index, neg_edge_index): pass def test(self, z, pos_edge_index, neg_edge_index): pass
class SignedGCN(torch.nn.Module): r"""The signed graph convolutional network model from the `"Signed Graph Convolutional Network" <https://arxiv.org/abs/1808.06354>`_ paper. Internally, this module uses the :class:`torch_geometric.nn.conv.SignedConv` operator. Args: in_channels (int): Size of each input sample. out_channels (int): Size of each output sample. num_layers (int): Number of layers. lamb (float, optional): Balances the contributions of the overall objective. (default: :obj:`5`) bias (bool, optional): If set to :obj:`False`, all layers will not learn an additive bias. (default: :obj:`True`) """ def __init__(self, in_channels, hidden_channels, num_layers, lamb=5, bias=True): super(SignedGCN, self).__init__() self.in_channels = in_channels self.lamb = lamb self.conv1 = SignedConv(in_channels, hidden_channels // 2, first_aggr=True) self.convs = torch.nn.ModuleList() for i in range(num_layers - 1): self.convs.append( SignedConv(hidden_channels // 2, hidden_channels // 2, first_aggr=False)) self.lin = torch.nn.Linear(2 * hidden_channels, 3) self.reset_parameters() def reset_parameters(self): self.conv1.reset_parameters() for conv in self.convs: conv.reset_parameters() self.lin.reset_parameters() def split_edges(self, edge_index, test_ratio=0.2): r"""Splits the edges :obj:`edge_index` into train and test edges. Args: edge_index (LongTensor): The edge indices. test_ratio (float, optional): The ratio of test edges. (default: :obj:`0.2`) """ mask = torch.ones(edge_index.size(1), dtype=torch.uint8) mask[torch.randperm(mask.size(0))[:int(test_ratio * mask.size(0))]] = 0 train_edge_index = edge_index[:, mask] test_edge_index = edge_index[:, 1 - mask] return train_edge_index, test_edge_index def create_spectral_features(self, pos_edge_index, neg_edge_index, num_nodes=None): r"""Creates :obj:`in_channels` spectral node features based on positive and negative edges. Args: pos_edge_index (LongTensor): The positive edge indices. neg_edge_index (LongTensor): The negative edge indices. num_nodes (int, optional): The number of nodes, *i.e.* :obj:`max_val + 1` of :attr:`pos_edge_index` and :attr:`neg_edge_index`. (default: :obj:`None`) """ edge_index = torch.cat([pos_edge_index, neg_edge_index], dim=1) N = edge_index.max().item() + 1 if num_nodes is None else num_nodes edge_index = edge_index.to(torch.device('cpu')) pos_val = torch.full((pos_edge_index.size(1), ), 2, dtype=torch.float) neg_val = torch.full((neg_edge_index.size(1), ), 0, dtype=torch.float) val = torch.cat([pos_val, neg_val], dim=0) row, col = edge_index edge_index = torch.cat([edge_index, torch.stack([col, row])], dim=1) val = torch.cat([val, val], dim=0) edge_index, val = coalesce(edge_index, val, N, N) val = val - 1 # Borrowed from: # https://github.com/benedekrozemberczki/SGCN/blob/master/src/utils.py edge_index = edge_index.detach().numpy() val = val.detach().numpy() A = scipy.sparse.coo_matrix((val, edge_index), shape=(N, N)) svd = TruncatedSVD(n_components=self.in_channels, n_iter=128) svd.fit(A) x = svd.components_.T return torch.from_numpy(x).to(torch.float).to(pos_edge_index.device) def forward(self, x, pos_edge_index, neg_edge_index): """Computes node embeddings :obj:`z` based on positive edges :obj:`pos_edge_index` and negative edges :obj:`neg_edge_index`. Args: x (Tensor): The input node features. pos_edge_index (LongTensor): The positive edge indices. neg_edge_index (LongTensor): The negative edge indices. """ z = F.relu(self.conv1(x, pos_edge_index, neg_edge_index)) for conv in self.convs: z = F.relu(conv(z, pos_edge_index, neg_edge_index)) return z def discriminate(self, z, edge_index): """Given node embeddings :obj:`z`, classifies the link relation between node pairs :obj:`edge_index` to be either positive, negative or non-existent. Args: x (Tensor): The input node features. edge_index (LongTensor): The edge indices. """ value = torch.cat([z[edge_index[0]], z[edge_index[1]]], dim=1) value = self.lin(value) return torch.log_softmax(value, dim=1) def nll_loss(self, z, pos_edge_index, neg_edge_index): """Computes the discriminator loss based on node embeddings :obj:`z`, and positive edges :obj:`pos_edge_index` and negative nedges :obj:`neg_edge_index`. Args: z (Tensor): The node embeddings. pos_edge_index (LongTensor): The positive edge indices. neg_edge_index (LongTensor): The negative edge indices. """ edge_index = torch.cat([pos_edge_index, neg_edge_index], dim=1) none_edge_index = negative_sampling(edge_index, z.size(0)) nll_loss = 0 nll_loss += F.nll_loss( self.discriminate(z, pos_edge_index), pos_edge_index.new_full((pos_edge_index.size(1), ), 0)) nll_loss += F.nll_loss( self.discriminate(z, neg_edge_index), neg_edge_index.new_full((neg_edge_index.size(1), ), 1)) nll_loss += F.nll_loss( self.discriminate(z, none_edge_index), none_edge_index.new_full((none_edge_index.size(1), ), 2)) return nll_loss / 3.0 def pos_embedding_loss(self, z, pos_edge_index): """Computes the triplet loss between positive node pairs and sampled non-node pairs. Args: z (Tensor): The node embeddings. pos_edge_index (LongTensor): The positive edge indices. """ i, j, k = negative_triangle_sampling(pos_edge_index, z.size(0)) out = (z[i] - z[j]).pow(2).sum(dim=1) - (z[i] - z[k]).pow(2).sum(dim=1) return torch.clamp(out, min=0).mean() def neg_embedding_loss(self, z, neg_edge_index): """Computes the triplet loss between negative node pairs and sampled non-node pairs. Args: z (Tensor): The node embeddings. neg_edge_index (LongTensor): The negative edge indices. """ i, j, k = negative_triangle_sampling(neg_edge_index, z.size(0)) out = (z[i] - z[k]).pow(2).sum(dim=1) - (z[i] - z[j]).pow(2).sum(dim=1) return torch.clamp(out, min=0).mean() def loss(self, z, pos_edge_index, neg_edge_index): """Computes the overall objective. Args: z (Tensor): The node embeddings. pos_edge_index (LongTensor): The positive edge indices. neg_edge_index (LongTensor): The negative edge indices. """ nll_loss = self.nll_loss(z, pos_edge_index, neg_edge_index) loss_1 = self.pos_embedding_loss(z, pos_edge_index) loss_2 = self.neg_embedding_loss(z, neg_edge_index) return nll_loss + self.lamb * (loss_1 + loss_2) def test(self, z, pos_edge_index, neg_edge_index): """Evaluates node embeddings :obj:`z` on positive and negative test edges by computing AUC and F1 scores. Args: z (Tensor): The node embeddings. pos_edge_index (LongTensor): The positive edge indices. neg_edge_index (LongTensor): The negative edge indices. """ with torch.no_grad(): pos_p = self.discriminate(z, pos_edge_index)[:, :2].max(dim=1)[1] neg_p = self.discriminate(z, neg_edge_index)[:, :2].max(dim=1)[1] pred = (1 - torch.cat([pos_p, neg_p])).cpu() y = torch.cat( [pred.new_ones((pos_p.size(0))), pred.new_zeros(neg_p.size(0))]) pred, y = pred.numpy(), y.numpy() auc = roc_auc_score(y, pred) f1 = f1_score(y, pred, average='binary') if pred.sum() > 0 else 0 return auc, f1
def test_signed_conv(): x = torch.randn(4, 16) edge_index = torch.tensor([[0, 1, 2, 3], [0, 0, 1, 1]]) row, col = edge_index adj = SparseTensor(row=row, col=col, sparse_sizes=(4, 4)) conv1 = SignedConv(16, 32, first_aggr=True) assert conv1.__repr__() == 'SignedConv(16, 32, first_aggr=True)' conv2 = SignedConv(32, 48, first_aggr=False) assert conv2.__repr__() == 'SignedConv(32, 48, first_aggr=False)' out1 = conv1(x, edge_index, edge_index) assert out1.size() == (4, 64) assert conv1(x, adj.t(), adj.t()).tolist() == out1.tolist() out2 = conv2(out1, edge_index, edge_index) assert out2.size() == (4, 96) assert conv2(out1, adj.t(), adj.t()).tolist() == out2.tolist() if is_full_test(): t = '(Tensor, Tensor, Tensor) -> Tensor' jit1 = torch.jit.script(conv1.jittable(t)) jit2 = torch.jit.script(conv2.jittable(t)) assert jit1(x, edge_index, edge_index).tolist() == out1.tolist() assert jit2(out1, edge_index, edge_index).tolist() == out2.tolist() t = '(Tensor, SparseTensor, SparseTensor) -> Tensor' jit1 = torch.jit.script(conv1.jittable(t)) jit2 = torch.jit.script(conv2.jittable(t)) assert jit1(x, adj.t(), adj.t()).tolist() == out1.tolist() assert jit2(out1, adj.t(), adj.t()).tolist() == out2.tolist() adj = adj.sparse_resize((4, 2)) assert torch.allclose(conv1((x, x[:2]), edge_index, edge_index), out1[:2], atol=1e-6) assert torch.allclose(conv1((x, x[:2]), adj.t(), adj.t()), out1[:2], atol=1e-6) assert torch.allclose(conv2((out1, out1[:2]), edge_index, edge_index), out2[:2], atol=1e-6) assert torch.allclose(conv2((out1, out1[:2]), adj.t(), adj.t()), out2[:2], atol=1e-6) if is_full_test(): t = '(PairTensor, Tensor, Tensor) -> Tensor' jit1 = torch.jit.script(conv1.jittable(t)) jit2 = torch.jit.script(conv2.jittable(t)) assert torch.allclose(jit1((x, x[:2]), edge_index, edge_index), out1[:2], atol=1e-6) assert torch.allclose(jit2((out1, out1[:2]), edge_index, edge_index), out2[:2], atol=1e-6) t = '(PairTensor, SparseTensor, SparseTensor) -> Tensor' jit1 = torch.jit.script(conv1.jittable(t)) jit2 = torch.jit.script(conv2.jittable(t)) assert torch.allclose(jit1((x, x[:2]), adj.t(), adj.t()), out1[:2], atol=1e-6) assert torch.allclose(jit2((out1, out1[:2]), adj.t(), adj.t()), out2[:2], atol=1e-6)