示例#1
0
def test_given_adj(p_adj: sp.csr_matrix, p_feat: np.ndarray,
                   p_label: np.ndarray, p_train_idx, p_val_idx, p_test_idx):
    """
    Parameters:
        p_adj : csr_matrix


    """
    use_gpu = t.cuda.is_available()
    adj = p_adj.A
    features = p_feat
    labels = p_label

    # surrogate model
    surrogate = GCN(nfeat=features.shape[1],
                    nclass=labels.max().item() + 1,
                    nhid=16,
                    dropout=0,
                    with_relu=True,
                    with_bias=True,
                    device='cuda:0').cuda()
    # train model
    surrogate.fit(features, adj, labels, p_train_idx, p_val_idx, patience=30)

    # test_model
    surrogate.test(p_test_idx)

    return surrogate
示例#2
0
model = GCN(nfeat=features.shape[1],
            nhid=args.hidden,
            nclass=labels.max().item() + 1,
            dropout=args.dropout,
            device=device)

if args.only_gcn:
    perturbed_adj, features, labels = preprocess(perturbed_adj,
                                                 features,
                                                 labels,
                                                 preprocess_adj=False,
                                                 sparse=True,
                                                 device=device)
    model.fit(features,
              perturbed_adj,
              labels,
              idx_train,
              idx_val,
              verbose=True,
              train_iters=args.epochs)
    model.test(idx_test)
else:
    perturbed_adj, features, labels = preprocess(perturbed_adj,
                                                 features,
                                                 labels,
                                                 preprocess_adj=False,
                                                 device=device)
    prognn = ProGNN(model, args, device)
    prognn.fit(features, perturbed_adj, labels, idx_train, idx_val)
    prognn.test(features, labels, idx_test)
示例#3
0
# no defense
model = GCNTorch(nfeat=features.shape[1],
                 nhid=16,
                 nclass=labels.max() + 1,
                 device=device)
model = model.to(device)
model.fit(features,
          nettack.adj_preprocessed,
          labels,
          idx_train,
          idx_val,
          train_iters=200,
          verbose=False,
          normalize=False)
model.eval()
acc = model.test(idx_test)
probs_after_defense = model.predict().detach().cpu().numpy()[u]
preds['no'] = np.argmax(probs_after_defense)

###########################
# vae settings
model = 'arga_vae'  #'arga_ae' or 'arga_vae'
iterations = 500 if dataname == "polblogs" else 200
train = True  # train or load pretrain

###########################

percentile = (1 - adj.sum() / adj.shape[0]**2 * 20) * 100
reconstructor = Link_rec_Runner(nettack.adj, features, model, iterations)
reconstructor.erun(train=train)
print("finishing reconstruction")
示例#4
0
        torch.cuda.manual_seed(args.seed)
    vgae = GCN(nfeat=features.shape[1],
               nhid=16,
               nclass=labels.max() + 1,
               device=device)
    vgae = vgae.to(device)
    vgae.fit(features,
             adj_rec_norm,
             labels,
             idx_train,
             idx_val,
             train_iters=200,
             verbose=False,
             normalize=False)
    vgae.eval()
    output = vgae.test(idx_val)
    res.append(output.item())

print("retraining")
bst = np.argmax(res) + 1
percentile = (1 - perturbed_adj.sum() / adj.shape[0]**2 * bst) * 100
adj_rec = reconstructor.sparse(percentile)
adj_rec_norm = utils.preprocess_graph(adj_rec)

torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)
vgae = GCN(nfeat=features.shape[1],
           nhid=16,
           nclass=labels.max() + 1,
           device=device)
示例#5
0
reconstructor = Link_rec_Runner(adj, features, model, iterations)
reconstructor.erun(train=train)

print("finishing reconstruction")

result = []
for p in percentiles:
    torch.manual_seed(args.seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(args.seed)
    print(p)
    adj_rec = reconstructor.sparse(p)
    adj_rec_norm = preprocess_graph(adj_rec)
    model = GCN(nfeat=features.shape[1],
                nhid=16,
                nclass=labels.max() + 1,
                device=device)
    model = model.to(device)
    model.fit(features,
              adj_rec_norm,
              labels,
              idx_train,
              idx_val,
              train_iters=200,
              verbose=False,
              normalize=False)
    model.eval()
    acc = model.test(idx_test)
    result.append(acc.item())
print(result)