Пример #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
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)