Skip to content

Commit

Permalink
Merge pull request #207 from pyt-team/ninamiolane-fix-error
Browse files Browse the repository at this point in the history
FIX: SCN and SCCNN breaking after TopoNetX code change on the up laplacian
  • Loading branch information
ninamiolane authored Sep 19, 2023
2 parents de06c7c + 3805244 commit 0d2bdb6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 194 deletions.
2 changes: 1 addition & 1 deletion test/nn/simplicial/test_sccnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_forward(self):
incidence_2 = simplicial_complex.incidence_matrix(rank=2)
laplacian_0 = simplicial_complex.hodge_laplacian_matrix(rank=0, weight=True)
laplacian_down_1 = simplicial_complex.down_laplacian_matrix(rank=1, weight=True)
laplacian_up_1 = simplicial_complex.up_laplacian_matrix(rank=1, weight=True)
laplacian_up_1 = simplicial_complex.up_laplacian_matrix(rank=1)
laplacian_2 = simplicial_complex.hodge_laplacian_matrix(rank=2, weight=True)

incidence_1 = torch.from_numpy(incidence_1.todense()).to_sparse()
Expand Down
3 changes: 2 additions & 1 deletion test/nn/simplicial/test_scnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_forward(self):
incidence_2 = simplicial_complex.incidence_matrix(rank=2)
laplacian_0 = simplicial_complex.hodge_laplacian_matrix(rank=0, weight=True)
laplacian_down_1 = simplicial_complex.down_laplacian_matrix(rank=1, weight=True)
laplacian_up_1 = simplicial_complex.up_laplacian_matrix(rank=1, weight=True)
laplacian_up_1 = simplicial_complex.up_laplacian_matrix(rank=1)
laplacian_2 = simplicial_complex.hodge_laplacian_matrix(rank=2, weight=True)

incidence_1 = torch.from_numpy(incidence_1.todense()).to_sparse()
Expand Down Expand Up @@ -80,6 +80,7 @@ def get_simplicial_features(dataset, rank):
conv_order_down=conv_order_down,
conv_order_up=conv_order_up,
n_layers=num_layers,
aggr=True,
)
with torch.no_grad():
forward_pass = model(x_1, laplacian_down_1, laplacian_up_1)
Expand Down
49 changes: 28 additions & 21 deletions topomodelx/nn/simplicial/scnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ class SCNN(torch.nn.Module):
Parameters
----------
in_channels: int
Dimension of input features
intermediate_channels: int
Dimension of features of intermediate layers
out_channels: int
Dimension of output features
conv_order_down: int
Order of lower convolution
conv_order_up: int
Order of upper convolution
n_layers: int
Numer of layers
in_channels : int
Dimension of input features.
intermediate_channels : int
Dimension of features of intermediate layers.
out_channels : int
Dimension of output features.
conv_order_down : int
Order of lower convolution.
conv_order_up : int
Order of upper convolution.
aggr : bool
Whether to aggregate features on the nodes into 1 feature for the whole complex.
Default: False.
n_layers : int
Number of layers.
"""

def __init__(
Expand All @@ -33,6 +36,7 @@ def __init__(
out_channels,
conv_order_down,
conv_order_up,
aggr=False,
aggr_norm=False,
update_func=None,
n_layers=2,
Expand All @@ -59,7 +63,7 @@ def __init__(
update_func=update_func,
)
)

self.aggr = aggr
self.linear = torch.nn.Linear(out_channels, 1)
self.layers = torch.nn.ModuleList(layers)

Expand All @@ -68,22 +72,25 @@ def forward(self, x, laplacian_down, laplacian_up):
Parameters
----------
x: tensor
shape = [n_simplices, channels]
x : tensor, shape=[n_simplices, channels]
node/edge/face features
laplacian: tensor
shape = [n_simplices,n_simplices]
laplacian_down : tensor, shape=[n_simplices, n_simplices]
Down Laplacian.
For node features, laplacian_down = None
laplacian_up: tensor, shape=[n_edges, n_nodes]
Up Laplacian.
incidence_1: tensor
order 1 incidence matrix
shape = [n_edges, n_nodes]
Returns
-------
one_dimensional_cells_mean : tensor, shape=[n_simplices, 1]
Mean on one-dimensional cells.
"""
for layer in self.layers:
x = layer(x, laplacian_down, laplacian_up)

x_1 = self.linear(x)
if not self.aggr:
return x_1
one_dimensional_cells_mean = torch.nanmean(x_1, dim=0)
one_dimensional_cells_mean[torch.isnan(one_dimensional_cells_mean)] = 0

Expand Down
101 changes: 49 additions & 52 deletions tutorials/simplicial/sccnn_train.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -84,16 +84,13 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"downloading shrec 16 small dataset...\n",
"\n",
"done!\n",
"Loading shrec 16 small dataset...\n",
"\n",
"done!\n"
Expand All @@ -113,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -146,7 +143,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -162,10 +159,10 @@
"for simplex in simplexes:\n",
" incidence_1 = simplex.incidence_matrix(rank=1)\n",
" incidence_2 = simplex.incidence_matrix(rank=2)\n",
" laplacian_0 = simplex.hodge_laplacian_matrix(rank=0, weight=True)\n",
" laplacian_down_1 = simplex.down_laplacian_matrix(rank=1, weight=True)\n",
" laplacian_up_1 = simplex.up_laplacian_matrix(rank=1, weight=True)\n",
" laplacian_2 = simplex.hodge_laplacian_matrix(rank=2, weight=True)\n",
" laplacian_0 = simplex.hodge_laplacian_matrix(rank=0)\n",
" laplacian_down_1 = simplex.down_laplacian_matrix(rank=1)\n",
" laplacian_up_1 = simplex.up_laplacian_matrix(rank=1)\n",
" laplacian_2 = simplex.hodge_laplacian_matrix(rank=2)\n",
"\n",
" incidence_1 = torch.from_numpy(incidence_1.todense()).to_sparse()\n",
" incidence_2 = torch.from_numpy(incidence_2.todense()).to_sparse()\n",
Expand Down Expand Up @@ -193,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -245,7 +242,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -286,7 +283,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -301,16 +298,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch: 1 loss: 117979.8366\n",
"Test_loss: 406.8377\n",
"Epoch: 2 loss: 977.5059\n",
"Test_loss: 2.4212\n",
"Epoch: 3 loss: 217.5435\n",
"Test_loss: 5.1847\n",
"Epoch: 4 loss: 202.9460\n",
"Test_loss: 2.7320\n",
"Epoch: 5 loss: 212.8642\n",
"Test_loss: 3.1975\n"
"Epoch: 1 loss: 944857.9959\n",
"Test_loss: 214.5353\n",
"Epoch: 2 loss: 2055.6737\n",
"Test_loss: 8.6187\n",
"Epoch: 3 loss: 1060.7969\n",
"Test_loss: 1.4612\n",
"Epoch: 4 loss: 635.5951\n",
"Test_loss: 15.8349\n",
"Epoch: 5 loss: 408.8397\n",
"Test_loss: 45.9482\n"
]
}
],
Expand Down Expand Up @@ -420,7 +417,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -444,7 +441,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -475,7 +472,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand All @@ -497,7 +494,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -510,22 +507,22 @@
}
],
"source": [
"laplacian_0 = dataset.hodge_laplacian_matrix(rank=0, weight=True)\n",
"laplacian_down_1 = dataset.down_laplacian_matrix(rank=1, weight=True)\n",
"laplacian_up_1 = dataset.up_laplacian_matrix(rank=1, weight=True)\n",
"laplacian_down_2 = dataset.down_laplacian_matrix(rank=2, weight=True)\n",
"laplacian_up_2 = dataset.up_laplacian_matrix(rank=2, weight=True)\n",
"\n",
"laplacian_0 = dataset.adjacency_matrix(rank=0, weight=True)\n",
"laplacian_down_1 = dataset.coadjacency_matrix(rank=1, weight=True)\n",
"laplacian_up_1 = dataset.adjacency_matrix(rank=1, weight=True)\n",
"laplacian_down_2 = dataset.coadjacency_matrix(rank=2, weight=True)\n",
"laplacian_up_2 = dataset.adjacency_matrix(rank=2, weight=True)"
"laplacian_0 = dataset.hodge_laplacian_matrix(rank=0)\n",
"laplacian_down_1 = dataset.down_laplacian_matrix(rank=1)\n",
"laplacian_up_1 = dataset.up_laplacian_matrix(rank=1)\n",
"laplacian_down_2 = dataset.down_laplacian_matrix(rank=2)\n",
"laplacian_up_2 = dataset.up_laplacian_matrix(rank=2)\n",
"\n",
"laplacian_0 = dataset.adjacency_matrix(rank=0)\n",
"laplacian_down_1 = dataset.coadjacency_matrix(rank=1)\n",
"laplacian_up_1 = dataset.adjacency_matrix(rank=1)\n",
"laplacian_down_2 = dataset.coadjacency_matrix(rank=2)\n",
"laplacian_up_2 = dataset.adjacency_matrix(rank=2)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -550,7 +547,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -580,7 +577,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -601,7 +598,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -701,23 +698,23 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch: 1 loss: 62.1591 Train_acc: 0.1333\n",
"Test_acc: 0.5000\n",
"Epoch: 2 loss: 67.6678 Train_acc: 0.2333\n",
"Epoch: 1 loss: 31.6361 Train_acc: 0.6000\n",
"Test_acc: 0.7500\n",
"Epoch: 2 loss: 17.7911 Train_acc: 0.5667\n",
"Test_acc: 0.7500\n",
"Epoch: 3 loss: 75.0000 Train_acc: 0.2333\n",
"Epoch: 3 loss: 6.6667 Train_acc: 0.6333\n",
"Test_acc: 0.7500\n",
"Epoch: 4 loss: 74.3241 Train_acc: 0.2667\n",
"Test_acc: 0.2500\n",
"Epoch: 5 loss: 73.3333 Train_acc: 0.2000\n",
"Test_acc: 0.2500\n"
"Epoch: 4 loss: 6.6667 Train_acc: 0.6333\n",
"Test_acc: 1.0000\n",
"Epoch: 5 loss: 5.0000 Train_acc: 0.7000\n",
"Test_acc: 1.0000\n"
]
}
],
Expand Down
Loading

0 comments on commit 0d2bdb6

Please sign in to comment.