コード例 #1
0
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()
コード例 #2
0
ファイル: signed_gcn.py プロジェクト: LONG-9621/SplineCNN
    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()
コード例 #3
0
ファイル: pg_signed.py プロジェクト: adityaketkar/sngnn
 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)
コード例 #4
0
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)
コード例 #5
0
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)