-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparsify.py
101 lines (85 loc) · 3.45 KB
/
sparsify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
import torch.nn.functional as F
import argparse
import numpy as np
import pdb
import time
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
class UniformEdges(nn.Module):
# sampling edges with uniform distribution
# theoretical reason: https://cs.stanford.edu/~jure/pubs/sampling-kdd06.pdf
def __init__(self, num_sample_edges):
super(UniformEdges, self).__init__()
self.num_sample_edges = num_sample_edges
def sparsify(self, W):
# input: W
# output: H, sparsified.
H = torch.zeros(*W.size(), device=device).to(device)
edges = (W.triu().nonzero()).to(device) # index of edges with shape [nE, 2]
num_edges = edges.shape[0]
edges_idx = (torch.randperm(num_edges, dtype=torch.int64)[:int(np.around(self.num_sample_edges/2))]).to(device)
edges = edges[edges_idx].t()
H[edges[0],edges[1]] = 1
H = (H + H.t()).clone().to(device)
return H
class Phase2Edges(nn.Module):
# y_ij = P((i->j) \in E_{k,k}) = y_i * y_j
def __init__(self, num_sample_edges):
super(Phase2Edges, self).__init__()
self.num_sample_edges = num_sample_edges
def sparsify(self, pred):
# input: predictions on nodes from phase 1
# output: H, sparsified adjacency matrix
# pred shape = [1, N]
N = pred.shape[1]
pred = pred.squeeze()
pred = torch.ger(pred, pred).to(device)
# to kill diagonal entries
mask = torch.eye(N,device=device).to(device).byte()
pred[mask] = 0
pred = pred.view(-1)
_, ind = torch.sort(-pred)
ind = ind[:int(np.around(self.num_sample_edges))]
# now pred = output
pred = torch.zeros(*(N,N), device=device).to(device)
pred[(ind // N), (ind % N)] = 1
return pred
if __name__=='__main__':
sparsifier = Phase2Edges(20)
pred = torch.abs(torch.rand(6))
H = sparsifier.sparsify(pred)
# sample nodes according to degree distribution
# select edges uniformly
# class DegreeNodesUniformEdges(nn.Module):
# # sampling nodes with degree distribution
# # and then sampling edges with uniform distribution.
# def __init__(self, prop_nodes, num_edges):
# super(DegreeNodesUniformEdges, self).__init__()
# self.prob_nodes = prob_nodes
# self.num_edges = num_edges
#
# def sample_nodes(self, deg):
# # deg shape : [bs, N, 1]
# deg = deg.squeeze() # now [bs, N]
# deg = torch.multinomial(deg, num_samples=int(self.prob_nodes*deg.shape[1]), replacement=False).to(device)
# # deg.shape = [ bs, prob_nodes*N ]
# return deg
#
# def sample_edges(self, W, deg):
# # input: W
# # output: H, sparsified.
# bs, N, _ = W.shape
# deg = self.sample_nodes(deg) # shape [bs, prob_nodes*N]
# H = torch.sparse.FloatTensor(bs, self.num_edges, self.num_edges)
# H = torch.zeros((bs, self.num_edges, self.num_edges), device=device) # [bs, nE, nE]
# for i in range(bs):
# edges = (W[i].triu().nonzero()).to(device) # index of edges with shape [nE, 2]
# num_edges = edges.shape[0]
# edges_idx = (torch.randperm(num_edges, dtype=torch.int64)[:self.num_edges]).to(device)
# edges = edges[edges_idx]