forked from hongzimao/decima-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse_op.py
executable file
·191 lines (146 loc) · 4.64 KB
/
sparse_op.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
import tensorflow as tf
class SparseMat(object):
def __init__(self, dtype, shape):
self.dtype = dtype
self.shape = shape
self.row = []
self.col = []
self.data = []
def add(self, row, col, data):
self.row.append(row)
self.col.append(col)
self.data.append(data)
def get_col(self):
return np.array(self.col)
def get_row(self):
return np.array(self.row)
def get_data(self):
return np.array(self.data)
def to_tfsp_matrix(self):
indices = np.mat([row, col]).transpose()
return tf.SparseTensorValue(indices, data, self.shape)
def absorb_sp_mats(in_mats, depth):
"""
Merge multiple sparse matrices to
a giant one on its diagonal
e.g.,
[0, 1, 0] [0, 1, 0] [0, 0, 1]
[1, 0, 0] [0, 0, 1] [0, 1, 0]
[0, 0, 1] [1, 0, 0] [0, 1, 0]
to
[0, 1, 0]
[1, 0, 0] .. .. .. ..
[0, 0, 1]
[0, 1, 0]
.. .. [0, 0, 1] .. ..
[1, 0, 0]
[0, 0, 1]
.. .. .. .. [0, 1, 0]
[0, 1, 0]
where ".." are all zeros
depth is on the 3rd dimension,
which is orthogonal to the planar
operations above
output SparseTensorValue from tensorflow
"""
sp_mats = []
for d in range(depth):
row_idx = []
col_idx = []
data = []
shape = 0
base = 0
for m in in_mats:
row_idx.append(m[d].get_row() + base)
col_idx.append(m[d].get_col() + base)
data.append(m[d].get_data())
shape += m[d].shape[0]
base += m[d].shape[0]
row_idx = np.hstack(row_idx)
col_idx = np.hstack(col_idx)
data = np.hstack(data)
indices = np.mat([row_idx, col_idx]).transpose()
sp_mats.append(tf.SparseTensorValue(
indices, data, (shape, shape)))
return sp_mats
def expand_sp_mat(sp, exp_step):
"""
Make a stack of same sparse matrix to
a giant one on its diagonal
The input is tf.SparseTensorValue
e.g., expand dimension 3
[0, 1, 0] [0, 1, 0]
[1, 0, 0] [1, 0, 0] .. .. .. ..
[0, 0, 1] [0, 0, 1]
[0, 1, 0]
to .. .. [1, 0, 0] .. ..
[0, 0, 1]
[0, 1, 0]
.. .. .. .. [1, 0, 0]
[0, 0, 1]
where ".." are all zeros
depth is on the 3rd dimension,
which is orthogonal to the planar
operations above
output SparseTensorValue from tensorflow
"""
extended_mat = []
depth = len(sp)
for d in range(depth):
row_idx = []
col_idx = []
data = []
shape = 0
base = 0
for i in range(exp_step):
indices = sp[d].indices.transpose()
row_idx.append(np.squeeze(np.asarray(indices[0, :]) + base))
col_idx.append(np.squeeze(np.asarray(indices[1, :]) + base))
data.append(sp[d].values)
shape += sp[d].dense_shape[0]
base += sp[d].dense_shape[0]
row_idx = np.hstack(row_idx)
col_idx = np.hstack(col_idx)
data = np.hstack(data)
indices = np.mat([row_idx, col_idx]).transpose()
extended_mat.append(tf.SparseTensorValue(
indices, data, (shape, shape)))
return extended_mat
def merge_and_extend_sp_mat(sp):
"""
Transform a stack of sparse matrix into a giant diagonal matrix
These sparse matrices should have same shape
e.g.,
list of
[1, 0, 1, 1] [0, 0, 0, 1]
[1, 1, 1, 1] [0, 1, 1, 1]
[0, 0, 1, 1] [1, 1, 1, 1]
to
[1, 0, 1, 1]
[1, 1, 1, 1] .. ..
[0, 0, 1, 1]
[0, 0, 0, 1]
.. .. [0, 1, 1, 1]
[1, 1, 1, 1]
"""
batch_size = len(sp)
row_idx = []
col_idx = []
data = []
shape = (sp[0].dense_shape[0] * batch_size, sp[0].dense_shape[1] * batch_size)
row_base = 0
col_base = 0
for b in range(batch_size):
indices = sp[b].indices.transpose()
row_idx.append(np.squeeze(np.asarray(indices[0, :]) + row_base))
col_idx.append(np.squeeze(np.asarray(indices[1, :]) + col_base))
data.append(sp[b].values)
row_base += sp[b].dense_shape[0]
col_base += sp[b].dense_shape[1]
row_idx = np.hstack(row_idx)
col_idx = np.hstack(col_idx)
data = np.hstack(data)
indices = np.mat([row_idx, col_idx]).transpose()
extended_mat = tf.SparseTensorValue(indices, data, shape)
return extended_mat