Commit 6db5679e authored by Rishabh Jain's avatar Rishabh Jain
Browse files

Checking with expSetups file

parent edd4bae5
citeseer 2 5
0.39910002052783966
citeseer 7 5
0.695600026845932
citeseer 10 10
0.7231000304222107
citeseer 10 5
0.7251000344753266
citeseer 7 5
0.7153000354766845
citeseer 5 5
0.6896000385284424
0.7060000419616699
citeseer 2 5
0.3349000155925751
cora 10 5
0.7334000289440155
cora 7 5
0.7257000267505646
cora 5 5
0.7080000221729279
cora 2 5
0.56290003657341
cora 3 5
0.6594000339508057
citeseer 3 5
0.6269000232219696
citeseer 4 5
0.6566000342369079
cora 3 5
0.5221000224351883
citeseer 3 5
0.6660000205039978
citeseer 2.5 5
0.5117000252008438
cora 2.5 5
0.6196000277996063
import numpy as np
import torch
def graph_delete_connections(prob_del, seed, adj, enforce_connected=False):
rnd = np.random.RandomState(seed)
del_adj = np.array(adj, dtype=np.float32)
pre_num_edges = np.sum(del_adj)
smpl = rnd.choice([0., 1.], p=[prob_del, 1. - prob_del], size=adj.shape) * np.triu(np.ones_like(adj), 1)
smpl += smpl.transpose()
del_adj *= smpl
if enforce_connected:
add_edges = 0
for k, a in enumerate(del_adj):
if not list(np.nonzero(a)[0]):
prev_connected = list(np.nonzero(adj[k, :])[0])
other_node = rnd.choice(prev_connected)
del_adj[k, other_node] = 1
del_adj[other_node, k] = 1
add_edges += 1
print('# ADDED EDGES: ', add_edges)
cur_num_edges = np.sum(del_adj)
print('[ Deleted {}% edges ]'.format(100 * (pre_num_edges - cur_num_edges) / pre_num_edges))
return del_adj
def graph_add_connections(prob_add, seed, adj, enforce_connected=False):
rnd = np.random.RandomState(seed)
add_adj = np.array(adj, dtype=np.float32)
smpl = rnd.choice([0., 1.], p=[1. - prob_add, prob_add], size=adj.shape) * np.triu(np.ones_like(adj), 1)
smpl += smpl.transpose()
add_adj += smpl
if enforce_connected:
add_edges = 0
for k, a in enumerate(add_adj):
if not list(np.nonzero(a)[0]):
prev_connected = list(np.nonzero(adj[k, :])[0])
other_node = rnd.choice(prev_connected)
add_adj[k, other_node] = 1
add_adj[other_node, k] = 1
add_edges += 1
print('# ADDED EDGES: ', add_edges)
add_adj = (add_adj > 0).astype(float)
return add_adj
def sample_edges(edge_index, ratio, seed=123):
# sample from half side of the symmetric adjacency matrix
half_edge_index = []
for i in range(edge_index.shape[1]):
if edge_index[0, i] < edge_index[1, i]:
half_edge_index.append(edge_index[:, i].view(2,-1))
half_edge_index = torch.cat(half_edge_index, dim=1)
np.random.seed(seed)
num_edge = half_edge_index.shape[1]
samples = np.random.choice(num_edge, size=int(ratio*num_edge), replace=False)
sampled_edge_index = half_edge_index[:, samples]
sampled_edge_index = torch.cat([sampled_edge_index, sampled_edge_index[torch.LongTensor([1,0])]], dim=1)
return sampled_edge_index
#Setup 3 - addition of noise to features
def features_noise_addition(dataset, features, noise, r, nr):
ogb = False
if dataset.startswith('ogb') or dataset in ["wine", "digits", "breast_cancer"]:
probs = torch.full(features.shape, 1 / r)
mask = torch.bernoulli(probs)
ogb = True
else:
nones = torch.sum(features > 0.0).float()
nzeros = features.shape[0] * features.shape[1] - nones
pzeros = nones / nzeros / r * nr
probs = torch.zeros(features.shape)
probs[features == 0.0] = pzeros
probs[features > 0.0] = 1 / r
mask = torch.bernoulli(probs)
if dataset == "20news10":
ogb = True
if ogb:
if noise == 'mask':
masked_features = features * (1 - mask)
elif noise == 'normal':
masked_features = torch.normal(0.0, 1.0, size = features.shape).cuda()
masked_features = features + features * noise
else:
print("Invalid noise input encountered while adding noise to features")
else:
masked_features = features * (1 - mask)
return masked_features
......@@ -17,7 +17,7 @@ from utils import accuracy, get_random_mask, get_random_mask_ogb, nearest_neighb
EOS = 1e-10
from exp_setups import features_noise_addition
#CHANGED-------------------------*************
def noisy_features(features, mask, noise, ogb):
#ogb dataset
......@@ -255,29 +255,25 @@ class Experiment:
##CHANGED---------------------------------------*************************
features = orig_features
r = 2
r = 2.5
nr = 5
nones = torch.sum(features > 0.0).float()
nzeros = features.shape[0] * features.shape[1] - nones
pzeros = nones / nzeros / r * nr
probs = torch.zeros(features.shape)
probs[features == 0.0] = pzeros
probs[features > 0.0] = 1 / r
mask = torch.bernoulli(probs)
ogb = False
'''
if args.dataset.startswith('ogb') or args.dataset in ["wine", "digits", "breast_cancer"]:
mask = get_random_mask_ogb(features, args.ratio)
ogb = True
elif args.dataset == "20news10":
mask = get_random_mask(features, args.ratio, args.nr)
if args.dataset.startswith('ogb'):
probs = torch.full(features.shape, 1 / r)
mask = torch.bernoulli(probs)
ogb = True
else:
mask = get_random_mask(features, args.ratio, args.nr)
nones = torch.sum(features > 0.0).float()
nzeros = features.shape[0] * features.shape[1] - nones
pzeros = nones / nzeros / r * nr
probs = torch.zeros(features.shape)
probs[features == 0.0] = pzeros
probs[features > 0.0] = 1 / r
mask = torch.bernoulli(probs)
ogb = False
'''
features = noisy_features(features, mask, args.noise, ogb)
features = features_noise_addition(args.dataset, features, "mask", r, nr)
#features = noisy_features(features, mask, 'mask', ogb)
##CHANGED---------------------------------------*************************
model1 = GCN_DAE(nlayers=args.nlayers_adj, in_dim=nfeats, hidden_dim=args.hidden_adj, nclasses=nfeats,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment