def chebyshev5(self, inputs, L, Fout, K): # if not hasattr(self, 'InterX'): # self.InterX = x N, M, Fin = inputs.get_shape() N, M, Fin = int(N), int(M), int(Fin) # Rescale Laplacian and store as a TF sparse tensor. Copy to not modify the shared L. L = scipy.sparse.csr_matrix(L) L = graph.rescale_L(L, 2) L = L.tocoo() indices = np.column_stack((L.row, L.col)) L = tf.SparseTensor(indices, L.data, L.shape) L = tf.sparse_reorder(L) # Transform to Chebyshev basis x0 = tf.transpose(inputs, perm=[1, 2, 0]) # M x Fin x N x0 = tf.reshape(x0, [M, Fin * N]) # M x Fin*N x = tf.expand_dims(x0, 0) # 1 x M x Fin*N def concat(x, x_): x_ = tf.expand_dims(x_, 0) # 1 x M x Fin*N return tf.concat([x, x_], axis=0) # K x M x Fin*N if K > 1: x1 = tf.sparse_tensor_dense_matmul(L, x0) x = concat(x, x1) for _ in range(2, K): x2 = 2 * tf.sparse_tensor_dense_matmul(L, x1) - x0 # M x Fin*N x = concat(x, x2) x0, x1 = x1, x2 x = tf.reshape(x, [K, M, Fin, N]) # K x M x Fin x N x = tf.transpose(x, perm=[3, 1, 2, 0]) # N x M x Fin x K x = tf.reshape(x, [N * M, Fin * K]) # N*M x Fin*K # Filter: Fin*Fout filters of order K, i.e. one filterbank per feature pair. W = self._weight_variable([Fin * K, Fout], regularization=False) x = tf.matmul(x, W) # N*M x Fout return tf.reshape(x, [N, M, Fout]) # N x M x Fout
def chebyshev2(self, x, L, Fout, K): """ Filtering with Chebyshev interpolation Implementation: numpy. Data: x of size N x M x F N: number of signals M: number of vertices F: number of features per signal per vertex """ N, M, Fin = x.get_shape() N, M, Fin = int(N), int(M), int(Fin) # Rescale Laplacian. Copy to not modify the shared L. L = scipy.sparse.csr_matrix(L) L = graph.rescale_L(L, lmax=2) # Transform to Chebyshev basis x = tf.transpose(x, perm=[1, 2, 0]) # M x Fin x N x = tf.reshape(x, [M, Fin*N]) # M x Fin*N def chebyshev(x): return graph.chebyshev(L, x, K) x = tf.py_func(chebyshev, [x], [tf.float32])[0] # K x M x Fin*N x = tf.reshape(x, [K, M, Fin, N]) # K x M x Fin x N x = tf.transpose(x, perm=[3,1,2,0]) # N x M x Fin x K x = tf.reshape(x, [N*M, Fin*K]) # N*M x Fin*K # Filter: Fin*Fout filters of order K, i.e. one filterbank per feature. W = self._weight_variable([Fin*K,Fout], 'weight',regularization=False) x = tf.matmul(x, W) # N*M x Fout return tf.reshape(x, [N, M, Fout]) # N x M x Fout
def laplacian_to_sparse(self, laplacian): # Rescale Laplacian and store as a torch sparse tensor. Copy to not modify the shared laplacian. laplacian = scipy.sparse.csr_matrix(laplacian) laplacian = graph.rescale_L(laplacian, lmax=2) laplacian = laplacian.tocoo() indices = torch.LongTensor(np.row_stack( (laplacian.row, laplacian.col))) data = torch.FloatTensor(laplacian.data) shape = torch.Size(laplacian.shape) sparse_lapla = torch.sparse.FloatTensor(indices, data, shape).cuda() return sparse_lapla
def Chebyshev5(x, L, Fout, K): N, M, Fin = x.shape N, M, Fin = int(N), int(M), int(Fin) # Rescale Laplacian and store as a TF sparse tensor. Copy to not modify the shared L. L = scipy.sparse.csr_matrix(L) L = graph.rescale_L(L, lmax=2) L = L.tocoo() indices = np.column_stack((L.row, L.col)) i = torch.LongTensor(np.transpose(indices)) v = torch.FloatTensor(L.data) L = torch.sparse.FloatTensor(i, v, L.shape) # Transform to Chebyshev basis x0 = x.permute(1, 2, 0) # M x Fin x N x0 = x0.contiguous() x0 = x0.view(M, Fin * N) # M x Fin*N x = x0.unsqueeze(0) # 1 x M x Fin*N def concat(x, x_): x_ = x_.unsqueeze(0) # 1 x M x Fin*N return torch.cat((x, x_), 0) # K x M x Fin*N if K > 1: x1 = torch.spmm(L, x0) x = concat(x, x1) for k in range(2, K): x2 = 2 * torch.spmm(L, x1) - x0 # M x Fin*N x = concat(x, x2) x0, x1 = x1, x2 x = x.view(K, M, Fin, N) # K x M x Fin x N x = x.permute(3, 1, 2, 0) # N x M x Fin x K x = x.contiguous() x = x.view(N * M, Fin * K) # N*M x Fin*K # Filter: Fin*Fout filters of order K, i.e. one filterbank per feature pair. # W = self._weight_variable([Fin * K, Fout], regularization=False) #W = Parameter(torch.FloatTensor(Fin * K, Fout)) W = torch.tensor([[-6.2733e-09, 3.0666e-41, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, -0.6, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00], [0.11, 0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, -0.5, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.4, 0.0000e+00]]) x = torch.mm(x, W) # N*M x Fout return x.view(N, M, Fout) # N x M x Fout
def TensorChebyshev5(x, L, Fout, K): N, M, Fin = x.get_shape() N, M, Fin = int(N), int(M), int(Fin) # Rescale Laplacian and store as a TF sparse tensor. Copy to not modify the shared L. L = scipy.sparse.csr_matrix(L) L = graph.rescale_L(L, lmax=2) L = L.tocoo() indices = np.column_stack((L.row, L.col)) L = tf.SparseTensor(indices, L.data, L.shape) L = tf.sparse_reorder(L) # Transform to Chebyshev basis x0 = tf.transpose(x, perm=[1, 2, 0]) # M x Fin x N x0 = tf.reshape(x0, [M, Fin * N]) # M x Fin*N x = tf.expand_dims(x0, 0) # 1 x M x Fin*N def concat(x, x_): x_ = tf.expand_dims(x_, 0) # 1 x M x Fin*N return tf.concat([x, x_], axis=0) # K x M x Fin*N if K > 1: x1 = tf.sparse_tensor_dense_matmul(L, x0) x = concat(x, x1) for k in range(2, K): x2 = 2 * tf.sparse_tensor_dense_matmul(L, x1) - x0 # M x Fin*N x = concat(x, x2) x0, x1 = x1, x2 x = tf.reshape(x, [K, M, Fin, N]) # K x M x Fin x N x = tf.transpose(x, perm=[3, 1, 2, 0]) # N x M x Fin x K x = tf.reshape(x, [N * M, Fin * K]) # N*M x Fin*K # Filter: Fin*Fout filters of order K, i.e. one filterbank per feature pair. #W = weight_variable([Fin * K, Fout], regularization=False) W = torch.tensor([[-6.2733e-09, 3.0666e-41, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, -0.6, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00], [0.11, 0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, -0.5, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.4, 0.0000e+00]]) W = tf.convert_to_tensor(W.numpy()) x = tf.matmul(x, W) # N*M x Fout return tf.reshape(x, [N, M, Fout]) # N x M x Fout
def my_gcn(self, x, L, th): Fout, neibs = 1, 3 _, M, Fin = x.get_shape() M, Fin = int(M), int(Fin) # Rescale Laplacian and store as a TF sparse tensor. Copy to not modify the shared L. L = scipy.sparse.csr_matrix(L) L = graph.rescale_L(L, lmax=2) L = L.tocoo() indices = np.column_stack((L.row, L.col)) L = tf.SparseTensor(indices, L.data, L.shape) L = tf.sparse_reorder(L) # Transform to Chebyshev basis x0 = tf.transpose(x, perm=[1, 2, 0]) # M x Fin x N x0 = tf.reshape(x0, [M, -1]) # M x Fin*N x = tf.expand_dims(x0, 0) # 1 x M x Fin*N def concat(x, x_): x_ = tf.expand_dims(x_, 0) # 1 x M x Fin*N return tf.concat([x, x_], 0) # K x M x Fin*N if neibs > 1: x1 = tf.sparse_tensor_dense_matmul(L, x0) x = concat(x, x1) for k in range(2, neibs): x2 = 2 * tf.sparse_tensor_dense_matmul(L, x1) - x0 # M x Fin*N x = concat(x, x2) x0, x1 = x1, x2 x = tf.reshape(x, [neibs, M, Fin, -1]) # K x M x Fin x N x = tf.transpose(x, perm=[3, 1, 2, 0]) # N x M x Fin x K x = tf.reshape(x, [-1, Fin * neibs]) # N*M x Fin*K # Filter: Fin*Fout filters of order K, i.e. one filterbank per feature pair. # W = _weight_variable([Fin*K, Fout]) # x = tf.matmul(x, W) # N*M x Fout self.w_gcn = self.add_weight(name='w_gcn_' + th, shape=([Fin * neibs, self.output_dim]), initializer='glorot_uniform', trainable=True) x = K.dot(x, self.w_gcn) return tf.reshape(x, [-1, M, Fout]) # N x M x Fout