def test_twostage(model_ts, test_instances_eval, algoname):
        for test_i in test_instances_eval:
            #predict probability that all unobserved edges exist
            n = adj_train[test_i].shape[0]
            indices = torch.tensor(np.arange(n))
            to_pred = torch.zeros(n**2, 2)
            to_pred[:, 1] = indices.repeat(n)
            for i in range(n):
                to_pred[i * n:(i + 1) * n, 0] = i
            to_pred = to_pred.long()
            preds = model_ts(features_train[test_i], adj_train[test_i],
                             to_pred)
            preds = nn.Sigmoid()(preds).view(n, n)
            preds = bin_adj_train[test_i] + (1 - bin_adj_train[test_i]) * preds

            if args.objective == 'modularity':
                r = greedy_modularity_communities(preds, K)
                loss = loss_fn(None, r, None, None, bin_adj_all[test_i],
                               test_object[test_i], args).item()
                vals[algoname +
                     '-agglomerative'][test_instances.index(test_i)] = loss
                r = partition(preds, K)
                loss = loss_fn(None, r, None, None, bin_adj_all[test_i],
                               test_object[test_i], args).item()
                vals[algoname +
                     '-recursive'][test_instances.index(test_i)] = loss
                degrees = preds.sum(dim=1)
                preds = torch.diag(1. / degrees) @ preds
                mod_pred = make_modularity_matrix(preds)
                r = baseline_spectral(mod_pred, K)
                loss = loss_fn(None, r, None, None, bin_adj_all[test_i],
                               test_object[test_i], args).item()
                vals[algoname +
                     '-spectral'][test_instances.index(test_i)] = loss
            elif args.objective == 'kcenter':
                print('making dists')
                if args.use_igraph:
                    dist_ts = make_dists_igraph(preds)
                else:
                    dist_ts = make_all_dists(preds, 100)
                    diameter = dist_ts[dist_ts < 100].max()
                    dist_ts[dist_ts == 100] = diameter
                print(test_i)
                dist_ts = dist_ts.float()
                diameter = dist_ts.max()
                x = gonzalez_kcenter(dist_ts, K)
                loss = obj_test[test_i](x)
                vals[algoname +
                     '-gonzalez'][test_instances.index(test_i)] = loss.item()
                x = greedy_kcenter(dist_ts, diameter, K)
                loss = obj_test[test_i](x)
                vals[algoname +
                     '-greedy'][test_instances.index(test_i)] = loss.item()
Esempio n. 2
0
            torch.save(dist_train,
                       '{}_{}_train_dist.pt'.format(args.dataset, train_pct))
        obj_train = CenterObjective(dist_train, diameter, args.kcentermintemp)
        obj_train_hardmax = CenterObjective(dist_train,
                                            diameter,
                                            args.kcentermintemp,
                                            hardmax=True)
        obj_test = CenterObjective(dist_all,
                                   diameter,
                                   args.kcentertemp,
                                   hardmax=True)
        obj_test_softmax = CenterObjective(dist_all, diameter,
                                           args.kcentermintemp)

    if args.objective == 'modularity':
        mod_train = make_modularity_matrix(bin_adj_train)
        mod_test = make_modularity_matrix(bin_adj_test)
        mod_valid = make_modularity_matrix(bin_adj_valid)
        mod_all = make_modularity_matrix(bin_adj_all)

##############################################################################
#DEFINE LOSS FUNCTIONS
##############################################################################

if args.objective == 'modularity':
    loss_fn = loss_modularity
    test_object = mod_all
    train_object = mod_train
    test_only_object = mod_test
    valid_object = mod_valid
elif args.objective == 'kcenter':
model_ts = GCNLink(no_features=nfeat, no_hidden=50, no_output=50, dropout=0.5)
'''ClusterNet Model'''
model_cluster = GCNClusterNet(no_features=nfeat,
                              no_hidden=50,
                              no_output=50,
                              dropout=0.5,
                              K=K,
                              cluster_temp=30)
#uses GCNs to predict the cluster membership of each node
'''GCN end to end'''
model_gcn = GCNDeep(no_features=nfeat, no_hidden=50, no_output=K, dropout=0.5)
optimizer = optim.Adam(model_cluster.parameters(), lr=0.005, weight_decay=5e-4)
losses = []
losses_test = []

mod_train = make_modularity_matrix(bin_adj_train)
# mod_valid = make_modularity_matrix(bin_adj_valid)
mod_all = make_modularity_matrix(bin_adj_all)

loss_fn = loss_modularity
test_object = mod_all
train_object = mod_train
'''ClusterNet'''
clusterNet_start = time.time()
best_train_val = 100
for t in range(1001):
    #link prediction setting: get loss with respect to training edges only
    r, dist = model_cluster(features_train, adj_train, num_cluster_iter)
    loss = loss_fn(r, bin_adj_train, train_object)
    loss = -loss
    optimizer.zero_grad()
    ts_algos = ['gonzalez', 'greedy']
for algo in ts_algos:
    algs.append('train-' + algo)
    algs.append('ts-' + algo)
    algs.append('ts-ft-' + algo)
    algs.append('ts-ft-only-' + algo)
for algo in algs:
    vals[algo] = np.zeros(numtest)

aucs_algs = ['ts', 'ts-ft', 'ts-ft-only']
aucs = {}
for algo in aucs_algs:
    aucs[algo] = np.zeros(numtest)

if args.objective == 'modularity':
    mods_test = [make_modularity_matrix(A) for A in bin_adj_all]
    mods_train = [make_modularity_matrix(A) for A in bin_adj_train]
    test_object = mods_test
    train_object = mods_train
    loss_fn = loss_modularity
elif args.objective == 'kcenter':
    for i in range(num_graphs):
        try:
            dist_all.append(torch.load('{}_{}_test_dist.pt'.format(args.dataset, i)))
            dist_train.append(torch.load('{}_{}_{:.2f}_train_dist.pt'.format(args.dataset, i, train_pct)))
            diameter = dist_all[-1].max()
        except:
            dist_all_i = make_all_dists(bin_adj_all[i], 100)
            diameter = dist_all_i[dist_all_i < 100].max()
            dist_all_i[dist_all_i == 100] = diameter
            torch.save(dist_all_i, '{}_{}_test_dist.pt'.format(args.dataset, i))