Ejemplo n.º 1
0
            torch.save(adj_t, path_sym)
        adj_t = gcn_norm(adj_t, add_self_loops=True)
        torch.save(adj_t, path)
    print(f"Done! [{time.perf_counter() - t:.2f}s]")

    train_idx = torch.from_numpy(dataset.get_idx_split("train"))
    valid_idx = torch.from_numpy(dataset.get_idx_split("valid"))
    test_idx = torch.from_numpy(dataset.get_idx_split("test"))

    y_train = torch.from_numpy(dataset.paper_label[train_idx]).to(torch.long)
    y_valid = torch.from_numpy(dataset.paper_label[valid_idx]).to(torch.long)

    model = CorrectAndSmooth(
        args.num_correction_layers,
        args.correction_alpha,
        args.num_smoothing_layers,
        args.smoothing_alpha,
        autoscale=True,
    )

    t = time.perf_counter()
    print("Correcting predictions...", end=" ", flush=True)
    y_pred = model.correct(y_pred, y_train, train_idx, adj_t)
    print(f"Done! [{time.perf_counter() - t:.2f}s]")

    t = time.perf_counter()
    print("Smoothing predictions...", end=" ", flush=True)
    y_pred = model.smooth(y_pred, y_train, train_idx, adj_t)
    print(f"Done! [{time.perf_counter() - t:.2f}s]")

    train_acc = evaluator.eval({
Ejemplo n.º 2
0
best_val_acc = 0
for epoch in range(1, 301):
    loss = train()
    train_acc, val_acc, test_acc, out = test()
    if val_acc > best_val_acc:
        best_val_acc = val_acc
        y_soft = out.softmax(dim=-1)
    print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}, '
          f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')

adj_t = data.adj_t.to(device)
deg = adj_t.sum(dim=1).to(torch.float)
deg_inv_sqrt = deg.pow_(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
DAD = deg_inv_sqrt.view(-1, 1) * adj_t * deg_inv_sqrt.view(1, -1)
DA = deg_inv_sqrt.view(-1, 1) * deg_inv_sqrt.view(-1, 1) * adj_t

post = CorrectAndSmooth(num_correction_layers=50,
                        correction_alpha=1.0,
                        num_smoothing_layers=50,
                        smoothing_alpha=0.8,
                        autoscale=False,
                        scale=20.)

print('Correct and smooth...')
y_soft = post.correct(y_soft, y_train, train_idx, DAD)
y_soft = post.smooth(y_soft, y_train, train_idx, DA)
print('Done!')
train_acc, val_acc, test_acc, _ = test(y_soft)
print(f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')