Skip to content

Commit

Permalink
doc!: update notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLapous committed Sep 24, 2024
1 parent 1f4a76f commit 0dae0d5
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 389 deletions.
41 changes: 9 additions & 32 deletions docs/notebooks/degree_rips_interface.ipynb

Large diffs are not rendered by default.

132 changes: 48 additions & 84 deletions docs/notebooks/graph_autodiff.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@
"import torch.nn as nn\n",
"import numpy as np\n",
"import multipers.ml.signed_measures as mms\n",
"import multipers.grids as mpg"
"import multipers.grids as mpg\n",
"torch.manual_seed(1)\n",
"## TODO : fixme\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n"
]
},
{
"cell_type": "markdown",
"id": "9c16285d-badc-44e5-834c-8e2b9ff0b990",
"metadata": {},
"source": [
"This code is not meant to realize state of the art graph classification,\n",
"but to give an idea on how to use `multipers` in a DL setting."
]
},
{
Expand All @@ -42,7 +55,7 @@
"metadata": {},
"outputs": [],
"source": [
"dataset_name = \"BZR\""
"dataset_name = \"MUTAG\""
]
},
{
Expand All @@ -69,20 +82,7 @@
"execution_count": 4,
"id": "1aa47583-05ef-4171-a46c-2bd9233432ce",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/hschreiber/micromamba/envs/multipers-old/lib/python3.12/site-packages/torch_geometric/data/dataset.py:238: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" if osp.exists(f) and torch.load(f) != _repr(self.pre_transform):\n",
"/home/hschreiber/micromamba/envs/multipers-old/lib/python3.12/site-packages/torch_geometric/data/dataset.py:246: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" if osp.exists(f) and torch.load(f) != _repr(self.pre_filter):\n",
"/home/hschreiber/micromamba/envs/multipers-old/lib/python3.12/site-packages/torch_geometric/io/fs.py:215: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" return torch.load(f, map_location)\n"
]
}
],
"outputs": [],
"source": [
"transform = T.Compose([\n",
" # T.GDC(diffusion_kwargs={\n",
Expand Down Expand Up @@ -124,7 +124,7 @@
{
"data": {
"text/plain": [
"(torch.Size([13092, 56]), torch.Size([1376, 56]), torch.Size([14479, 56]))"
"(torch.Size([3027, 7]), torch.Size([336, 7]), torch.Size([3371, 7]))"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -172,7 +172,7 @@
"outputs": [],
"source": [
"out_channels1 = 3 ## Note: this is the number of parameter on which to compute Multiparameter Persistence; keep it low!\n",
"first_gcn = GCN(in_channels=train.x.shape[-1], hidden_channels=50, num_layers=5, out_channels=out_channels1, dropout=.2)"
"first_gcn = GCN(in_channels=train.x.shape[-1], hidden_channels=50, num_layers=5, out_channels=out_channels1)"
]
},
{
Expand All @@ -184,7 +184,7 @@
{
"data": {
"text/plain": [
"(torch.Size([14479, 3]), torch.float32)"
"(torch.Size([3371, 3]), torch.float32)"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -271,8 +271,8 @@
" ) # empty -> -inf\n",
" st = st.grid_squeeze(diff_grid, coordinate_values=True)\n",
" \n",
" # if num_parameters == 2:\n",
" # st.collapse_edges(-1)\n",
" if num_parameters == 2:\n",
" st.collapse_edges(-1)\n",
" sms = mp.signed_measure(st, degrees=self.degrees, coordinate_measure=True)\n",
" return sms\n",
"\n",
Expand Down Expand Up @@ -351,7 +351,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[KeOps] Warning : cuda was detected, but driver API could not be initialized. Switching to cpu only.\n"
"[KeOps] Warning : Cuda libraries were not detected on the system or could not be loaded ; using cpu only mode\n"
]
},
{
Expand Down Expand Up @@ -406,15 +406,15 @@
"\n",
" biases = torch.stack(\n",
" [\n",
" torch.diag(torch.rand(self.num_parameters, dtype=dtype))\n",
" 10*torch.diag(torch.rand(self.num_parameters, dtype=dtype))\n",
" for _ in range(self.num_convolutions)\n",
" ],\n",
" dim=0,\n",
" ).type(dtype)\n",
" self.Rs = nn.Parameter(\n",
" # torch.randn(\n",
" torch.randn(\n",
" # torch.rand(\n",
" torch.zeros(\n",
" # torch.zeros(\n",
" (self.num_convolutions, num_parameters, num_parameters),\n",
" dtype=dtype,\n",
" requires_grad=True,\n",
Expand All @@ -438,7 +438,7 @@
" ):\n",
" from multipers.ml.convolutions import batch_signed_measure_convolutions\n",
" kernel_matrices = (\n",
" # The KDE implementation expects the inverse of the covariance for multiparameter kernels\n",
" # This KDE implementation expects the inverse of the covariance for multiparameter kernels\n",
" (R.T @ R).inverse()\n",
" for R in self.Rs\n",
" )\n",
Expand Down Expand Up @@ -478,18 +478,10 @@
"id": "d8ca5384-8638-4ae4-9111-1705aae7c312",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 1) of formula (d*e)*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,3,0), b=Var(1,3,1), c=Var(2,9,2), d=Var(3,1,2), e=Var(4,1,0) ... OK\n",
"[pyKeOps] Compiling pykeops cpp fd51638efa module ... OK\n"
]
},
{
"data": {
"text/plain": [
"torch.Size([405, 10])"
"torch.Size([188, 10])"
]
},
"execution_count": 17,
Expand Down Expand Up @@ -531,16 +523,16 @@
"data": {
"text/plain": [
"GraphModel(\n",
" (first_gcn): GCN(62, 2, num_layers=2)\n",
" (first_gcn): GCN(13, 2, num_layers=2)\n",
" (topological_layer): Graph2SMLayer()\n",
" (vectorization_layer): SMConvLayer(\n",
" (final_reshape): Sequential(\n",
" (0): Linear(in_features=1000, out_features=5, bias=True)\n",
" (0): Linear(in_features=400, out_features=2, bias=True)\n",
" (1): ReLU()\n",
" )\n",
" )\n",
" (classifier): Sequential(\n",
" (0): Linear(in_features=5, out_features=2, bias=True)\n",
" (0): Linear(in_features=2, out_features=2, bias=True)\n",
" (1): ReLU()\n",
" (2): Softmax(dim=-1)\n",
" )\n",
Expand Down Expand Up @@ -586,7 +578,10 @@
"graphclassifier = GraphModel(\n",
" in_channels = _stuff.x.shape[1], \n",
" out_channels = np.unique(_stuff.y).shape[0],\n",
" num_parameters =2,\n",
" hidden_channels=10,\n",
" num_layers=2,\n",
" num_parameters=2,\n",
" num_convolutions=2,\n",
" resolution=10,\n",
")\n",
"graphclassifier"
Expand All @@ -598,22 +593,14 @@
"id": "080f67ea-477f-4f84-8937-227c37b78646",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 1) of formula (d*e)*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,2,0), b=Var(1,2,1), c=Var(2,4,2), d=Var(3,1,2), e=Var(4,1,0) ... OK\n",
"[pyKeOps] Compiling pykeops cpp 947eef4777 module ... OK\n"
]
},
{
"data": {
"text/plain": [
"tensor([[0.6072, 0.3928],\n",
" [0.3750, 0.6250],\n",
" [0.3767, 0.6233],\n",
" [0.3420, 0.6580],\n",
" [0.3752, 0.6248]], grad_fn=<SliceBackward0>)"
"tensor([[0.4823, 0.5177],\n",
" [0.4811, 0.5189],\n",
" [0.4811, 0.5189],\n",
" [0.4806, 0.5194],\n",
" [0.4812, 0.5188]], grad_fn=<SliceBackward0>)"
]
},
"execution_count": 20,
Expand Down Expand Up @@ -641,52 +628,29 @@
"metadata": {},
"outputs": [],
"source": [
"num_epoch = 200\n",
"num_epoch = 100\n",
"batch_size = len(train_dataset)\n",
"data_loader = DataLoader(train_dataset,batch_size=batch_size)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 24,
"id": "2ce73fcb-0570-43ca-914e-81b37e3702e2",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Current acc 0.214, loss 0.8241797685623169, : 0%| | 0/200 [00:01<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 0) of formula (-1/2*((f*(d*e))*(MatVecMult(c,a-b)+VecMatMult(a-b,c))))*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,2,0), b=Var(1,2,1), c=Var(2,4,2), d=Var(3,1,2), e=Var(4,1,0), f=Var(5,1,1) ... OK\n",
"[pyKeOps] Compiling pykeops cpp c836920a74 module ... OK\n",
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 1) of formula (1/2*((f*(d*e))*(MatVecMult(c,a-b)+VecMatMult(a-b,c))))*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,2,0), b=Var(1,2,1), c=Var(2,4,2), d=Var(3,1,2), e=Var(4,1,0), f=Var(5,1,1) ... OK\n",
"[pyKeOps] Compiling pykeops cpp dd41e8c2a6 module ... OK\n",
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 0) of formula (-1/2*((f*(d*e))*TensorProd(a-b,a-b)))*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,2,0), b=Var(1,2,1), c=Var(2,4,2), d=Var(3,1,2), e=Var(4,1,0), f=Var(5,1,1) ... OK\n",
"[pyKeOps] Compiling pykeops cpp d4f9325c2b module ... OK\n",
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 0) of formula (e*f)*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,2,0), b=Var(1,2,1), c=Var(2,4,2), e=Var(4,1,0), f=Var(5,1,1) ... OK\n",
"[pyKeOps] Compiling pykeops cpp e25a464c96 module ... OK\n",
"[KeOps] Generating code for Sum_Reduction reduction (with parameters 0) of formula (d*f)*Exp(-1/2*(c|TensorProd(a-b,a-b))) with a=Var(0,2,0), b=Var(1,2,1), c=Var(2,4,2), d=Var(3,1,2), f=Var(5,1,1) ... OK\n",
"[pyKeOps] Compiling pykeops cpp b01d4ec1ad module ... OK\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Current acc 0.783, loss 0.5174151062965393, constant to 0: 100%|████████████████████████████████████████████████████████████| 200/200 [06:03<00:00, 1.82s/it]\n"
"Current acc 0.775, loss 0.518334150314331, : 100%|█| 100/100 [01:12<00:00, 1.38\n"
]
}
],
"source": [
"graphclassifier.train()\n",
"loss = torch.nn.CrossEntropyLoss()\n",
"optim = torch.optim.Adam(graphclassifier.parameters(), lr=1e-3, weight_decay=0.1)\n",
"optim = torch.optim.Adam(graphclassifier.parameters(), lr=1e-2)\n",
"losses = []\n",
"with tqdm(range(num_epoch)) as epoch:\n",
" for i in epoch:\n",
Expand All @@ -708,24 +672,24 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 25,
"id": "8062c6e8-3b61-412e-98fd-777d9e3405be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor(0.4847, grad_fn=<DivBackward1>), tensor(0.9268))"
"(tensor(0.5426, grad_fn=<DivBackward1>), tensor(0.6842))"
]
},
"execution_count": 23,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graphclassifier.eval()\n",
"test_stuff = next(iter(DataLoader(train_dataset,batch_size=len(test_dataset))))\n",
"test_stuff = next(iter(DataLoader(test_dataset,batch_size=len(test_dataset))))\n",
"prediction = graphclassifier(test_stuff)\n",
"verdad = one_hot(test_stuff.y).type(torch.float32)\n",
"loss(prediction, verdad), (prediction.argmax(1)==test_stuff.y).type(torch.float).abs().mean()"
Expand Down Expand Up @@ -756,7 +720,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.6"
}
},
"nbformat": 4,
Expand Down
45 changes: 26 additions & 19 deletions docs/notebooks/graph_classification.ipynb

Large diffs are not rendered by default.

43 changes: 16 additions & 27 deletions docs/notebooks/molecular_embedding.ipynb

Large diffs are not rendered by default.

141 changes: 76 additions & 65 deletions docs/notebooks/multipers_intro.ipynb

Large diffs are not rendered by default.

82 changes: 37 additions & 45 deletions docs/notebooks/rips_density_autodiff.ipynb

Large diffs are not rendered by default.

50 changes: 22 additions & 28 deletions docs/notebooks/simplex_tree_by_hand.ipynb

Large diffs are not rendered by default.

193 changes: 104 additions & 89 deletions docs/notebooks/time_series_classification.ipynb

Large diffs are not rendered by default.

0 comments on commit 0dae0d5

Please sign in to comment.