summaryrefslogtreecommitdiff
path: root/scnn/scnn.py
diff options
context:
space:
mode:
Diffstat (limited to 'scnn/scnn.py')
-rw-r--r--scnn/scnn.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/scnn/scnn.py b/scnn/scnn.py
new file mode 100644
index 0000000..49efe71
--- /dev/null
+++ b/scnn/scnn.py
@@ -0,0 +1,42 @@
+import torch
+import torch.nn as nn
+import numpy as np
+import scipy.sparse as sp
+
+import chebyshev
+
+def coo2tensor(A):
+ assert(sp.isspmatrix_coo(A))
+ idxs = torch.LongTensor(np.vstack((A.row, A.col)))
+ vals = torch.FloatTensor(A.data)
+ return torch.sparse_coo_tensor(idxs, vals, size = A.shape, requires_grad = False)
+
+class SimplicialConvolution(nn.Module):
+ def __init__(self, K, L, C_in, C_out, groups = 1):
+ assert(groups == 1, "Only groups = 1 is currently supported.")
+ super().__init__()
+
+ assert(len(L.shape) == 2)
+ assert(L.shape[0] == L.shape[1])
+ assert(C_in > 0)
+ assert(C_out > 0)
+ assert(K > 0)
+
+ self.M = L.shape[0]
+ self.C_in = C_in
+ self.C_out = C_out
+ self.K = K
+ self.L = L # Don't modify afterwards!
+
+ self.theta = nn.parameter.Parameter(torch.randn((self.C_out, self.C_in, self.K)))
+
+ def forward(self, x):
+ (B, C_in, M) = x.shape
+ assert(M == self.M)
+ assert(C_in == self.C_in)
+
+ X = chebyshev.assemble(self.K, self.L, x)
+ y = torch.einsum("bimk,oik->bom", (X, self.theta))
+ assert(y.shape == (B, self.C_out, M))
+
+ return y