text
stringlengths
1
93.6k
if alpha != -0.5:
return adj / (adj.sum(dim=1).reshape(adj.shape[0], -1))
else:
return adj
def preprocess_adj(features, adj, logger, metric='similarity', threshold=0.03, jaccard=True):
"""Drop dissimilar edges.(Faster version using numba)
"""
if not sp.issparse(adj):
adj = sp.csr_matrix(adj)
adj_triu = sp.triu(adj, format='csr')
if sp.issparse(features):
features = features.todense().A # make it easier for njit processing
if metric == 'distance':
removed_cnt = dropedge_dis(adj_triu.data, adj_triu.indptr, adj_triu.indices, features, threshold=threshold)
else:
if jaccard:
removed_cnt = dropedge_jaccard(adj_triu.data, adj_triu.indptr, adj_triu.indices, features,
threshold=threshold)
else:
removed_cnt = dropedge_cosine(adj_triu.data, adj_triu.indptr, adj_triu.indices, features,
threshold=threshold)
logger.info('removed %s edges in the original graph' % removed_cnt)
modified_adj = adj_triu + adj_triu.transpose()
return modified_adj
def dropedge_dis(A, iA, jA, features, threshold):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
C = np.linalg.norm(features[n1] - features[n2])
if C > threshold:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def dropedge_both(A, iA, jA, features, threshold1=2.5, threshold2=0.01):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
C1 = np.linalg.norm(features[n1] - features[n2])
a, b = features[n1], features[n2]
inner_product = (a * b).sum()
C2 = inner_product / (np.sqrt(np.square(a).sum() + np.square(b).sum())+ 1e-6)
if C1 > threshold1 or threshold2 < 0:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def dropedge_jaccard(A, iA, jA, features, threshold):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
a, b = features[n1], features[n2]
intersection = np.count_nonzero(a*b)
J = intersection * 1.0 / (np.count_nonzero(a) + np.count_nonzero(b) - intersection)
if J < threshold:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt
def dropedge_cosine(A, iA, jA, features, threshold):
removed_cnt = 0
for row in range(len(iA)-1):
for i in range(iA[row], iA[row+1]):
# print(row, jA[i], A[i])
n1 = row
n2 = jA[i]
a, b = features[n1], features[n2]
inner_product = (a * b).sum()
C = inner_product / (np.sqrt(np.square(a).sum()) * np.sqrt(np.square(b).sum()) + 1e-8)
if C <= threshold:
A[i] = 0
# A[n2, n1] = 0
removed_cnt += 1
return removed_cnt