Ejemplo n.º 1
0
def track_time(graph_name, format, k):
    device = utils.get_bench_device()
    graph = utils.get_graph(graph_name, format)
    graph = graph.to(device)
    graph = graph.formats([format])
    # dry run
    dgl.khop_graph(graph, k)

    # timing
    with utils.Timer() as t:
        for i in range(10):
            gg = dgl.khop_graph(graph, k)

    return t.elapsed_secs / 10
Ejemplo n.º 2
0
def track_time(graph_name, format, k):
    device = utils.get_bench_device()
    graph = utils.get_graph(graph_name, format)
    graph = graph.to(device)
    graph = graph.formats([format])
    # dry run
    dgl.khop_graph(graph, k)

    # timing
    t0 = time.time()
    for i in range(10):
        gg = dgl.khop_graph(graph, k)
    t1 = time.time()

    return (t1 - t0) / 10
Ejemplo n.º 3
0
def calculate_homophily(g,
                        labels,
                        K=1,
                        method="edge",
                        multilabels=False,
                        heterograph=False):
    assert method in ["edge", "node"]
    if multilabels:
        assert len(labels.shape) == 2
    else:
        if (labels.max() == 1) and len(labels.shape) > 1:
            labels = labels.argmax(dim=1)
    if heterograph:
        target_mask = g.ndata['target_mask']
        target_ids = g.ndata[dgl.NID][target_mask]
        num_target = target_mask.sum().item()
        g = g.subgraph(np.arange(g.number_of_nodes())[target_mask])
    g = dgl.khop_graph(g, K)
    src, dst = g.edges()
    # if multilabels:
    #     out = 0
    #     for c in labels.shape[1]:
    #         mask = (labels[src, c])
    mask = (labels[src] == labels[dst]).float()
    if method == "edge":
        out = mask.mean(dim=0)
    elif method == "node":
        g.edata["mask"] = mask
        g.update_all(fn.copy_e("mask", "m"), fn.mean("m", "out"))
        out = g.ndata.pop("out").mean(dim=0)
    # for multilabels, we average homophily across labels

    return out.mean(0).item()
Ejemplo n.º 4
0
 def _test(g):
     for k in range(4):
         g_k = dgl.khop_graph(g, k)
         # use original graph to do message passing for k times.
         g.ndata['h'] = feat
         for _ in range(k):
             g.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
         h_0 = g.ndata.pop('h')
         # use k-hop graph to do message passing for one time.
         g_k.ndata['h'] = feat
         g_k.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
         h_1 = g_k.ndata.pop('h')
         assert F.allclose(h_0, h_1, rtol=1e-3, atol=1e-3)
Ejemplo n.º 5
0
def test_khop_graph():
    N = 20
    feat = F.randn((N, 5))
    g = dgl.DGLGraph(nx.erdos_renyi_graph(N, 0.3))
    for k in range(4):
        g_k = dgl.khop_graph(g, k)
        # use original graph to do message passing for k times.
        g.ndata['h'] = feat
        for _ in range(k):
            g.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
        h_0 = g.ndata.pop('h')
        # use k-hop graph to do message passing for one time.
        g_k.ndata['h'] = feat
        g_k.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'h'))
        h_1 = g_k.ndata.pop('h')
        assert F.allclose(h_0, h_1, rtol=1e-3, atol=1e-3)
Ejemplo n.º 6
0
        logits = model(g, features)
        logits = logits[mask]
        labels = labels[mask]
        _, indices = th.max(logits, dim=1)
        correct = th.sum(indices == labels)
        return correct.item() * 1.0 / len(labels)


# 训练网络
import time
import numpy as np

# g, features, labels, train_mask, test_mask = load_cora_data()

g = dgl.graph(([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 1, 2, 3, 4, 0]))
g = dgl.khop_graph(g, 3)
input = th.ones(32, 5, 1)
net = Net(1, 5)
res = net(g, input)
pass

# # 定义优化器
# optimizer = th.optim.Adam(net.parameters(), lr=1e-3)
# dur = []  # 时间
# for epoch in range(100):
#     print(epoch)
#     if epoch >= 3:
#         t0 = time.time()
#     net.train()
#     logits = net(g, features)
#     logp = F.log_softmax(logits, 1)